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-137855: Optimise import time for textwrap by AA-Turner · Pull Request #131956 · python/cpython

Conversation

@AA-Turner

@AA-Turner AA-Turner commented Mar 31, 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 uses self-overwriting descriptors to implement compiled class-level patterns. An unorthodox approach, but I think cleaner than using is None checks everywhere. If this looks reasonable, I'll add NEWS etc.

A

  • Issue: Improve import time of various stdlib modules #118761
  • Issue: Improve import time of various stdlib modules #137855

Sorry, something went wrong.

Uh oh!

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

All reactions
@AA-Turner
AA-Turner requested a review from picnixz March 31, 2025 19:30
@bedevere-app bedevere-app Bot mentioned this pull request Mar 31, 2025
Improve import time of various stdlib modules #118761
Closed
picnixz
picnixz reviewed Mar 31, 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.

That's an interesting and cleaner way to make it work but maybe we could add some tests as well, just in case?

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/textwrap.py Resolved

Uh oh!

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

@AA-Turner

AA-Turner commented Mar 31, 2025

Copy link
Copy Markdown
Member Author

we could add some tests as well, just in case?

Happy to, do you mean for _cached_regex, or tests for TextWrapper, or?

A

All reactions

Sorry, something went wrong.

Uh oh!

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

@picnixz

picnixz commented Mar 31, 2025

Copy link
Copy Markdown
Member

I think tests with accessing / resetting / deleting the attributes (they are publicly named even though they are not exposed, just in case someone is subclassing them [again they shouldn't be part of the public API but well.. you never know; and it would be good to just check that the patterns are the expected ones.

But yes, also a small test for the descriptor with a fake pattern and check that its implementation is correct (well it should be AFAIK).

All reactions

Sorry, something went wrong.

Uh oh!

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

vstinner
vstinner reviewed Apr 2, 2025
View reviewed changes

@vstinner vstinner 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 not excited by this change, IMO it goes too far :-(

This change is an optimization, you should provide a benchmark to prove that the change is worth it.

Sorry, something went wrong.

Uh oh!

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

All reactions
@AA-Turner

AA-Turner commented Apr 2, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

This change is an optimization, you should provide a benchmark to prove that the change is worth it.

@vstinner sorry for not including them. Benchmarks show a consistent large decrease in import time, around 10ms on a standard release build in Windows. See below for detailed numbers:

Using -X importtime -Sc 'import textwrap'

Old: import time: 907 | 10796 | textwrap_current
New: import time: 311 | 311 | textwrap_new

The same command, on the default PCbuild\build.bat build:

Old: import time: 5272 | 29988 | textwrap_current
New: import time: 1076 | 1076 | textwrap_new

With hyperfine, Python 3.13.2:

PS>  hyperfine -N --warmup 5 "python -c ''" "python -c 'import textwrap_current'" "python -c 'import textwrap_new'"
Benchmark 1: python -c ''
  Time (mean ± σ):      24.1 ms ±   0.9 ms    [User: 5.4 ms, System: 3.4 ms]
  Range (min … max):    22.3 ms …  26.4 ms    124 runs

Benchmark 2: python -c 'import textwrap_current'
  Time (mean ± σ):      34.6 ms ±   1.5 ms    [User: 8.3 ms, System: 4.0 ms]
  Range (min … max):    29.5 ms …  38.1 ms    85 runs

Benchmark 3: python -c 'import textwrap_new'
  Time (mean ± σ):      24.2 ms ±   0.9 ms    [User: 8.3 ms, System: 3.7 ms]
  Range (min … max):    21.8 ms …  27.0 ms    124 runs

A

All reactions

Sorry, something went wrong.

Uh oh!

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

@python-cla-bot

python-cla-bot Bot commented Apr 6, 2025 •
edited
Loading

Uh oh!

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

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.

@StanFromIreland StanFromIreland changed the title gh-118761: Optimise import time for textwrap gh-137855: Optimise import time for textwrap Aug 20, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Aug 20, 2025
Improve import time of various stdlib modules #137855
Closed
@github-actions

github-actions Bot commented Apr 23, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

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

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 23, 2026
@vstinner

vstinner commented Apr 27, 2026

Copy link
Copy Markdown
Member

Can this change be made simpler by using lazy import re?

All reactions

Sorry, something went wrong.

Uh oh!

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

@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label May 14, 2026
@picnixz

picnixz commented May 24, 2026

Copy link
Copy Markdown
Member

Can this change be made simpler by using lazy import re?

I don't think so as the class body of the textwrap class will be executed at import time, so re will be reified directly.

All reactions

Sorry, something went wrong.

Uh oh!

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

vstinner
vstinner reviewed May 25, 2026
View reviewed changes
Comment thread Lib/textwrap.py
whitespace = r'[%s]' % re.escape(_whitespace)
nowhitespace = '[^' + whitespace[1:]
wordsep_re = re.compile(r'''
whitespace = fr'[{_whitespace}]'

@vstinner vstinner May 25, 2026

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.

Half of this PR is code refactoring which makes the PR harder to review. I would prefer to have a separated PR just for the refactoring.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/textwrap.py
__all__ = ['TextWrapper', 'wrap', 'fill', 'dedent', 'indent', 'shorten']


class _cached_regex:

@vstinner vstinner May 25, 2026

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.

Can you add a comment explaining the purpose of this class (lazy import)?

Sorry, something went wrong.

Uh oh!

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

All reactions
@github-actions

github-actions Bot commented Aug 28, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

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

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Aug 28, 2026
AA-Turner added 2 commits October 20, 2026 23:15
@AA-Turner @ambv
Optimise import time for textwrap
f94844b
@AA-Turner @ambv
Add tests
d3bd833
@ambv
ambv force-pushed the opt-tw branch from b67e4aa to d3bd833 Compare October 20, 2026 21:17
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Oct 20, 2026
@ambv
Fix CI: add news entry for textwrap import-time optimisation (pythong…
b4e3ec3
…h-137855)

Co-authored-by: Muse Code (Meta Muse Spark)
@ambv
ambv force-pushed the opt-tw branch from ad5a3ec to b4e3ec3 Compare October 21, 2026 01:52
@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Oct 25, 2026
@vstinner

vstinner commented Nov 4, 2026

Copy link
Copy Markdown
Member

@AA-Turner: Are you still working on this PR? It's mostly idle for a few months. Maybe it should just be closed?

All reactions

Sorry, something went wrong.

Uh oh!

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner left review comments
@picnixz picnixz picnixz left review comments

Uh oh!

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

Assignees

No one assigned

Labels

type-feature A feature request or enhancement

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.

4 participants

@AA-Turner @picnixz @vstinner @ambv
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.