Keep rare decompression paths out of the hot loop under Dynamic PGO
Merged
BR
17d ago+108+108+012345678901234567890123456789-41-41-01234567890123456789
Motivation
A version comparison against 1.3.1 showed StreamDecompressHtml slower on main: 1.16 on .NET 10 and 1.24 on .NET 11.
Under Dynamic PGO, the JIT lays out DecompressAllTags differently from one process to the next:
- In the fast layout, stream decompression runs at about 115 µs.
- In the slow layout, the literal slow path and the longer
IncrementalCopytails sit inside the hot loop, and it runs at about 140 µs.
The code itself is no slower. With DOTNET_TieredPGO=0, main matches 1.3.1. No single commit caused the regression; #158 and #162 each made the slow layout more likely.
Modifications
SnappyDecompressor.DecompressAllTags: the literal path that runs whenTryFastAppendfails moves into a new[MethodImpl(NoInlining)]method,AppendLiteralSlow. It handles length prefixes longer than one byte and literals split across input buffers.CopyHelpers.IncrementalCopy: the threeremaining > 16/32/48checks are now nested instead of independent. Copies of 16 bytes or less take one branch, and the longer-copy code is one block the JIT can keep out of the hot path.
Results
BenchmarkDotNet, Version Comparison against 1.3.1 with 5 launches per job, on an i7-13800H:
| Method | Runtime | 1.3.1 | This PR | Ratio | Std dev, 1.3.1 → this PR |
|---|---|---|---|---|---|
| StreamDecompressHtml | .NET 10.0 | 123.890 µs | 108.476 µs | 0.89 | 13.6 → 4.4 µs |
| StreamDecompressHtml | .NET 11.0 | 120.911 µs | 117.927 µs | 0.99 | 14.0 → 4.0 µs |
| BlockDecompress64KbHtml | .NET 10.0 | 9.385 µs | 9.155 µs | 0.98 | |
| BlockDecompress64KbHtml | .NET 11.0 | 8.994 µs | 8.728 µs | 0.97 |
- Per-file decompression (
DecompressAll, 11 files) ranges from 0.91 to 1.02 on both runtimes. Most files are 2–9% faster, and none is outside noise. - Stand-alone stream-decompress harness, 16 processes per runtime:
- .NET 10: slow launches dropped from 8 to 0, and the mean went from 127.1 µs to 112.1 µs.
- .NET 11: slow launches dropped from 11 to 0, and the mean went from 131.2 µs to 119.1 µs.
Testing
All 1,268 tests pass.
🤖 Generated with Claude Code