Skip to content
Merged
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,2 @@
Avoid an unnecessary per-call memory allocation when matching :mod:`re`
patterns that have no capturing groups. Patch by Bernát Gábor.
14 changes: 10 additions & 4 deletions Modules/_sre/sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,16 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,

memset(state, 0, sizeof(SRE_STATE));

state->mark = PyMem_New(const void *, pattern->groups * 2);
if (!state->mark) {
PyErr_NoMemory();
goto err;
/* Patterns with no capturing groups never emit MARK opcodes and never
read state->mark (group 0's span comes from state->start/ptr), so skip
the allocation entirely -- state->mark stays NULL, which both the err
path and state_fini already free safely. */
if (pattern->groups) {
state->mark = PyMem_New(const void *, pattern->groups * 2);
if (!state->mark) {
PyErr_NoMemory();
goto err;
}
}
state->lastmark = -1;
state->lastindex = -1;
Expand Down
Loading