Repository navigation
feat: experimental zero-tracking behind mi_option_purge_zeroes (#67) - #79
Conversation
After a decommit-purge the range reads back zero when it is recommitted, but the
arena's dirty bits stayed set, so the next allocation was reported non-zero and
mi_zalloc did a redundant memset. This clears the dirty bits on that path.
Off by default. Two guards, both load-bearing, because claiming zero wrongly hands
dirty memory to mi_zalloc -- silent heap corruption, not a visible failure:
* only on the `needs_recommit` branch, i.e. only a genuine decommit. A *reset*
(MADV_FREE / MEM_RESET) may preserve contents.
* only when the arena has no custom commit_fun, which makes no zeroing promise.
The option is appended at the END of the mi_option enum rather than next to the
other purge options, so the numeric values published in 0.9.0 do not shift.
Correctness is tested by filling every byte of every block with a non-zero pattern
before freeing and requiring the next mi_zalloc to return genuinely zeroed memory.
A weaker check that samples a few offsets cannot distinguish "zeroed" from "stale
but happens to be zero" -- my first benchmark made exactly that mistake and its
result was meaningless. Registered twice in ctest, off and on, per #67's rule that
any flag that ships must have a job running the suite with it ON.
No verdict yet, deliberately. Local numbers do not support one: a first sequential
A/B "measured" a 13% regression on the anti-workload, and re-running the same two
binaries interleaved reversed the sign -- it was measurement order on a loaded
machine. The zero-tracking workflow runs the arms interleaved and paired from a
single binary on all three runners, and ci/zero_tracking_report.py prints medians
with the within-arm spread, flagging any effect smaller than the noise instead of
reporting it as a result.
What is already clear locally: the favourable workload is not evidence on its own.
It shows ~-97%, but touching only one byte per block means skipping the memset also
skips faulting the pages in -- that is lazy commit, not zeroing. The anti-workload
is the one that decides adoption.
Refs #67
The A/B step used a trailing-glob find, which on the MSVC runner matched `...exe.recipe` before the executable; bash then tried to run the XML and died with a syntax error. Correctness had already passed on that runner -- only the timing step was broken. Matches the two real names explicitly and echoes the chosen path, so a future mismatch is visible in the log rather than inferred from a parse error.
Measurement complete — 8 interleaved pairs per runner
Correctness passed on all three runners with the option off and on, plus with Reading this honestlyThe win is real on Windows and it is on the anti-workload, which is the number #67 said would decide adoption. -10.8% with within-arm spreads of 3-4% is well outside the noise, and this is a Windows-first repo. It does nothing on Linux. +0.2% / +0.6% is not a small win, it is no effect — almost certainly because Linux purges with macOS is unmeasured, not neutral. Spreads of 32-67% on that runner swamp any effect this size. The The sparse column is not evidence and should not be quoted alone: touching one byte per block means skipping the memset also skips faulting the pages in, so -96% is mostly lazy commit. Against #67's acceptance criteria
Win: yes, on Windows, on the anti-workload. Regression: none observed anywhere. So it clears the bar it was set. |
|
Holding this open as a draft, not merging into Per #61 (rev 4), community improvements now target a v4 line rather than v3. This is a Bun-derived feature, and it is Windows-only in practice, so it is exactly the kind of change that line exists for. Blocked on:
Retarget and merge once the v4 branch exists. Nothing here needs rework: correctness passes on all three runners with the option off and on, and the measurement is done. The one thing still outstanding before it could ever go on by default is a macOS number from something quieter than a shared runner — 32-67% spread there means the current result is no evidence either way. |
…96) rust-native is red on v4. #79 added mi_option_purge_zeroes to include/mimalloc.h and changed src/arena.c and src/options.c, but did not carry any of it into the vendored copies, so xtask check reports drift on all three: mimalloc-pprof-amalgamated.c mimalloc-pprof-amalgamated.h mimalloc.h This is the second time I have made exactly this mistake -- the first broke main after #72. Rule 2 (never mix C-core and rust/ paths in one commit) means a C change ALWAYS needs a Rust-only follow-up, and skipping it is invisible until a downstream job fails. #88 tracks making CI catch it on the PR instead of after the merge; until that lands, this remains a manual step that is easy to forget. Diff is the mi_option_purge_zeroes enum entry, its options-table row, and the guarded dirty-bit clear in the purge path. xtask check now clean.
* ci: fail any PR that leaves the vendored amalgamation stale (#88) The Rust crate compiles rust/mimalloc-pprof/vendor/mimalloc-pprof-amalgamated.c, not the root src/ tree, and rule 2 forbids mixing C-core and rust/ paths in one commit -- so every C change needs a separate Rust-only regeneration commit. Nothing enforced that second half. It has now bitten twice, both times invisible on the PR that caused it because rust-native only triggers on rust/**: #72 -> broke rust-native on main (fixed by #85) #79 -> broke rust-native on v4 (fixed by #96) Both surfaced later, on unrelated PRs, which is the worst place to find them. No paths: filter, deliberately. Filtering on rust/** is precisely the bug, and running on every PR also closes the second gap in #88: a docs-only PR previously ran zero checks (#81), so it could not surface an already-broken main either. Every PR now has at least one required check. * TEMP: deliberate C-only change with no regeneration (positive control for #88) This commit exists to prove the new guard fires. It edits src/profile.c and deliberately does NOT regenerate the vendored amalgamation -- exactly the mistake #72 and #79 made. amalgamation-drift must go red on this commit. Reverted in the next commit. A gate that has never been observed to fail proves nothing, and this repo has now shipped four gates that were silently checking nothing. * Revert "TEMP: deliberate C-only change with no regeneration (positive control for #88)" This reverts commit dd3d71a.
Refs #67. Opened to get the measurement, not to claim a result. See "verdict" below.
After a decommit-purge the range reads back zero on recommit, but the arena's dirty bits stayed set — so the next allocation was reported non-zero and
mi_zallocdid a redundantmemset. This clears the dirty bits on that path, off by default.Two guards, both load-bearing
Claiming zero wrongly hands dirty memory to
mi_zalloc— silent heap corruption, not a visible failure. So:needs_recommitbranch (a genuine decommit). A reset (MADV_FREE/MEM_RESET) may preserve contents.commit_fun, which makes no zeroing promise at all.Both verified by running the correctness test with
MIMALLOC_PURGE_DECOMMITS=0as well as the default.The option is appended at the end of the
mi_optionenum rather than beside the other purge options, so the numeric values published in 0.9.0 don't shift.Correctness
Fills every byte of every block with
0xA5before freeing, then requires the nextmi_zallocto return genuinely zeroed memory across purge/reuse cycles.This matters: my first benchmark checked 3 bytes per block and never dirtied the middle, so it could not distinguish "zeroed" from "stale but happens to be zero" — it would have passed a corrupting implementation. Registered twice in ctest, off and on, per #67's rule that any flag that ships must have a job running the suite with it ON.
No verdict yet — deliberately
Local numbers do not support one, and I nearly published a wrong one:
The difference was measurement order on a machine running concurrent builds. So this PR ships the harness rather than a conclusion: the
zero-trackingworkflow runs the arms interleaved and paired from a single binary on all three runners, andci/zero_tracking_report.pyprints medians with the within-arm spread, explicitly flagging any effect smaller than the noise.One thing already clear: the favourable workload is not evidence on its own. It shows ~−97%, but touching only one byte per block means skipping the memset also skips faulting the pages in. That is lazy commit, not zeroing. The anti-workload decides adoption.
Merge criteria
Per #67, this merges only on a measured win with the anti-workload number published, and is dropped if it regresses anything. If CI shows neutral, the right outcome is to close this unmerged — a neutral feature that ships lives forever untested.