FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Speed-up lazy heapq import in collections by rhettinger · Pull Request #127538 · python/cpython · GitHub

Repository navigation

Speed-up lazy heapq import in collections - #127538

Merged
rhettinger merged 1 commit into
python:mainfrom
rhettinger:collections_lazy_import
Dec 3, 2024
Merged

rhettinger merged 1 commit into
python:mainfrom
rhettinger:collections_lazy_import

Conversation

Copy link
Copy Markdown
Contributor

It is faster to test for None than to go through the import logic to check sys.modules.

Baseline:

$ ./python.exe -m timeit -s 'from collections import Counter' -s 'c=Counter("abc")' 'c.most_common(1)'
500000 loops, best of 5: 481 nsec per loop

Patched:

$ ./python.exe -m timeit -s 'from collections import Counter' -s 'c=Counter("abc")' 'c.most_common(1)'
500000 loops, best of 5: 400 nsec per loop

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM.

I hope that in future such optimization will not be needed. The lookup in sys.modules has the same cost as the lookup in globals(), but there is currently additional cost of checking that the module is not partially imported -- this is two additional attribute lookups (__spec__ and other). I am planning to optimize them out.

Copy link
Copy Markdown
Contributor

There have been some similar optimizations before, and it is suggested to start with gh-110761

#118761

rhettinger merged commit dffb909 into python:main Dec 3, 2024
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Jan 8, 2025
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance or resource usage skip issue skip news

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL