Skip to content

python@3.12, python@3.13, python@3.14: avoid importing re in sitecustomize - #301536

Open
namaserajesh wants to merge 3 commits into
Homebrew:mainfrom
namaserajesh:sitecustomize-no-re
Open

python@3.12, python@3.13, python@3.14: avoid importing re in sitecustomize#301536
namaserajesh wants to merge 3 commits into
Homebrew:mainfrom
namaserajesh:sitecustomize-no-re

Conversation

@namaserajesh

@namaserajesh namaserajesh commented Aug 31, 2026

Copy link
Copy Markdown

Closes #301528. Opened at @SMillerDev's request there: "Please submit a PR for this instead, it'll make the fix and potential side effects much easier to evaluate."

sitecustomize.py is generated by these formulae and runs on every interpreter start. It does a module-level import re solely to compile two path-rewriting regexes.

Measured (M3, macOS 26.6.2, python@3.14 3.14.7, 300 iterations, median of 5 batches):

before after
python3 -c pass 20.26 ms 16.29 ms
saving 3.98 ms (19.6%)
site cumulative (-X importtime) 5,165 µs 2,208 µs

re was 2,672 µs of sitecustomize's 3,156 µs — ~85% of its cost.

The replacement is strictly equivalent to the regexes, including mid-string and repeated matches — not merely equivalent for realistic paths. An earlier prefix-only sketch was not, which is why I did not attach a patch to the issue:

input:  /some/prefix/opt/homebrew/Cellar/python@3.14/3.14.7/lib/python3.14/site-packages
regex:  /some/prefix/opt/homebrew/lib/python3.14/site-packages
naive:  (unchanged)   <- wrong
this:   /some/prefix/opt/homebrew/lib/python3.14/site-packages   <- correct

The sitecustomize method is byte-identical across python@3.12, @3.13 and @3.14 (verified by hashing it with the version normalised out), so one commit per formula.

I have not bumped revision — the change only takes effect on rebuild, and bumping it here would rebuild an enormous dependent tree for a ~4 ms startup win. Happy to add bumps if you would rather it land immediately.


  • Have you followed the guidelines for contributing?
  • Have you ensured that your commits follow the commit style guide?
  • Have you checked that there aren't other open pull requests for the same formula update/change?
  • Have you built your formula locally with HOMEBREW_NO_INSTALL_FROM_API=1 brew install --build-from-source <formula>?
  • Is your test running fine brew test <formula>?
  • Does your build pass brew audit --strict <formula> (after doing HOMEBREW_NO_INSTALL_FROM_API=1 brew install --build-from-source <formula>)? If this is a new formula, does it pass brew audit --new <formula>?

The last three are unticked deliberately — I have not run them, and the template says not to tick what I have not done. I do not have homebrew/core tapped locally (brew audit --strict python@3.14 returns "These formulae are not in any locally installed taps"), and a three-formula CPython source build is not something this machine can complete reliably. Rather than tick them, here is what I did verify:

  • brew lgtmbrew typecheck clean.
  • Ruby syntax on all three patched formulae with Homebrew's own portable Ruby 4.0.6 — Syntax OK. (Note for anyone reproducing: macOS system Ruby is 2.6 and rejects the formulae's existing endless-method definitions, so it fails on main too — not a signal.)
  • Equivalence, 4,475 inputs, zero mismatches against the original regexes — generated version shapes (3.14.7, 3.14.10_1, 3.14.0rc1, 3.14.0a1, 3.14.0b2, bare 3.14, 3, empty, invalid like xyz / 3.14.7x), both Frameworks/… and plain lib/… layouts, matching and non-matching tails, prefixed, repeated and doubled paths, plus 4,000 randomized fuzz strings.
  • Behavioural, on a real install — rendered the heredoc (accounting for <<~ indentation stripping), installed the result as sitecustomize.py via PYTHONPATH, and compared interpreter state against stock: sys.path, sys.prefix, sys.base_prefix, sys.exec_prefix, sys.base_exec_prefix and sys.executable all identical, both normally and inside a venv.

If the build/test/audit trio is a hard requirement before review, say so and I will find a way to run them or close this.


  • I did not use AI/LLM to create this PR, or I disclosed the tool/model below and reviewed its output; I did not attribute commits to AI and will answer maintainer questions and review comments myself without AI/LLM.

Disclosure: drafted with Claude Code (Anthropic, Claude Opus 5). Per CONTRIBUTING.md and Responsible AI Usage: the three commits carry no AI trailer (no Co-authored-by, Assisted-by or similar — authored solely by me), every measurement above was run on my machine and is reproducible from the commands shown, I have reviewed the diff and the test methodology myself, and I will answer maintainer questions and review comments myself without AI/LLM. This is my only AI-assisted PR open here.

My apologies for the first version of this PR missing the template — that was my mistake, not the tooling's.

sitecustomize.py runs on every interpreter start and imported `re` solely to
compile two path-rewriting regexes. Measured on an M3 Mac with python@3.14
3.14.7: `python3 -c pass` 20.26ms -> 16.29ms, a 3.98ms (19.6%) saving, and
`site` cumulative in -X importtime drops 5165us -> 2208us.

Replaced with a small scanner that is strictly equivalent to the regexes,
including mid-string and repeated matches. Verified against the originals over
4475 inputs (generated version shapes 3.14.7 / 3.14.10_1 / 3.14.0rc1 /
3.14.0a1 / 3.14.0b2, both Frameworks and plain lib layouts, prefixed, repeated
and malformed paths, plus 4000 randomized) with zero mismatches. The rendered
file produces byte-identical sys.path, sys.prefix, sys.base_prefix,
sys.exec_prefix, sys.base_exec_prefix and sys.executable, both normally and
inside a venv.
sitecustomize.py runs on every interpreter start and imported `re` solely to
compile two path-rewriting regexes. Measured on an M3 Mac with python@3.14
3.14.7: `python3 -c pass` 20.26ms -> 16.29ms, a 3.98ms (19.6%) saving, and
`site` cumulative in -X importtime drops 5165us -> 2208us.

Replaced with a small scanner that is strictly equivalent to the regexes,
including mid-string and repeated matches. Verified against the originals over
4475 inputs (generated version shapes 3.14.7 / 3.14.10_1 / 3.14.0rc1 /
3.14.0a1 / 3.14.0b2, both Frameworks and plain lib layouts, prefixed, repeated
and malformed paths, plus 4000 randomized) with zero mismatches. The rendered
file produces byte-identical sys.path, sys.prefix, sys.base_prefix,
sys.exec_prefix, sys.base_exec_prefix and sys.executable, both normally and
inside a venv.
sitecustomize.py runs on every interpreter start and imported `re` solely to
compile two path-rewriting regexes. Measured on an M3 Mac with python@3.14
3.14.7: `python3 -c pass` 20.26ms -> 16.29ms, a 3.98ms (19.6%) saving, and
`site` cumulative in -X importtime drops 5165us -> 2208us.

Replaced with a small scanner that is strictly equivalent to the regexes,
including mid-string and repeated matches. Verified against the originals over
4475 inputs (generated version shapes 3.14.7 / 3.14.10_1 / 3.14.0rc1 /
3.14.0a1 / 3.14.0b2, both Frameworks and plain lib layouts, prefixed, repeated
and malformed paths, plus 4000 randomized) with zero mismatches. The rendered
file produces byte-identical sys.path, sys.prefix, sys.base_prefix,
sys.exec_prefix, sys.base_exec_prefix and sys.executable, both normally and
inside a venv.
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your pull request. This has been closed because it appears to be missing the pull request template, perhaps because this was written by an AI not a human. We require humans to read and fill in these templates.

Please edit this pull request to fill in the current pull request template. This workflow will reopen this pull request automatically once the template is complete. Do not open a new pull request for this.

@github-actions github-actions Bot closed this Aug 31, 2026
@github-actions github-actions Bot added formula deprecated Formula deprecated formula disabled Formula disabled CI-no-fail-fast-deps Continue dependent tests despite failing GitHub Actions matrix tests. long dependent tests Set a long timeout for dependent testing labels Aug 31, 2026
@github-actions github-actions Bot reopened this Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI-no-fail-fast-deps Continue dependent tests despite failing GitHub Actions matrix tests. formula deprecated Formula deprecated formula disabled Formula disabled long dependent tests Set a long timeout for dependent testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

python@3.14: sitecustomize.py imports re at startup, costing ~12% of interpreter start time

1 participant