https://github.com/python/cpython/commit/51e0f2b06c042b8742145ce094323a34b61f02aa
commit: 51e0f2b06c042b8742145ce094323a34b61f02aa
branch: main
author: Adam Turner <9087854+aa-tur...@users.noreply.github.com>
committer: AA-Turner <9087854+aa-tur...@users.noreply.github.com>
date: 2025-03-31T18:31:39+01:00
summary:

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

Co-authored-by: Bénédikt Tran <10796600+picn...@users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2025-03-31-02-06-57.gh-issue-107369.8M-HVz.rst
M Lib/textwrap.py

diff --git a/Lib/textwrap.py b/Lib/textwrap.py
index bb6a1186316275..bac98c99e41df8 100644
--- a/Lib/textwrap.py
+++ b/Lib/textwrap.py
@@ -451,19 +451,20 @@ def indent(text, prefix, predicate=None):
     it will default to adding 'prefix' to all non-empty lines that do not
     consist solely of whitespace characters.
     """
-    if predicate is None:
-        # str.splitlines(True) doesn't produce empty string.
-        #  ''.splitlines(True) => []
-        #  'foo\n'.splitlines(True) => ['foo\n']
-        # So we can use just `not s.isspace()` here.
-        predicate = lambda s: not s.isspace()
-
     prefixed_lines = []
-    for line in text.splitlines(True):
-        if predicate(line):
-            prefixed_lines.append(prefix)
-        prefixed_lines.append(line)
-
+    if predicate is None:
+        # str.splitlines(keepends=True) doesn't produce the empty string,
+        # so we need to use `str.isspace()` rather than a truth test.
+        # Inlining the predicate leads to a ~30% performance improvement.
+        for line in text.splitlines(True):
+            if not line.isspace():
+                prefixed_lines.append(prefix)
+            prefixed_lines.append(line)
+    else:
+        for line in text.splitlines(True):
+            if predicate(line):
+                prefixed_lines.append(prefix)
+            prefixed_lines.append(line)
     return ''.join(prefixed_lines)
 
 
diff --git 
a/Misc/NEWS.d/next/Library/2025-03-31-02-06-57.gh-issue-107369.8M-HVz.rst 
b/Misc/NEWS.d/next/Library/2025-03-31-02-06-57.gh-issue-107369.8M-HVz.rst
new file mode 100644
index 00000000000000..9b1bd9e7502bf0
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-03-31-02-06-57.gh-issue-107369.8M-HVz.rst
@@ -0,0 +1,2 @@
+Improved performance of :func:`textwrap.dedent` by an average of ~1.3x.
+Patch by Adam Turner.

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to