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

gh-137855: Optimise import time for textwrap by AA-Turner · Pull Request #131956 · python/cpython · GitHub

/ cpython Public

gh-137855: Optimise import time for textwrap - #131956

Draft
AA-Turner wants to merge 2 commits into
python:mainfrom
AA-Turner:opt-tw
Draft

gh-137855: Optimise import time for textwrap#131956
AA-Turner wants to merge 2 commits into
python:mainfrom
AA-Turner:opt-tw

Conversation

AA-Turner commented Mar 31, 2025
edited by bedevere-app Bot
Loading

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

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

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

Comment thread Lib/textwrap.py

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

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

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

AA-Turner commented Apr 2, 2025
edited
Loading

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

python-cla-bot Bot commented Apr 6, 2025

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

StanFromIreland changed the title gh-118761: Optimise import time for textwrap gh-137855: Optimise import time for textwrap Aug 20, 2025

Copy link
Copy Markdown

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

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

Copy link
Copy Markdown
Member

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

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

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.

Comment thread Lib/textwrap.py
whitespace = r'[%s]' % re.escape(_whitespace)
nowhitespace = '[^' + whitespace[1:]
wordsep_re = re.compile(r'''
whitespace = fr'[{_whitespace}]'

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

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.

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


class _cached_regex:

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

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

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL