Skip to content

Commit bebd549

Browse files
committed
gh-150942: Optimize :func:re.split performance by using reference-stealing list append
1 parent 7a845ce commit bebd549

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Speed up :func:`re.split` by appending result items to the output list
2+
without an extra reference-count round-trip (using the internal
3+
reference-stealing list append helper).

Modules/_sre/sre.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,8 +1273,7 @@ _sre_SRE_Pattern_split_impl(PatternObject *self, PyObject *string,
12731273
);
12741274
if (!item)
12751275
goto error;
1276-
status = PyList_Append(list, item);
1277-
Py_DECREF(item);
1276+
status = _PyList_AppendTakeRef((PyListObject *)list, item);
12781277
if (status < 0)
12791278
goto error;
12801279

@@ -1283,8 +1282,7 @@ _sre_SRE_Pattern_split_impl(PatternObject *self, PyObject *string,
12831282
item = state_getslice(&state, i+1, string, 0);
12841283
if (!item)
12851284
goto error;
1286-
status = PyList_Append(list, item);
1287-
Py_DECREF(item);
1285+
status = _PyList_AppendTakeRef((PyListObject *)list, item);
12881286
if (status < 0)
12891287
goto error;
12901288
}
@@ -1301,8 +1299,7 @@ _sre_SRE_Pattern_split_impl(PatternObject *self, PyObject *string,
13011299
);
13021300
if (!item)
13031301
goto error;
1304-
status = PyList_Append(list, item);
1305-
Py_DECREF(item);
1302+
status = _PyList_AppendTakeRef((PyListObject *)list, item);
13061303
if (status < 0)
13071304
goto error;
13081305

0 commit comments

Comments
 (0)