python@3.12, python@3.13, python@3.14: avoid importing re in sitecustomize - #301536
Open
namaserajesh wants to merge 3 commits into
Open
python@3.12, python@3.13, python@3.14: avoid importing re in sitecustomize#301536namaserajesh wants to merge 3 commits into
namaserajesh wants to merge 3 commits into
Conversation
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.
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. |
Open
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.pyis generated by these formulae and runs on every interpreter start. It does a module-levelimport resolely to compile two path-rewriting regexes.Measured (M3, macOS 26.6.2,
python@3.143.14.7, 300 iterations, median of 5 batches):python3 -c passsitecumulative (-X importtime)rewas 2,672 µs ofsitecustomize'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:
The
sitecustomizemethod is byte-identical acrosspython@3.12,@3.13and@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.HOMEBREW_NO_INSTALL_FROM_API=1 brew install --build-from-source <formula>?brew test <formula>?brew audit --strict <formula>(after doingHOMEBREW_NO_INSTALL_FROM_API=1 brew install --build-from-source <formula>)? If this is a new formula, does it passbrew 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/coretapped locally (brew audit --strict python@3.14returns "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 lgtm—brew typecheckclean.Syntax OK. (Note for anyone reproducing: macOS system Ruby is 2.6 and rejects the formulae's existing endless-method definitions, so it fails onmaintoo — not a signal.)3.14.7,3.14.10_1,3.14.0rc1,3.14.0a1,3.14.0b2, bare3.14,3, empty, invalid likexyz/3.14.7x), bothFrameworks/…and plainlib/…layouts, matching and non-matching tails, prefixed, repeated and doubled paths, plus 4,000 randomized fuzz strings.<<~indentation stripping), installed the result assitecustomize.pyviaPYTHONPATH, and compared interpreter state against stock:sys.path,sys.prefix,sys.base_prefix,sys.exec_prefix,sys.base_exec_prefixandsys.executableall 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.
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-byor 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.