Skip to content

fix(bench): build the same instruction-count binary in every checkout and host - #9574

Draft
kixelated wants to merge 3 commits into
mainfrom
quest/m0/repeatable-instruction-counts
Draft

kixelated wants to merge 3 commits into
mainfrom
quest/m0/repeatable-instruction-counts

Conversation

@kixelated

@kixelated kixelated commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

The same code read different instruction counts from one run to the next, at different host loads, and in different checkouts and hosts. Noisy series feed Kixel Quest's regression filing and bisects (quest).

Findings

Diffing Callgrind profiles of runs that disagree: nearly every disagreement is the same code path hitting a different glibc malloc state (_int_malloc against a tcache hit). So any work before the measured iteration that varies will move the count. Three sources:

  • Random seeds. std's RandomState takes fresh getrandom bytes per process, which changes HashMap iteration and free order. A preloaded getrandom returning a fixed-seed stream steadied every stats, retirement, and announce count. Full-load agreement rose from 527 to 900 of 1,284. A constant stream is not enough: it hangs QUIC connection-ID generation.
  • The host clock. Every remaining mover reads it, through tokio's real clock or kio's std fallback outside a runtime. Adding a preloaded clock that advances 1µs per read made all 1,284 counts agree exactly across two runs at 32 jobs on a host already at load 32, auth included. Compared with today's counts, 53 shift by more than 1% (the worst is auth/waiting at -26%).
  • Location. nix develop linked an RPATH to <checkout>/outputs/out/lib into every binary, and $CARGO_HOME registry paths land in panic locations. That meant the same code built a different binary per checkout and per host. At run time, rust2-bench also passes the host's whole environment, and its size moves the stack (up to 12 instructions on jitter_handoff::4[64]).

Approach

This PR fixes the build-side location dependence, which moq owns:

  • The dev shell sets NIX_NO_SELF_RPATH, which drops the stdenv RPATH to a directory that never exists.
  • sh/rs/instructions.sh remaps $CARGO_HOME to /cargo. It fails loud if CARGO_ENCODED_RUSTFLAGS would silently drop the remap.

The quest is rewritten to what remains: deterministic entropy and clock plus a canonical job environment in rust2-bench, then a rust2 bump and the two-checkout proof here.

Impact

  • Public API: none. Wire: none.
  • Bench builds in the dev shell no longer carry the checkout RPATH. Instruction-count binaries no longer contain $CARGO_HOME paths. Counts may step once at the next nightly, since the binaries' data layout changes.

Validation

  • Two checkouts at different paths, the second with CARGO_HOME at another path, built byte-identical jitter, varint, and both codec bench binaries (cmp), with no /home paths (strings).
  • Before this change, the only checkout-specific string in a bench binary was that RPATH, and rust2-bench compare HEAD read moq-json codec full[big_static] 0.33% apart across worktrees.
  • shellcheck, shfmt -d, and nixfmt --check pass on the changed files. I did not run just ci or a full nightly-equivalent count on this head.

Alternatives

  • Fixing each bench's clock (paused tokio, or kio's simulated clock) cannot reach std's RandomState inside library code, so seeds would need rust2-bench anyway. It also leaves every future bench to get this right on its own.
  • Dropping the noisy targets from the selection: the maintainer rejected this on moq-dev/moq#9555.

Decisions

  • Decided 2026-11-24 (maintainer, /quest-run): ✅ the fixed-seed and fixed-clock fix lives in rust2-bench, deterministic randomness and clock for every counted job, both in the fingerprint so Kixel Quest starts new series, binaries that bypass it refused (rejected: a paused clock in each moq bench). Decision comment. kixel-ai/rust2's quest/m0/bench-determinism.md, planned in kixel-ai/rust2#1016, owns it together with the canonical job environment.
  • The quest's completion check now matches rust2's: the two-checkout count is one idle run and one loaded run (taken by the iterating agent while the maintainer was away, adopting rust2's criterion so the two quests agree).

Waiting

  • kixel-ai/rust2: ship quest/m0/bench-determinism.md (deterministic randomness and clock for counted jobs, launched with a canonical environment, cwd, and fixed-length workspace-root path), claimed by kixel-ai/rust2#1023, a draft holding only its claim as of 2026-11-25. Check: that PR merged. Then bump rust2 here and count every benchmark twice in two checkouts, idle and loaded.

Follow-ups

Models

Size: M
Implementer: Claude Code, claude-opus-5-5, unknown
Implementer: Claude Code, claude-opus-5-5, unknown (iterate, S)

(Written by Claude Opus 5.5)

kixelated and others added 2 commits November 24, 2026 18:24
… and host

The dev shell no longer links an RPATH to <checkout>/outputs/out/lib, and
instructions.sh remaps CARGO_HOME, so bench binaries are byte-identical across
checkout paths and hosts. Records what still moves counts (random seeds, the
host clock, the job environment) and the rust2-bench fix it needs in the quest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title Every counted bench repeats its instruction count fix(bench): build the same instruction-count binary in every checkout and host Nov 25, 2026
@kixelated

Copy link
Copy Markdown
Collaborator Author

Maintainer decision (/quest-run pick, 2026-11-24):

  • ✅ The fixed-seed and fixed-clock fix lives in rust2-bench: every counted job gets deterministic randomness and a deterministic clock, both enter the fingerprint so Kixel Quest starts new series, and binaries that bypass it are refused (rejected: a paused clock in each moq bench, which can't reach library HashMap seeds).

(Written by Claude Opus 5.5)

…bench-determinism

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated

Copy link
Copy Markdown
Collaborator Author

Applied the maintainer decision in c20f90d: the quest records it, and the Maintainer wait is gone since kixel-ai/rust2's quest/m0/bench-determinism.md (kixel-ai/rust2#1016) owns the fix. This PR stays a draft on the remaining kixel-ai/rust2 wait: kixel-ai/rust2#1023 has claimed that quest but holds no work yet. Once it ships, bump rust2 here and run the two-checkout count.

(Written by Claude Opus 5.5)

This branch has not been deployed

No deployments
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