python / python/cpython

Improve speed of stdlib functions by replacing `re` uses

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

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

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

説明

We can often find the module re in the standard library modules but it can be replaced (if it is possible). I don't suggest removing it everywhere, there are places where its use is appropriate, but there are also places where it is an unnecessary solution and leads to unpleasant consequences (they can be found below)

Cons of regular expressions and reasons to replace regular expressions with functions and methods:

  1. We spend time to compile re pattern (one time, but anyway we spend it)
  2. In most cases simple string methods are faster (according to my benchmarks about 2x)
  3. We can remove import re which will affect import time
  4. Additionally: I think for those who don't know regular expressions, the code is more difficult to read and therefore difficult to maintain.

[!IMPORTANT]
For those who want to work on the issue, please:

  • Read https://devguide.python.org/getting-started/pull-request-lifecycle/ before anything else.
  • Select one function to improve. It's easier to review and possibly backport.
  • Always report benchmarks using pyperf, hyperfine, and tuna together with -X importtime to compare import times and execution time.
  • Open a pull request with the following title: gh-130167: Improve speed of `module.function` by replacing `re`
Linked PRs
  • gh-130170
  • gh-130242
  • gh-130243
  • gh-131919
  • gh-131925
  • gh-131924
  • gh-132065
  • gh-132666
  • gh-143292

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

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

はじめの一歩

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

調査の方向性

まず pull request のライフサイクルガイドを読み、その後、re を import している標準ライブラリモジュールを調べ、改善する関数を 1 つ選択します。pyperf、hyperfine、および tuna with -X importtime を使って、既存の実装と文字列メソッドを比較します。1 つの関数にベンチマークの根拠があり、指定されたタイトル形式の pull request が作成されていれば完了です。

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

評価

技術スタック
python
領域
performance
issue の種類
リファクタリング
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
35/100

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

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