gh-150942: Optimize stringlib split/splitlines with _PyList_AppendTakeRef (gh-155922)

corona10 <[email protected]>
Newsgroups gmane.comp.python.cvs
Message-ID <[email protected]>
https://github.com/python/cpython/commit/07a9427992a7c5132397bfc7b8fec08a185a4bcc
commit: 07a9427992a7c5132397bfc7b8fec08a185a4bcc
branch: main
author: Seungki Kim <[email protected]>
committer: corona10 <[email protected]>
date: 2026-08-17T14:34:25+09:00
summary:

gh-150942: Optimize stringlib split/splitlines with _PyList_AppendTakeRef (gh-155922)

* gh-150942: Optimize stringlib split/splitlines with _PyList_AppendTakeRef

* Delete Misc/NEWS.d/next/Core_and_Builtins/2026-08-17-14-00-00.gh-issue-150942.sTrSpL.rst

---------

Co-authored-by: Donghee Na <[email protected]>

files:
M Objects/stringlib/split.h

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++; }
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.