Skip to content

gh-153161: Cache the match object in the compiled re pattern#153162

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:sre-match-freelist
Open

gh-153161: Cache the match object in the compiled re pattern#153162
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:sre-match-freelist

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Jul 6, 2026

Copy link
Copy Markdown
Member

A dead match object is kept in the pattern (retaining only its type reference) and resurrected for the next match instead of allocating a new one. Matches of one pattern all have the same size, and the next match is typically created just before the previous one dies.

Saves 15-18 ns per successful match. No refleaks; the cache slot is accessed with atomic operations and was stress-tested on the free-threaded build.

A dead match object is kept in the pattern (it retains only its type
reference) and resurrected for the next match instead of allocating a
new one.  Matches of one pattern all have the same size, and the next
match is typically created just before the previous one dies.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant