Skip to content

gh-107369: Optimise textwrap.indent() - #131923

Merged
AA-Turner merged 1 commit into
python:mainfrom
AA-Turner:tw-indent
Mar 31, 2025
Merged

AA-Turner merged 1 commit into
python:mainfrom
AA-Turner:tw-indent

Conversation

@AA-Turner

@AA-Turner AA-Turner commented Mar 31, 2025 •

Copy link
Copy Markdown
Member

Follow-up to #131919, I noticed there was opportunity for improvement here too, a ~30% speed-up.

Benchmark textwrap new
indent 3.59 ms 2.81 ms: 1.28x faster

Benchmarked by indenting the contents of Objects/unicodeobject.c.

A

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

This is funny because this was my very first attempt at CPython: https://github.com/python/cpython/pull/107426/files :') It was rejected at that time (#107426 (comment)) though

@AA-Turner

Copy link
Copy Markdown
Member Author

Oh, I didn't see that PR! I think the optimisation is worth it, it's a small change and a reasonable speed-up for the default case (predicate=None). I'll give you co-authored-by credit!

A

@AA-Turner
AA-Turner merged commit 51e0f2b into python:main Mar 31, 2025
@AA-Turner
AA-Turner deleted the tw-indent branch March 31, 2025 17:31
@@ -0,0 +1,2 @@
Improved performance of :func:`textwrap.dedent` by an average of ~1.3x.

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.

s/dedent/indent/

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

Labels

None yet

3 participants