python / python/cpython

Improve speed of stdlib functions by replacing `re` uses

Open
#130,167 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

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

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

Read the pull-request lifecycle guide first, then inspect standard-library modules that import re and select one function to improve. Compare the existing implementation with string methods using pyperf, hyperfine, and tuna with -X importtime. Done means one function has benchmark evidence and a pull request using the requested title format.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.