From 37551c9cef307ca3172c60b28c7de9db921808ad Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sat, 29 Jul 2023 15:37:23 +0900 Subject: [PATCH] gh-107369: optimize textwrap.indent() (#107374) --- Doc/whatsnew/3.13.rst | 3 ++- Lib/textwrap.py | 18 ++++++++++++------ ...3-07-28-14-56-35.gh-issue-107369.bvTq8F.rst | 2 ++ 3 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-07-28-14-56-35.gh-issue-107369.bvTq8F.rst diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 7cad5d1ec02..e20832a3a4c 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -150,7 +150,8 @@ typing Optimizations ============= - +* :func:`textwrap.indent` is now ~30% faster than before for large input. + (Contributed by Inada Naoki in :gh:`107369`.) Deprecated diff --git a/Lib/textwrap.py b/Lib/textwrap.py index 98bedd27ea3..7ca393d1c37 100644 --- a/Lib/textwrap.py +++ b/Lib/textwrap.py @@ -476,13 +476,19 @@ def indent(text, prefix, predicate=None): consist solely of whitespace characters. """ if predicate is None: - def predicate(line): - return line.strip() + # 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() - def prefixed_lines(): - for line in text.splitlines(True): - yield (prefix + line if predicate(line) else line) - return ''.join(prefixed_lines()) + prefixed_lines = [] + for line in text.splitlines(True): + if predicate(line): + prefixed_lines.append(prefix) + prefixed_lines.append(line) + + return ''.join(prefixed_lines) if __name__ == "__main__": diff --git a/Misc/NEWS.d/next/Library/2023-07-28-14-56-35.gh-issue-107369.bvTq8F.rst b/Misc/NEWS.d/next/Library/2023-07-28-14-56-35.gh-issue-107369.bvTq8F.rst new file mode 100644 index 00000000000..76aeab65e90 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-07-28-14-56-35.gh-issue-107369.bvTq8F.rst @@ -0,0 +1,2 @@ +Optimize :func:`textwrap.indent`. It is ~30% faster for large input. Patch +by Inada Naoki.