Repository navigation
fix: wipe FixedSizeDictionary's slots when its version wraps - #704
nadrummond wants to merge 1 commit into
Conversation
Reset() and OptionalClear() bump a byte version and leave old entries in place, counting them as absent while their version differs. Once the version wraps from 255 back to 1, an entry written 255 versions earlier matches again, so a pooled BindingContext could hand a block param bound by one render to a later, unrelated render. When the version wraps, the entries and indexes are now wiped before version 1 is reused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
/harness adopt See if you can reconcile fixing the bug with not introducing the performance regression. |
|
Good catch on the benchmark alert, it was worth digging into. It turns out the slowdown comes from the machines the benchmarks ran on rather than from this change: run on the same machine, #704 is as fast as master. The benchmark job compares each PR with the most recent master run. That master run (11a2daa) happened to land on the fastest machine type in recent runs, and #704's run happened to land on the slowest. The same master code runs about twice as fast on one as on the other, which on its own is enough to trip the 2× alert. That would also explain why benchmarks this PR doesn't touch, such as template compilation, were flagged as well, and why #708 received the same 18 alerts on the same machine type, while #706, which carried the identical change, received none on a faster one. To rule the hardware out, I ran master, #704, and a variant that moves the wipe into a separate non-inlined method on a single runner, one after another. Across all 32 benchmarks, #704 comes out at 1.000× master, and master's own two runs differ from each other by 0.8%, so I couldn't find any difference between them. The non-inlined variant comes out at 1.001×, so it doesn't seem to gain anything, and I've left #704 as it is. If you'd like it measured another way, or you see something I've missed, I'm happy to look again. Technical detailWhy the change shouldn't cost anything. On the hot path, Which machine each run landed on.
Against the median of those five master runs on the EPYC 7763, #704's CI run has a geometric mean of 1.015× across all 32 benchmarks. The action compares against the latest Same-runner A/B. One job on an AMD EPYC 9V74 built master (11a2daa), #704 (ddb1260) and #704 with the wrap moved into a
All 32 benchmarks
|
Fixes #705.
FixedSizeDictionaryclears cheaply:Reset()andOptionalClear()bump abyteversion and leaveold entries in place, counting them as absent while their version differs. When the version wraps from
255 back to 1, an entry written 255 versions earlier matches the current version again. Pooled
BindingContexts go throughOptionalClear()on every return (BindingContext.Pool.cs:61-62), so ablock param bound by one render comes back in a later, unrelated render, from another
IHandlebarsinstance, and shadows that render's own model. The issue has a repro.
Reset()andClear()now move to the next version throughNextVersion(), which wipes_entriesand
_indexeswhen the version wraps, before reusing version 1, so no entry can outlive a full cycleof versions. The cost is one clear of both arrays per 255 resets (112 slots for a binding context's
dictionaries); the per-return path is otherwise unchanged.
EntryIndexvalues kept outside thedictionary (
BindingContext.WellKnownVariables, indexes intoContextDataObject) can still carry aversion from an earlier cycle; after the wipe such an index points at an empty slot or at an entry
written since the wipe, never at one left from before the wrap.
Tests
FixedSizeDictionaryTests.EntryFromAnEarlierUseStaysGoneOnceTheVersionWraps, a theory overResetand
OptionalClear: add a key, reset 255 times, add another key, and the first key must stay absent.Deterministic.
BindingContextPoolTests.BlockParamBoundInOneRenderIsNotSeenByLaterRenders, in a non-parallelcollection: 20 renders bind
x, then a second template renders up to 100,000 times and must neverprint it. It reads
xin two nested frames, so the pool's rotation can't hand the context holdingthe old binding only to the root frame, where nothing is read. It depends on the state other tests
leave the shared pool in, so the dictionary theory is the deterministic check.
On
master(11a2daa), with only the tests added, the dictionary theory fails on every run, and thepool test fails both alone (5 of 5) and in the full suite (15 of 15). With the fix, the full suite
passes on net10.0: 2,352 tests (2,349 on
master, plus the three new ones). The issue's console reproprints the first render's value on renders 509, 1019, 1529, … before the fix, and nothing in 100,000
renders after it.