From 1430948d6c488e99740298ce02c7d60f6eda82c4 Mon Sep 17 00:00:00 2001 From: Seungki Kim Date: Mon, 17 Aug 2026 12:11:34 +0900 Subject: [PATCH 1/2] gh-150942: Optimize stringlib split/splitlines with _PyList_AppendTakeRef --- ...026-08-17-14-00-00.gh-issue-150942.sTrSpL.rst | 4 ++++ Objects/stringlib/split.h | 16 +++++----------- 2 files changed, 9 insertions(+), 11 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-08-17-14-00-00.gh-issue-150942.sTrSpL.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-17-14-00-00.gh-issue-150942.sTrSpL.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-17-14-00-00.gh-issue-150942.sTrSpL.rst new file mode 100644 index 000000000000000..0623a7736690e57 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-17-14-00-00.gh-issue-150942.sTrSpL.rst @@ -0,0 +1,4 @@ +Speed up :meth:`str.split`, :meth:`str.rsplit` and :meth:`str.splitlines` +(and the corresponding :class:`bytes` and :class:`bytearray` methods) by +appending result items to the output list without an extra reference-count +round-trip. diff --git a/Objects/stringlib/split.h b/Objects/stringlib/split.h index 0c11b7214e9b0b8..0b23b6430c8d636 100644 --- a/Objects/stringlib/split.h +++ b/Objects/stringlib/split.h @@ -4,6 +4,8 @@ #error must include "stringlib/fastsearch.h" before including this module #endif +#include "pycore_list.h" // _PyList_AppendTakeRef() + /* Overallocate the initial list to reduce the number of reallocs for small split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three resizes, to sizes 4, 8, then 16. Most observed string splits are for human @@ -22,12 +24,8 @@ (right) - (left)); \ if (sub == NULL) \ goto onError; \ - if (PyList_Append(list, sub)) { \ - Py_DECREF(sub); \ - goto onError; \ - } \ - else \ - Py_DECREF(sub); + if (_PyList_AppendTakeRef((PyListObject *)list, sub)) \ + goto onError; #define SPLIT_ADD(data, left, right) { \ sub = STRINGLIB_NEW((data) + (left), \ @@ -37,12 +35,8 @@ if (count < MAX_PREALLOC) { \ PyList_SET_ITEM(list, count, sub); \ } else { \ - if (PyList_Append(list, sub)) { \ - Py_DECREF(sub); \ + if (_PyList_AppendTakeRef((PyListObject *)list, sub)) \ goto onError; \ - } \ - else \ - Py_DECREF(sub); \ } \ count++; } From f3fd338f20907014e57588703af9fffd0dee7408 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Mon, 17 Aug 2026 12:32:24 +0900 Subject: [PATCH 2/2] Delete Misc/NEWS.d/next/Core_and_Builtins/2026-08-17-14-00-00.gh-issue-150942.sTrSpL.rst --- .../2026-08-17-14-00-00.gh-issue-150942.sTrSpL.rst | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-08-17-14-00-00.gh-issue-150942.sTrSpL.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-17-14-00-00.gh-issue-150942.sTrSpL.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-17-14-00-00.gh-issue-150942.sTrSpL.rst deleted file mode 100644 index 0623a7736690e57..000000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-17-14-00-00.gh-issue-150942.sTrSpL.rst +++ /dev/null @@ -1,4 +0,0 @@ -Speed up :meth:`str.split`, :meth:`str.rsplit` and :meth:`str.splitlines` -(and the corresponding :class:`bytes` and :class:`bytearray` methods) by -appending result items to the output list without an extra reference-count -round-trip.