GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-109653: Defer importing `warnings` in several modules by AlexWaygood · Pull Request #110286 · python/cpython

Conversation

@AlexWaygood

@AlexWaygood AlexWaygood commented Oct 3, 2023 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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 a DeprecationWarning 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 importing warnings is so small, I've tried to avoid modules that make multiple uses of warnings -- it's not particularly DRY to have import warnings repeated several times in the same module. The one exception I made here is argparse: argparse really should be importing the absolute minimum set of modules given that it's a CLI framework (see #74338 for previous discussion).

  • Issue: Improve import time of various stdlib modules #109653

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
AlexWaygood added 2 commits October 3, 2023 14:58
@AlexWaygood
Defer importing warnings in several modules
0229f59
@AlexWaygood
news
653d1fa
@AlexWaygood
AlexWaygood requested a review from ethanfurman as a code owner October 3, 2023 14:19
@bedevere-app bedevere-app Bot added the awaiting review label Oct 3, 2023
@bedevere-app bedevere-app Bot mentioned this pull request Oct 3, 2023
Improve import time of various stdlib modules #109653
Closed
@AlexWaygood AlexWaygood added the performance Performance or resource usage label Oct 3, 2023
@AlexWaygood AlexWaygood changed the title gh-109653: Defer importing warnings in several modules gh-109653: Defer importing warnings in several modules Oct 3, 2023
hauntsaninja
hauntsaninja approved these changes Oct 4, 2023
View reviewed changes

@hauntsaninja hauntsaninja left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this 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"

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@bedevere-app bedevere-app Bot added awaiting core review and removed awaiting review labels Oct 4, 2023
@AlexWaygood

AlexWaygood commented Oct 4, 2023

Copy link
Copy Markdown
Member Author

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

Thanks for the review!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood
AlexWaygood merged commit bfe7e72 into python:main Oct 4, 2023
@AlexWaygood
AlexWaygood deleted the lazy-warnings-imports branch October 4, 2023 05:09
@bedevere-app bedevere-app Bot removed the awaiting core review label Oct 4, 2023
@AlexWaygood AlexWaygood mentioned this pull request Nov 18, 2023
Possible lazy import opportunities in pathlib #77621
Closed
@AlexWaygood
AlexWaygood restored the lazy-warnings-imports branch February 22, 2024 16:37
@AlexWaygood
AlexWaygood deleted the lazy-warnings-imports branch March 7, 2024 14:35
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
@AlexWaygood @Glyphack
pythongh-109653: Defer importing warnings in several modules (pytho…
c4f17c5
…n#110286)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@hauntsaninja hauntsaninja hauntsaninja approved these changes
@ethanfurman ethanfurman Awaiting requested review from ethanfurman ethanfurman is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

performance Performance or resource usage

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@AlexWaygood @hauntsaninja
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.