Repository navigation
Run Callgrind children from a fixed staging layout - #1623
Merged
Merged
Conversation
Two checkouts of one commit at paths of different lengths measured up to 3% apart on getitem_istr, because every path the interpreter allocates at startup, and the listing of every directory it imports from, shifts the heap the measured loop runs on. The children now run under -S from a fixed-length temporary directory holding a link to the base interpreter, a copy of their own scripts and one import directory with only the modules they import, with a private bytecode cache and a reduced environment. On the free-threaded build mimalloc also placed its arena at a random address and purged on a timer, which moved counts between two runs of one tree; both are pinned through MIMALLOC_* options. The driver also refuses to collect whole-process counts, taken when pytest-codspeed is missing, unless given --whole-process.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1623 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 44 44
Lines 8205 8381 +176
Branches 697 706 +9
==========================================
+ Hits 8205 8381 +176
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
asvetlov
marked this pull request as ready for review
September 28, 2026 09:42
Contributor
|
This comment has been minimized.
This comment has been minimized.
The children run from the stage directory without the caller's PATH, so a relative --valgrind such as ./tools/valgrind was accepted by the precheck and then not found at launch.
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.
What do these changes do?
benchmarks/callgrind_driver.pygave different instruction counts for identical code depending on where the checkout lived. Every path the interpreter allocates at startup, and the listing of every directory it imports from, shifts the heap the measured loop runs on. An allocating operation such as anistrlookup (it copies its key) then takes pymalloc's pool-full/pool-used transition on every iteration in one tree and never in the other: identical code measured 700.5 vs 721.5 Ir ongetitem_istrfrom two trees whose paths differ by one character. That is what made the #1619 measurement look like a +127 Irctor_itemsregression.The children now run under
-Sfrom a fixed-length temporary directory holding a link to the base interpreter, a copy of their own scripts, and one import directory with a link to each module they actually import (formultidict, only its imported files, so stray build products do not count either). They also get a private bytecode cache and a reduced environment. The result records what was staged undermetadata.stage.While proving that, the free-threaded build turned out to be nondeterministic between two runs of one tree. mimalloc seeds its arena address from
getrandom()and purges on a timer.MIMALLOC_ARENA_RESERVE=2GiB(a reservation mimalloc leaves the kernel to place) andMIMALLOC_PURGE_DELAY=-1pin both.The driver also refuses to run without the Valgrind client requests (no
pytest-codspeed), whose whole-process counts cannot be compared with bracketed ones, unless given--whole-process. Both traps are documented indocs/benchmark.rstand AGENTS.md.Are there changes in behavior for the user?
No; this only touches the benchmark tooling. Contributors now need
pytest-codspeedinstalled (or--whole-process) to run the Callgrind driver. Absolute counts shift once, since the children's heap changed.Is it a substantial burden for the maintainers to support this?
No. The staging is self-contained in the driver, and the child's
--probereports what to stage, so nothing needs updating whenoperations.pygains imports.Related issue number
Related to #1619
Checklist
CONTRIBUTORS.txt: N/ACHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtDrafted with Claude Code (Claude Opus 5.5); reviewed by .
Agent run details (optional, for reviewers)
All controls measure one commit (73e853a) from several checkouts on CPython 3.14.7 and 3.14.7t with Valgrind 3.26.0 and pytest-codspeed 5.0.3:
a/tree,ab/tree(one character longer) anda-considerably-longer-directory-name-to-shift-paths/tree, each with its own venv.Before (old driver, GIL,
--include-multidict-only --impl multidict_c, Ir/op):Old driver, FT, three runs of the same tree:
update46363 / 46371 / 46491.Root cause (callgrind per-line diff, a vs ab, getitem_istr): +8000 Ir in
_PyObject_Free(insert_to_usedpool), +5600 in_PyObject_Malloc(pymalloc_pool_extendunlinking a full pool) and a different branch in__memcmp_avx2_movbe, over 800 ops.Intermediate: path staging alone made FT worse, with getitem_istr at 805.8 to 979.0 across runs of one tree. A syscall trace showed mimalloc's 1 GiB arena at 0x2ab30000000 in one run and 0x49502000000 in another. With
MIMALLOC_ARENA_RESERVE=2GiBandMIMALLOC_PURGE_DELAY=-1, four concurrent runs matched exactly.After (final driver,
--include-multidict-only, all five implementations, comparing raw Callgrind points):.soto itsmultidict/.Other checks:
--whole-processrefusal: a venv without pytest-codspeed exits 1 with the message; with--whole-processit writes JSON withbracketed: false.python benchmarks/callgrind_driver.py --self-check: passed, 124 cells.2851 passed, 172 skipped; pure Python (MULTIDICT_NO_EXTENSIONS=1,--no-c-extensions)1336 passed, 20 skipped.pre-commit(PYENV_VERSION=3.13.2) andmake doc-spelling: pass.self_check()loads every implementation. The old driver fails the same way.