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-118761: Optimise import time forstring#132037

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
AA-Turner merged 6 commits intopython:mainfromAA-Turner:opt-string
Apr 8, 2025

Conversation

AA-Turner
Copy link
Member

@AA-TurnerAA-Turner commentedApr 3, 2025
edited by bedevere-appbot
Loading

This PR achieves a 27x improvement in import time for thestring module. The main improvement comes from replacingTemplate.__init_subclass__() (GH-16256) with a descriptor class, allowing lazy import of there module.

Current:

import string: cumulative timemean: 9162.100 µsmedian: 9133.000 µsstdev: 66.662min: 9071max: 9301

This PR:

import string: cumulative timemean: 334.967 µsmedian: 329.000 µsstdev: 13.438min: 316max: 368

Zagoshipda reacted with thumbs up emojistonebig reacted with heart emojiWulian233, sergey-miryanov, and rawwar reacted with rocket emoji
Copy link
Member

@picnixzpicnixz left a comment

Choose a reason for hiding this comment

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

I'm very happy with this because I need to access many timesstring.* constants without needing the Template class so it's a very good optimization.

@python-cla-bot
Copy link

All commit authors signed the Contributor License Agreement.

CLA signed

@AA-TurnerAA-Turner requested a review frompicnixzApril 6, 2025 17:56
Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Interesting idea. The code looks intimidating, but it might work.

How the help output looks now?

Is__init_subclass__() needed anymore?

@AA-Turner
Copy link
MemberAuthor

Help output looks fine:

>>>from stringimport Template>>>assert Template.flagsisNone>>>help(Template)Help on class Template in module string:class Template(builtins.object) |  Template(template) | |  A string class for supporting $-substitutions. | |  Methods defined here: | |  __init__(self, template) |      Initialize self.  See help(type(self)) for accurate signature. | |  get_identifiers(self) | |  is_valid(self) | |  safe_substitute(self, mapping={}, /, **kws) | |  substitute(self, mapping={}, /, **kws) | |  ---------------------------------------------------------------------- |  Class methods defined here: | |  __init_subclass__() |      This method is called when a class is subclassed. | |      The default implementation does nothing. It may be |      overridden to extend subclasses. | |  ---------------------------------------------------------------------- |  Data descriptors defined here: | |  __dict__ |      dictionary for instance variables | |  __weakref__ |      list of weak references to the object | |  ---------------------------------------------------------------------- |  Data and other attributes defined here: | |  braceidpattern = None | |  delimiter = '$' | |  flags = re.IGNORECASE | |  idpattern = '(?a:[_a-z][_a-z0-9]*)' | |  pattern = re.compile('\n            \\$(?:\n              ...identifie...>>>

Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

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

It perhaps could be made simpler withclassmethod +property, but the future of this feature is not clear.

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@AA-TurnerAA-Turnerenabled auto-merge (squash)April 8, 2025 09:44
@AA-TurnerAA-Turner merged commitee36572 intopython:mainApr 8, 2025
39 checks passed
@AA-TurnerAA-Turner deleted the opt-string branchApril 8, 2025 10:07
seehwan pushed a commit to seehwan/cpython that referenced this pull requestApr 16, 2025
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@serhiy-storchakaserhiy-storchakaserhiy-storchaka left review comments

@picnixzpicnixzAwaiting requested review from picnixz

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.

3 participants
@AA-Turner@serhiy-storchaka@picnixz

[8]ページ先頭

©2009-2025 Movatter.jp