python / python/cpython

Cache the match object in the compiled re pattern

オープン
#153,161 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

performance stdlib topic-regex type-feature
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

コンパイル済み re パターンの実装から始め、成功したマッチがどのように割り当てられるかを調べます。既存のテストスイートを使って動作が変わらないことを確認し、その後、free-threaded ビルドで refleak チェックと並行ストレステストを実行します。デッドマッチオブジェクトが安全に再利用され、報告されたパフォーマンス向上が維持されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
performance
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。