Compare buffer distances instead of forming out-of-range refs
Summary
A memory-safety scan flagged bounds checks that build a ref only to compare it. Examples are bufferEnd - 16 and opEnd - 16 in CopyHelpers.IncrementalCopy, and nextIp in the compressor's hash-skip loop. When the buffer is short or the skip is large, these refs point outside their buffer. The .NET runtime requires a ref to stay inside its object, or one past the end, even if it is only compared. The existing SAFETY comments described these refs as "only compared, never dereferenced", which isn't sufficient.
This PR rewrites those checks as distances between in-bounds refs, using Unsafe.ByteOffset. The code already uses this pattern elsewhere, for example in FindMatchLength.
Changes
CopyHelpers.IncrementalCopy- The
op/opEndagainstbufferEnd - 16,bufferEnd - 11andbufferEnd - 8checks now readUnsafe.ByteOffset(..., ref bufferEnd) >= N. - The three
op < opEnd - 16/32/48checks now compare oneByteOffset(ref op, ref opEnd), computed once. - The slop loop drops its
loopEnd = bufferEnd - 16and checksByteOffset(ref op, ref bufferEnd) > 16instead. - The "only compared, never dereferenced" SAFETY comments are replaced.
- The
SnappyCompressor: the hash-skip loop checksByteOffset(in ip, in ipLimit) < bytesBetweenHashLookupsbefore it formsnextIp. The old check came after. The result is the same becauseip <= ipLimitholds there.bufferEnd - 15(CopyHelpers.cs:124) is left as is: it is inside the buffer whenever it is formed.
Each rewrite gives the same result as the original. The signed comparison is correct because the left ref is never past the right one at these points.
Performance
Each rewrite should compile to the same number of instructions: sub, cmp and a signed jump, in place of lea, cmp and an unsigned jump. The slop loop does one extra subtraction per iteration, for at most 4 iterations. I haven't inspected the disassembly.
A/B against main (41ca711), BenchmarkDotNet Default job, out of process:
- net10.0:
CompressAllwas 1.5–5.3% faster on all 11 files.DecompressAllwas mostly 0–3% faster, withplrabn12.txt+4.7% andurls.10K+3.0%.mainalways ran first, so some run-order bias is possible. - net11.0: individual cases swung 10–25% in both directions between runs.
geo.protodatadecompression looked 12–25% slower on the branch in three runs.
I checked that geo.protodata result with a separate harness that times many process launches. On .NET 11, SnappyStream decompression settles into one of two speeds per process, about 23 µs or about 28 µs, whichever build is running. main landed in the slow mode in 3 of 12 launches and this branch in 2 of 12. Fast-mode medians were 22.45–24.60 µs on main and 22.68–23.77 µs on the branch. The Default job uses one launch per case, so a single run reports whichever mode it hit. On .NET 11 the change is therefore neutral within what can be measured. The block API (Snappy.Decompress(span, span)) has no such bimodality, and the two builds matched there: medians of 14.2–14.4 µs each.
Notes for reviewers
- The two-speed launches come from the benchmark setup, not from this change; they show up on
maintoo. Finding what picks the mode, and making the benchmark configs robust to it, is being worked on separately. - Not run on ARM.
Testing
dotnet build -c Release: 0 warnings, 0 errors.dotnet test -c Release: 1,232 passed, 0 failed (net8.0, net10.0, net11.0, net481).
🤖 Generated with Claude Code