Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Speed up :func:`re.findall`, :func:`re.sub` and :func:`re.subn` by appending
result items to the output list without an extra reference-count round-trip
(using the internal reference-stealing list append helper).
13 changes: 5 additions & 8 deletions Modules/_sre/sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ static const char copyright[] =
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION
#include "pycore_dict.h" // _PyDict_Next()
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_list.h" // _PyList_AppendTakeRef()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_tuple.h" // _PyTuple_FromPairSteal
#include "pycore_unicodeobject.h" // _PyUnicode_Copy
Expand Down Expand Up @@ -980,8 +981,7 @@ _sre_SRE_Pattern_findall_impl(PatternObject *self, PyObject *string,
break;
}

status = PyList_Append(list, item);
Py_DECREF(item);
status = _PyList_AppendTakeRef((PyListObject *)list, item);
if (status < 0)
goto error;

Expand Down Expand Up @@ -1327,8 +1327,7 @@ pattern_subx(_sremodulestate* module_state,
string, i, b);
if (!item)
goto error;
status = PyList_Append(list, item);
Py_DECREF(item);
status = _PyList_AppendTakeRef((PyListObject *)list, item);
if (status < 0)
goto error;

Expand Down Expand Up @@ -1357,8 +1356,7 @@ pattern_subx(_sremodulestate* module_state,

/* add to list */
if (item != Py_None) {
status = PyList_Append(list, item);
Py_DECREF(item);
status = _PyList_AppendTakeRef((PyListObject *)list, item);
if (status < 0)
goto error;
}
Expand All @@ -1375,8 +1373,7 @@ pattern_subx(_sremodulestate* module_state,
string, i, state.endpos);
if (!item)
goto error;
status = PyList_Append(list, item);
Py_DECREF(item);
status = _PyList_AppendTakeRef((PyListObject *)list, item);
if (status < 0)
goto error;
}
Expand Down
Loading