python / python/cpython

Cache the match object in the compiled re pattern

Open
#153,161 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

performance stdlib topic-regex type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

Proposal:

re allocates a new match object for every successful match. Matches of one pattern all have the same size, and in a typical loop the previous match dies just before the next one is created.

Keep one dead match object in the compiled pattern and reuse it for the next match instead of allocating. This saves 15-18 ns per successful match (e.g. p.match('42') goes 89 ns → 71 ns, and the string-to-Fraction constructor gains ~6%), with no behavior change. The cache slot is accessed with atomic operations; validated with the test suite, refleak runs, and a concurrent stress test on the free-threaded build.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Linked PRs
  • gh-153162

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the compiled re pattern implementation and inspect how successful matches are allocated. Use the existing test suite to confirm behavior remains unchanged, then run refleak checks and the concurrent stress test on a free-threaded build. Done means the dead match object is reused safely and the reported performance improvement is preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
performance
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.