From 191730a7c6b330edc0a157c9d35acac146c9c7dc Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Sun, 31 May 2026 23:42:05 +0200 Subject: [PATCH 1/2] gh-XXXXX: Speed up re.findall and re.sub/subn result building Append result items to the output list with _PyList_AppendTakeRef instead of PyList_Append followed by Py_DECREF, removing a reference-count round-trip per appended item (and a per-append lock on the free-threaded build). Applied to the result lists built by findall and the sub/subn helper, where the append is a meaningful share of the work. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../2026-05-31-12-00-00.gh-issue-XXXXX.Re7Ref.rst | 3 +++ Modules/_sre/sre.c | 13 +++++-------- 2 files changed, 8 insertions(+), 8 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-05-31-12-00-00.gh-issue-XXXXX.Re7Ref.rst diff --git a/Misc/NEWS.d/next/Library/2026-05-31-12-00-00.gh-issue-XXXXX.Re7Ref.rst b/Misc/NEWS.d/next/Library/2026-05-31-12-00-00.gh-issue-XXXXX.Re7Ref.rst new file mode 100644 index 000000000000000..63967108b1e0b3b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-05-31-12-00-00.gh-issue-XXXXX.Re7Ref.rst @@ -0,0 +1,3 @@ +Speed up :func:`re.findall`, :func:`re.sub` and :func:`re.subn` by appending +result items to the output list without an extra reference-count round-trip +(using the internal reference-stealing list append helper). diff --git a/Modules/_sre/sre.c b/Modules/_sre/sre.c index 7a07ed1d7aca20c..bd7b2ef9179c35b 100644 --- a/Modules/_sre/sre.c +++ b/Modules/_sre/sre.c @@ -42,6 +42,7 @@ static const char copyright[] = #include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION #include "pycore_dict.h" // _PyDict_Next() #include "pycore_long.h" // _PyLong_GetZero() +#include "pycore_list.h" // _PyList_AppendTakeRef() #include "pycore_moduleobject.h" // _PyModule_GetState() #include "pycore_tuple.h" // _PyTuple_FromPairSteal #include "pycore_unicodeobject.h" // _PyUnicode_Copy @@ -980,8 +981,7 @@ _sre_SRE_Pattern_findall_impl(PatternObject *self, PyObject *string, break; } - status = PyList_Append(list, item); - Py_DECREF(item); + status = _PyList_AppendTakeRef((PyListObject *)list, item); if (status < 0) goto error; @@ -1327,8 +1327,7 @@ pattern_subx(_sremodulestate* module_state, string, i, b); if (!item) goto error; - status = PyList_Append(list, item); - Py_DECREF(item); + status = _PyList_AppendTakeRef((PyListObject *)list, item); if (status < 0) goto error; @@ -1357,8 +1356,7 @@ pattern_subx(_sremodulestate* module_state, /* add to list */ if (item != Py_None) { - status = PyList_Append(list, item); - Py_DECREF(item); + status = _PyList_AppendTakeRef((PyListObject *)list, item); if (status < 0) goto error; } @@ -1375,8 +1373,7 @@ pattern_subx(_sremodulestate* module_state, string, i, state.endpos); if (!item) goto error; - status = PyList_Append(list, item); - Py_DECREF(item); + status = _PyList_AppendTakeRef((PyListObject *)list, item); if (status < 0) goto error; } From bf25fc59b3501f09a768747aef7e848cb326f7f7 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 5 Jun 2026 08:59:34 +0200 Subject: [PATCH 2/2] gh-150942: Set issue number on the re speedup news entry Co-Authored-By: Claude Opus 4.8 (1M context) --- ....Re7Ref.rst => 2026-05-31-12-00-00.gh-issue-150942.Re7Ref.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/NEWS.d/next/Library/{2026-05-31-12-00-00.gh-issue-XXXXX.Re7Ref.rst => 2026-05-31-12-00-00.gh-issue-150942.Re7Ref.rst} (100%) diff --git a/Misc/NEWS.d/next/Library/2026-05-31-12-00-00.gh-issue-XXXXX.Re7Ref.rst b/Misc/NEWS.d/next/Library/2026-05-31-12-00-00.gh-issue-150942.Re7Ref.rst similarity index 100% rename from Misc/NEWS.d/next/Library/2026-05-31-12-00-00.gh-issue-XXXXX.Re7Ref.rst rename to Misc/NEWS.d/next/Library/2026-05-31-12-00-00.gh-issue-150942.Re7Ref.rst