Skip to content

Size the copy cell's footprint, not one of its two buffers - #10

Merged
PhillThomas merged 2 commits into
mainfrom
fix/copy-footprint-pair
Nov 28, 2026
Merged

PhillThomas merged 2 commits into
mainfrom
fix/copy-footprint-pair

Conversation

@PhillThomas

Copy link
Copy Markdown
Owner

The copy cell was twice the cache it claimed to measure

What was wrong

A copy cell walks two buffers — a source and a destination. Every level planner sized
one buffer at the level's per-thread window (0.5 × L1d, 0.5 × L2,
0.75 × L3 ÷ cores in the L3 domain), so a copy cell's footprint was twice the window
it was planned for: 100% of L1d, 100% of L2, and 150% of L3.

A streaming footprint 1.5 × a cache does not measure a smaller version of that cache. It
measures DRAM, because with near-LRU replacement each line is evicted just before it is
reused — overshoot is a cliff, not a slope. The grid printed the result under
L3 Cache.

The plan machinery knew the pair existed: total_bytes multiplied by
buffers_per_thread for progress, and the buffer note printed "× 2 buffers". But
oversized_note was handed the size of a single buffer, so the one warning that should
have fired never could. plan_for's comment said "the plan already accounts for that".
It did not.

The evidence

1. The tool's own metadata, on a Supermicro X9 box (E5-2680 v2, 10 cores, 25 MiB
L3, one NUMA node, SSE2 path) — the first machine with a small enough L3 per core to
notice:

l3   read    threads 10  buf/thread 1966080 x1 = 18.8 MiB = 0.75x L3    213.9 GB/s
l3   write   threads 10  buf/thread 1966080 x1 = 18.8 MiB = 0.75x L3    151.2 GB/s
l3   copy    threads 10  buf/thread 1966080 x2 = 37.5 MiB = 1.50x L3     13.8 GB/s

13.8 GB/s against a memory copy of 11.2 GB/s on the same run.

2. A falsifiable prediction. The pair's aggregate is 2 × 0.75 × L3 whatever the
thread count, because the per-thread share is 0.75 × L3 ÷ threads. Capacity says the
cell is flat in threads; a kernel defect or a threading problem says it is not. Measured
on that box:

threads copy write
1 12.3 GB/s 19.1 GB/s
2 12.3 GB/s 144.6 GB/s
5 12.3 GB/s 210.2 GB/s

Flat against a neighbour that scaled 11×.

3. The README's own machine (Ryzen 5 PRO 5650G, 16 MiB L3) says the same: the copy
cell's footprint is 1.50 × L3 and it printed 75.0 GB/s beside a read of 353.9 GB/s. That
74.7 figure was in the README's headline grid.

4. The traffic arithmetic closes. Copy moves about three bytes of DRAM traffic per
counted byte (read source, fetch destination for ownership, write back): memory copy
11.2 × 3 = 33.6 GB/s, against a measured 33.8 GB/s for non-temporal write. The "L3" copy
cell's 13.8 × 3 = 41 GB/s sits at the 39.1 GB/s DRAM read ceiling. It was not
partly cache-resident; it was not an L3 cell.

The second bug, found in the same function

The L3 thread-shrink loop stopped at footprint ≥ 1 × L2 while the floor enforced
2 × L2. The loop therefore kept a thread count whose share was then lifted by the
floor, silently, above the window the level was sized for. On a fixture with 8 cores, a
16 MiB L3 and 2 MiB L2s it settled on 6 threads × 4 MiB = 24 MiB through a 16 MiB
cache
for a plain read cell. The loop now argues about the same quantity it enforces,
and takes 3 threads × 4 MiB = 12 MiB = the window, with no note required. Where a part
is so small that no footprint both fits the window and clears the floor, the plan says
which of the two gave way instead of printing a number that quietly means something
else.

The fix

Size the footprint — buffers × bytes — and divide into per-buffer sizes on the way
out. l1, l2 and l3 now take buffers; for_level_buffers is the entry point and
for_level is the one-buffer case. Halving after the planner would have left the floor
and the thread loop where they were, which is the wrong place: the cache sees the pair,
so the floor (2 × L2) is a footprint floor — two buffers of one L2 each still both fit
in L2.

The memory row keeps two full-size buffers, deliberately: it makes no claim to fit
anywhere, it is sized to clear the whole L3 four times over, and halving it would buy
nothing but a smaller resident set. Its derivation says the copy cell holds two.

The note that says part of this cell is memory traffic said too little. A cell whose
footprint overshoots is memory-bound; that is what it says now.

A copy plan whose footprint fits gets no note, because there is nothing to apologise
for. --dry-run gained a copy line per row, so the plan can be read before it is paid
for:

  L3         6 thread(s),         2M per thread  0.75 × 16M L3 ÷ 6 core(s) in L3 domain 0 = 2M per thread (floor 2 × 512K L2 = 1M)
           copy 2 × 1M per thread (the pair holds the window a single buffer gets elsewhere)

What it does to the numbers

Pre-fix binary against this one, three interleaved pairings (arms alternating, not
grouped), same host, --trials 5:

cell pre-fix fixed change
L1 read 1405.7 1408.4 +0.2%
L1 write 822.7 821.5 −0.1%
L1 copy 803.9 813.6 +1.2%
L2 read 761.4 780.0 +2.4%
L2 write 700.0 688.0 −1.7%
L2 copy 259.0 381.0 +47.1%
L3 read 373.4 360.7 −3.4%
L3 write 346.0 330.9 −4.4%
L3 copy 77.5 174.4 +125.0%
memory read/write/copy — — ≤ 0.4%

The read and write cells' plans did not change, so their −4%…+2% spread is this box's
noise floor — which makes the copy jumps 30 to 100 times the drift they are sitting in.
The L1 pair filled L1d completely and cost 1%; the L2 pair cost 47%; the L3 pair cost
125%. The damage scales with how much the level is shared, which is what a capacity
effect should do.

Why the gate did not catch this

reference/ is an independent C implementation with no shared code, and its gate is
10% per cell. It could not catch this because both sides sized a copy cell one buffer
at a time
: a gate between two implementations that share a rule cannot test the rule.
L3 copy sat inside the gate on this host for three pairings running (+6.5%, −1.8%,
+4.2%) while both programs measured DRAM. What caught it was a machine nobody had run
the tool on.

The reference now applies the same rule (it already multiplied by two in its notes,
just not in its sizing). Re-pairing on this host afterwards: L1 copy −1.6 to −7.3%, L2
copy −11 to −14.4%, L3 copy −4.2 to −5.6%, and cells over the gate 3 of 16 in each of
three pairings against 6 in a pre-fix pairing taken for this change. The two programs
agreeing better is how a shared mistake becomes visible afterwards; it is no way to find
one.

Notes for review

  • Result schema stays at 2. No field changed, so old documents still compare; the
    buffer_bytes_per_thread of a copy cell is now half what it was, and compare will
    show a large delta against pre-fix JSONs. That is a real measurement difference, and
    the derivation string says why.
  • The latency cell is single-buffer and is not halved — it walks one chain, so it gets
    the level's whole window. There is a test for exactly that, because the first version
    of this patch halved it and the test caught it.
  • PLAN.md said "Per-thread buffer" in its sizing table, which is where the bug was
    authorised. It now says "working set" and states the pair rule.
  • The thread count for a copy cell is the same as for read and write in practice (the
    window and the floor are both footprint quantities), so no row in the grid silently
    changes thread count under the copy column.
  • Follow-up, deliberately not here: source and destination are both page-aligned in
    both implementations, so every load/store pair shares bits 11:0 and stalls on Intel's
    4K-aliasing rule. L1 copy is where that would show, and it would be a second,
    separate effect on the same cell. It needs its own before/after, not a ride-along.
  • Provenance: the diagnosis was checked by an Anthropic model review (zero-tool, brief
    inline) before patching. It confirmed the capacity reading, proposed sizing the
    footprint inside the planner rather than halving afterwards — which is what landed —
    and found the loop/floor mismatch above. Its suggestion to "re-run at 6 threads" was
    wrong: the aggregate is thread-invariant, which is the basis of test 2 instead.

A copy cell walks a source and a destination, so its footprint is two buffers.
Every level planner sized ONE buffer at the level's per-thread window, which put
twice the working set through the cache the row is named after: 100% of L1d, 100%
of L2, 150% of L3. A streaming footprint 1.5x a cache measures DRAM, because each
line is evicted just before it is reused, and the grid printed it under 'L3 Cache'
(13.8 GB/s against 213.9 read on a 25 MiB part; 74.7 GB/s in this README's own
grid against 329.9 read).

The cell's own note printed 'x 2 buffers, 37.5M in total' beside '1.50x L3' and
oversized_note still did not fire, because it was handed one buffer's size.

Also fixes the L3 thread loop arguing 'footprint >= 1 x L2' while the floor
enforced 2 x L2, which let the floor lift the aggregate above the window in
silence; it now sheds to the floor it enforces, and says which gave way when both
cannot be satisfied.

l1/l2/l3 take the buffer count and size the footprint; for_level_buffers is the
entry point. The memory row keeps two full-size buffers on purpose (no residency
claim) and says so. PLAN.md's 'per-thread buffer' column, which is where the bug
was authorised, now says 'working set'. The C reference had the same conflation
(it doubles in its notes but not in its sizing) and now applies the same rule, so
the gate compares like with like: L1 copy moved from -54.5% to within a few
percent of the reference, L3 copy from +10.3% to -5.6%, and cells over the gate
from 6 to 3 of 16. --dry-run prints the copy split.

Measured on this host, three interleaved pairings old against new: L1 copy +1.2%,
L2 copy +47.1%, L3 copy +125.0%; every read and write cell within its noise,
because their plans did not change.
The CI failure on the previous commit was this script, not the change: it
exited 2 with 'no assembly produced'. Investigating that turned out to be
worse than a broken CI step.

`--emit=asm` is a rustc flag, so cargo's freshness check does not know about
it. When the crate is already built -- a second run in the same tree, or a
restored CI rust-cache, which is every CI run -- cargo prints 'Finished in
0.03s', rustc never runs, and no assembly is written. The script looked in
$OUT/release/deps, which a current toolchain does not write to at all: rustc
puts the file under $OUT/build/.../out now. So the only files that path could
ever match were leftovers, and a run with leftovers passed while scanning a
file this run did not produce. A run without them failed, loudly, for a reason
that had nothing to do with the commit.

That is a gate which approves by reading history. It has been doing it while I
was making changes to kernels in earlier rounds.

Fix: touch the crate so rustc really runs, search the tree cargo writes, and
accept only a file newer than this build, so 'no assembly' can never again
mean 'an old one will do'. Verified from a clean tree and warm, twice in a
row each time; 23 kernel labels, 0 copies outside our asm.
@PhillThomas

Copy link
Copy Markdown
Owner Author

Second commit: the asm gate was approving on history

The CI failure on the first commit here was not this change. It was
scripts/check-kernels-asm.sh exiting 2 with "no assembly produced" — and behind
that was a check that had been approving work I shipped in earlier rounds.

--emit=asm is a rustc flag, so cargo's freshness check does not know about it. When
the crate is already built — which is every CI run, because the cache is restored —
cargo prints "Finished in 0.03s", rustc never runs, and no assembly appears. The script
then looked in $OUT/release/deps, which a current toolchain does not write to at all:
rustc puts the file under $OUT/build/.../out now. The only files that path could match
were leftovers. So a run with leftovers passed while scanning a file that run did not
produce
, and a run without them failed loudly for a reason unrelated to its own
commit. Locally it had been passing on leftovers; CI had nothing left to read.

It now touches the crate so rustc really runs, searches the tree cargo actually writes,
and accepts only a file newer than the build — so "no assembly" can never again mean "an
old one will do". Verified from a clean tree and a warm one, twice each: 23 kernel
labels, 0 copies outside our assembly.

Pushed as 5f677c0. It is unrelated to the copy sizing except that the copy PR is what made CI disagree with itself.

@PhillThomas
PhillThomas merged commit b2ba17c into main Nov 28, 2026
4 checks passed
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