Skip to content

feat: experimental zero-tracking behind mi_option_purge_zeroes (#67) - #79

Merged
zackees merged 2 commits into
v4from
feat/zero-tracking-probe
Aug 1, 2026
Merged

zackees merged 2 commits into
v4from
feat/zero-tracking-probe

Conversation

@zackees

@zackees zackees commented Jul 31, 2026

Copy link
Copy Markdown
Owner

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_zalloc did a redundant memset. 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:

  • only on the needs_recommit branch (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 at all.

Both verified by running the correctness test with MIMALLOC_PURGE_DECOMMITS=0 as well as the default.

The option is appended at the end of the mi_option enum 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 0xA5 before freeing, then requires the next mi_zalloc to 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:

attempt anti-workload result
sequential A then B +13% slower — looked decisive
same binaries, interleaved 6 of 8 faster — sign reversed

The difference was measurement order on a machine running concurrent builds. So this PR ships the harness rather than a conclusion: 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, 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.

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

zackees commented Jul 31, 2026

Copy link
Copy Markdown
Owner Author

Measurement complete — 8 interleaved pairs per runner

Platform sparse (favourable) dense (ANTI-WORKLOAD) distinguishable from noise?
windows -96.4% -10.8% faster Yes — within-arm spreads 3.6% / 3.0-4.2%
ubuntu +0.2% +0.6% No — spreads 1.3-5.3%. No effect.
macos +4.5% +3.8% No — spreads 32-67%. Uninformative.

Correctness passed on all three runners with the option off and on, plus with MIMALLOC_PURGE_DECOMMITS=0 locally to exercise the reset path the guard must reject.

Reading this honestly

The 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 MADV_DONTNEED, which does not set needs_recommit, so the guarded branch never runs. That is the guard working as designed rather than a bug, but it means the feature is Windows-only in practice and the issue text should say so instead of implying a portable win.

macOS is unmeasured, not neutral. Spreads of 32-67% on that runner swamp any effect this size. The +3.8% sign is consistent across both workloads, which is mildly suspicious, but at that noise level it means nothing. Do not ship this on by default until macOS is measured on something quieter — the honest current status is "no evidence either way".

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

Measured win and anti-workload number published. Dropped if it regresses anything.

Win: yes, on Windows, on the anti-workload. Regression: none observed anywhere. So it clears the bar it was set.

@zackees

zackees commented Jul 31, 2026

Copy link
Copy Markdown
Owner Author

Holding this open as a draft, not merging into main.

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:

  1. Bump the upstream pin from 579f8c0e to the dev3 tip #80 — the upstream pin bump, which is the first v4 change and defines the branch
  2. META: adopt community mimalloc improvements into v3, and prove each one works [COMPLETE -- v4 work continues in #94] #61 Open Question 1 — whether v4 is a new branch or becomes main

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.

@zackees
zackees changed the base branch from main to v4 July 31, 2026 23:17
@zackees
zackees marked this pull request as ready for review July 31, 2026 23:17
@zackees
zackees merged commit fba9f8c into v4 Aug 1, 2026
29 checks passed
@zackees
zackees deleted the feat/zero-tracking-probe branch August 1, 2026 00:19
zackees added a commit that referenced this pull request Aug 1, 2026
…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.
zackees added a commit that referenced this pull request Aug 1, 2026
* 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.
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