Skip to content

Commit 1430948

Browse files
committed
gh-150942: Optimize stringlib split/splitlines with _PyList_AppendTakeRef
1 parent e3287f6 commit 1430948

2 files changed

Lines changed: 9 additions & 11 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Speed up :meth:`str.split`, :meth:`str.rsplit` and :meth:`str.splitlines`
2+
(and the corresponding :class:`bytes` and :class:`bytearray` methods) by
3+
appending result items to the output list without an extra reference-count
4+
round-trip.

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)