Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-109653: Defer importingwarnings in several modules#110286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
AlexWaygood merged 2 commits intopython:mainfromAlexWaygood:lazy-warnings-imports
Oct 4, 2023

Conversation

AlexWaygood
Copy link
Member

@AlexWaygoodAlexWaygood commentedOct 3, 2023
edited by bedevere-appbot
Loading

warnings is a pretty cheap import, so the import-time saving from deferring it is usually pretty minimal. However, it's also a textbook example of an import that should be deferred wherever reasonable. Most of the time, it's only imported so that aDeprecationWarning can be emitted, and we should try to make it so that users only have to pay the (small) performance penalty from it being imported if they actually hit the specific code path that's deprecated.

Nonetheless, since the performance impact of importingwarnings is so small, I've tried to avoid modules that make multiple uses ofwarnings -- it's not particularly DRY to have import warnings repeated several times in the same module. The one exception I made here isargparse:argparse really should be importing the absolute minimum set of modules given that it's a CLI framework (see#74338 for previous discussion).

@AlexWaygoodAlexWaygood added the performancePerformance or resource usage labelOct 3, 2023
@AlexWaygoodAlexWaygood changed the titlegh-109653: Defer importing warnings in several modulesgh-109653: Defer importingwarnings in several modulesOct 3, 2023
Copy link
Contributor

@hauntsaninjahauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thanks, look good!

This is somewhat marginal. On my laptop, the time it takes to import warnings is below the noise floor. On another system I have access to, it looks like it's about 0.3ms. (Both measuring Python 3.11 builds)

λ hyperfine --warmup 3 'python -c "pass"' 'python -c "import warnings"'Benchmark 1: python -c "pass"  Time (mean ± σ):      39.0 ms ±   1.4 ms    [User: 30.8 ms, System: 8.3 ms]  Range (min … max):    34.4 ms …  41.3 ms    78 runs Benchmark 2: python -c "import warnings"  Time (mean ± σ):      39.3 ms ±   1.9 ms    [User: 28.9 ms, System: 10.4 ms]  Range (min … max):    30.4 ms …  47.0 ms    69 runs Summary  python -c "pass" ran    1.01 ± 0.06 times faster than python -c "import warnings"

AlexWaygood reacted with thumbs up emoji
@AlexWaygood
Copy link
MemberAuthor

Yeah, definitely not the biggest contributor to import-time issues, but probably one of the easiest to tackle :)

Thanks for the review!

@AlexWaygoodAlexWaygood merged commitbfe7e72 intopython:mainOct 4, 2023
@AlexWaygoodAlexWaygood deleted the lazy-warnings-imports branchOctober 4, 2023 05:09
@AlexWaygoodAlexWaygood restored the lazy-warnings-imports branchFebruary 22, 2024 16:37
@AlexWaygoodAlexWaygood deleted the lazy-warnings-imports branchMarch 7, 2024 14:35
Glyphack pushed a commit to Glyphack/cpython that referenced this pull requestSep 2, 2024
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@hauntsaninjahauntsaninjahauntsaninja approved these changes

@ethanfurmanethanfurmanAwaiting requested review from ethanfurmanethanfurman is a code owner

Assignees
No one assigned
Labels
performancePerformance or resource usage
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@AlexWaygood@hauntsaninja

[8]ページ先頭

©2009-2025 Movatter.jp