From bebd549330f3a1e5c4d5a96c8b6340639e3355e3 Mon Sep 17 00:00:00 2001 From: snurf198 Date: Mon, 17 Aug 2026 15:00:48 +0900 Subject: [PATCH 1/2] gh-150942: Optimize :func:`re.split` performance by using reference-stealing list append --- .../2026-08-17-14-49-02.gh-issue-150942.y1jM9t.rst | 3 +++ Modules/_sre/sre.c | 9 +++------ 2 files changed, 6 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-08-17-14-49-02.gh-issue-150942.y1jM9t.rst diff --git a/Misc/NEWS.d/next/Library/2026-08-17-14-49-02.gh-issue-150942.y1jM9t.rst b/Misc/NEWS.d/next/Library/2026-08-17-14-49-02.gh-issue-150942.y1jM9t.rst new file mode 100644 index 00000000000000..c7094cfd5219b1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-17-14-49-02.gh-issue-150942.y1jM9t.rst @@ -0,0 +1,3 @@ +Speed up :func:`re.split` 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 e742a25e4891fc..031e972aeba031 100644 --- a/Modules/_sre/sre.c +++ b/Modules/_sre/sre.c @@ -1273,8 +1273,7 @@ _sre_SRE_Pattern_split_impl(PatternObject *self, PyObject *string, ); if (!item) goto error; - status = PyList_Append(list, item); - Py_DECREF(item); + status = _PyList_AppendTakeRef((PyListObject *)list, item); if (status < 0) goto error; @@ -1283,8 +1282,7 @@ _sre_SRE_Pattern_split_impl(PatternObject *self, PyObject *string, item = state_getslice(&state, i+1, string, 0); if (!item) goto error; - status = PyList_Append(list, item); - Py_DECREF(item); + status = _PyList_AppendTakeRef((PyListObject *)list, item); if (status < 0) goto error; } @@ -1301,8 +1299,7 @@ _sre_SRE_Pattern_split_impl(PatternObject *self, PyObject *string, ); if (!item) goto error; - status = PyList_Append(list, item); - Py_DECREF(item); + status = _PyList_AppendTakeRef((PyListObject *)list, item); if (status < 0) goto error; From 5ffa72ab0db194d7bd202765765c9923c0c82013 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Mon, 17 Aug 2026 15:54:17 +0900 Subject: [PATCH 2/2] Delete Misc/NEWS.d/next/Library/2026-08-17-14-49-02.gh-issue-150942.y1jM9t.rst --- .../Library/2026-08-17-14-49-02.gh-issue-150942.y1jM9t.rst | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 Misc/NEWS.d/next/Library/2026-08-17-14-49-02.gh-issue-150942.y1jM9t.rst diff --git a/Misc/NEWS.d/next/Library/2026-08-17-14-49-02.gh-issue-150942.y1jM9t.rst b/Misc/NEWS.d/next/Library/2026-08-17-14-49-02.gh-issue-150942.y1jM9t.rst deleted file mode 100644 index c7094cfd5219b1..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-08-17-14-49-02.gh-issue-150942.y1jM9t.rst +++ /dev/null @@ -1,3 +0,0 @@ -Speed up :func:`re.split` by appending result items to the output list -without an extra reference-count round-trip (using the internal -reference-stealing list append helper).