| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 27ebd9a commit c79e18a
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + Avoid an unnecessary per-call memory allocation when matching :mod:`re` | ||
| 2 | + patterns that have no capturing groups. Patch by Bernát Gábor. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -548,10 +548,16 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string, | |||
| 548 | 548 | ||
| 549 | 549 | memset(state, 0, sizeof(SRE_STATE)); | |
| 550 | 550 | ||
| 551 | - state->mark = PyMem_New(const void *, pattern->groups * 2); | ||
| 552 | - if (!state->mark) { | ||
| 553 | - PyErr_NoMemory(); | ||
| 554 | - goto err; | ||
| 551 | + /* Patterns with no capturing groups never emit MARK opcodes and never | ||
| 552 | + read state->mark (group 0's span comes from state->start/ptr), so skip | ||
| 553 | + the allocation entirely -- state->mark stays NULL, which both the err | ||
| 554 | + path and state_fini already free safely. */ | ||
| 555 | + if (pattern->groups) { | ||
| 556 | + state->mark = PyMem_New(const void *, pattern->groups * 2); | ||
| 557 | + if (!state->mark) { | ||
| 558 | + PyErr_NoMemory(); | ||
| 559 | + goto err; | ||
| 560 | + } | ||
| 555 | 561 | } | |
| 556 | 562 | state->lastmark = -1; | |
| 557 | 563 | state->lastindex = -1; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments