Skip to content

Run Callgrind children from a fixed staging layout - #1623

Merged
asvetlov merged 2 commits into
aio-libs:masterfrom
asvetlov:callgrind-stage-fixed-layout
Sep 28, 2026
Merged

asvetlov merged 2 commits into
aio-libs:masterfrom
asvetlov:callgrind-stage-fixed-layout

Conversation

@asvetlov

Copy link
Copy Markdown
Member

What do these changes do?

benchmarks/callgrind_driver.py gave 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 an istr lookup (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 on getitem_istr from two trees whose paths differ by one character. That is what made the #1619 measurement look like a +127 Ir ctor_items regression.

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 a link to each module they actually import (for multidict, 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 under metadata.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) and MIMALLOC_PURGE_DELAY=-1 pin 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 in docs/benchmark.rst and AGENTS.md.

Are there changes in behavior for the user?

No; this only touches the benchmark tooling. Contributors now need pytest-codspeed installed (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 --probe reports what to stage, so nothing needs updating when operations.py gains imports.

Related issue number

Related to #1619

Checklist

  • I think the code is well written
  • Unit tests for the changes exist: N/A, benchmark tooling; verified with the base-to-base controls below
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt: N/A
  • Add a new news fragment into the CHANGES/ folder
  • make doc-spelling passes and any new technical words are added to docs/spelling_wordlist.txt

Drafted 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) and a-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):

row a ab (+1 char) long (+50 chars)
getitem_istr 700.5 721.5 698.5
setitem_istr 882.7 884.7 881.7
copy 25474.5 25458.5 25452.0
update 37024.0 37000.0 37000.0
ctor_items 44700.5 44700.5 44711.0

Old driver, FT, three runs of the same tree: update 46363 / 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_extend unlinking 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=2GiB and MIMALLOC_PURGE_DELAY=-1, four concurrent runs matched exactly.

After (final driver, --include-multidict-only, all five implementations, comparing raw Callgrind points):

  • GIL, a vs ab vs long: 0 of 124 cells differ.
  • FT, a vs a (second run) vs long: 0 of 124 cells differ.
  • Earlier revision: still 0 of 124 after adding a junk directory to one tree's root and a fake .so to its multidict/.

Other checks:

  • --whole-process refusal: a venv without pytest-codspeed exits 1 with the message; with --whole-process it writes JSON with bracketed: false.
  • python benchmarks/callgrind_driver.py --self-check: passed, 124 cells.
  • Tests: C extension 2851 passed, 172 skipped; pure Python (MULTIDICT_NO_EXTENSIONS=1, --no-c-extensions) 1336 passed, 20 skipped.
  • pre-commit (PYENV_VERSION=3.13.2) and make doc-spelling: pass.
  • Pre-existing and out of scope: the driver cannot run on a pure-Python-only install, since self_check() loads every implementation. The old driver fails the same way.

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.
@codspeed

codspeed Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 706 untouched benchmarks


Comparing asvetlov:callgrind-stage-fixed-layout (ac56cc1) with master (9802f1b)

Open in CodSpeed

@codecov

codecov Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (73e853a) to head (ac56cc1).
⚠️ Report is 5 commits behind head on master.

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     
Flag Coverage Δ
CI-GHA 100.00% <ø> (ø)
pytest 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@asvetlov
asvetlov marked this pull request as ready for review September 28, 2026 09:42
@asvetlov
asvetlov requested a review from webknjaz as a code owner September 28, 2026 09:42
@greptile-apps

greptile-apps Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Refactors benchmark infrastructure to use staging directories.

No outstanding findings block merging.

Reviews (2) · Last reviewed commit: "Resolve the Valgrind path before running..."

Comment thread benchmarks/callgrind_driver.py Resolved
@greptile-apps

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.
@asvetlov
asvetlov merged commit b27c7da into aio-libs:master Sep 28, 2026
116 of 119 checks passed
@asvetlov
asvetlov deleted the callgrind-stage-fixed-layout branch September 28, 2026 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant