Skip to content

Commit 07a9427

Browse files
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 <donghee.na92@gmail.com>
1 parent e391052 commit 07a9427

1 file changed

Lines changed: 5 additions & 11 deletions

File tree

Objects/stringlib/split.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#error must include "stringlib/fastsearch.h" before including this module
55
#endif
66

7+
#include "pycore_list.h" // _PyList_AppendTakeRef()
8+
79
/* Overallocate the initial list to reduce the number of reallocs for small
810
split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three
911
resizes, to sizes 4, 8, then 16. Most observed string splits are for human
@@ -22,12 +24,8 @@
2224
(right) - (left)); \
2325
if (sub == NULL) \
2426
goto onError; \
25-
if (PyList_Append(list, sub)) { \
26-
Py_DECREF(sub); \
27-
goto onError; \
28-
} \
29-
else \
30-
Py_DECREF(sub);
27+
if (_PyList_AppendTakeRef((PyListObject *)list, sub)) \
28+
goto onError;
3129

3230
#define SPLIT_ADD(data, left, right) { \
3331
sub = STRINGLIB_NEW((data) + (left), \
@@ -37,12 +35,8 @@
3735
if (count < MAX_PREALLOC) { \
3836
PyList_SET_ITEM(list, count, sub); \
3937
} else { \
40-
if (PyList_Append(list, sub)) { \
41-
Py_DECREF(sub); \
38+
if (_PyList_AppendTakeRef((PyListObject *)list, sub)) \
4239
goto onError; \
43-
} \
44-
else \
45-
Py_DECREF(sub); \
4640
} \
4741
count++; }
4842

0 commit comments

Comments
 (0)