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

gh-118761: Optimise import time for ``string`` by AA-Turner · Pull Request #132037 · python/cpython · GitHub

/ cpython Public

gh-118761: Optimise import time for string - #132037

Merged
AA-Turner merged 6 commits into
python:mainfrom
AA-Turner:opt-string
Apr 8, 2025
Merged

gh-118761: Optimise import time for string#132037
AA-Turner merged 6 commits into
python:mainfrom
AA-Turner:opt-string

Conversation

AA-Turner commented Apr 3, 2025
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

This PR achieves a 27x improvement in import time for the string module. The main improvement comes from replacing Template.__init_subclass__() (GH-16256) with a descriptor class, allowing lazy import of the re module.

Current:

import string: cumulative time
mean: 9162.100 µs
median: 9133.000 µs
stdev: 66.662
min: 9071
max: 9301

This PR:

import string: cumulative time
mean: 334.967 µs
median: 329.000 µs
stdev: 13.438
min: 316
max: 368

picnixz 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

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

Comment thread Lib/string.py Outdated

python-cla-bot Bot commented Apr 6, 2025

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

AA-Turner requested a review from picnixz April 6, 2025 17:56

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

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

How the help output looks now?

Is __init_subclass__() needed anymore?

Comment thread Lib/string.py Outdated

Copy link
Copy Markdown
Member Author

Help output looks fine:

>>> from string import Template
>>> assert Template.flags is None
>>> 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...

>>> 

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

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

Comment thread Lib/string.py Outdated
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
AA-Turner enabled auto-merge (squash) April 8, 2025 09:44
AA-Turner merged commit ee36572 into python:main Apr 8, 2025
AA-Turner deleted the opt-string branch April 8, 2025 10:07
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL