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-118761: Optimise import time for ``string`` by AA-Turner · Pull Request #132037 · python/cpython

Conversation

@AA-Turner

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

Uh oh!

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

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
  • Issue: Improve import time of various stdlib modules #118761

Sorry, something went wrong.

Uh oh!

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

Zagoshipda reacted with thumbs up emoji stonebig reacted with heart emoji Wulian233, sergey-miryanov, and rawwar reacted with rocket emoji
All reactions
  • 👍 1 reaction
  • ❤️ 1 reaction
  • 🚀 3 reactions
@AA-Turner
Optimise import time for string
ec78492
@AA-Turner AA-Turner added the performance Performance or resource usage label Apr 3, 2025
@AA-Turner
AA-Turner requested a review from serhiy-storchaka April 3, 2025 00:41
@bedevere-app bedevere-app Bot added the awaiting core review label Apr 3, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Apr 3, 2025
Improve import time of various stdlib modules #118761
Closed
AA-Turner added 2 commits April 3, 2025 02:52
@AA-Turner
Refactor to a common classmethod
2b542c7
@AA-Turner
Add comment
e28e43f
@Marius-Juston Marius-Juston mentioned this pull request Apr 3, 2025
gh-137855: email.quoprimime removing re import #132046
Closed
@AA-Turner
AA-Turner requested a review from picnixz April 6, 2025 04:37
picnixz
picnixz reviewed Apr 6, 2025
View reviewed changes

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

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.

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.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/string.py Resolved

Uh oh!

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

@python-cla-bot

python-cla-bot Bot commented Apr 6, 2025

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

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

@AA-Turner
Use None as the sentinel
056dd07
@AA-Turner
AA-Turner requested a review from picnixz April 6, 2025 17:56
serhiy-storchaka
serhiy-storchaka reviewed Apr 7, 2025
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

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.

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

How the help output looks now?

Is __init_subclass__() needed anymore?

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/string.py Resolved

Uh oh!

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

@AA-Turner

AA-Turner commented Apr 7, 2025

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...

>>> 
All reactions

Sorry, something went wrong.

Uh oh!

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

@AA-Turner
Make _TemplatePattern a singleton
bb3605a
serhiy-storchaka
serhiy-storchaka reviewed Apr 8, 2025
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

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.

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

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/string.py Resolved

Uh oh!

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

@AA-Turner @serhiy-storchaka
Serhiy's suggestion
6fb9b5d
Co-authored-by: Serhiy Storchaka <[email protected]>
@AA-Turner
AA-Turner enabled auto-merge (squash) April 8, 2025 09:44
@AA-Turner
AA-Turner merged commit ee36572 into python:main Apr 8, 2025
@bedevere-app bedevere-app Bot removed the awaiting core review label Apr 8, 2025
@AA-Turner
AA-Turner deleted the opt-string branch April 8, 2025 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka serhiy-storchaka left review comments
@picnixz picnixz Awaiting requested review from picnixz

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.

3 participants

@AA-Turner @serhiy-storchaka @picnixz
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.