Keep rare decompression paths out of the hot loop under Dynamic PGO

Merged
BR
17d ago
+108-41

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 IncrementalCopy tails 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 when TryFastAppend fails 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 three remaining > 16/32/48 checks 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:

MethodRuntime1.3.1This PRRatioStd dev, 1.3.1 → this PR
StreamDecompressHtml.NET 10.0123.890 µs108.476 µs0.8913.6 → 4.4 µs
StreamDecompressHtml.NET 11.0120.911 µs117.927 µs0.9914.0 → 4.0 µs
BlockDecompress64KbHtml.NET 10.09.385 µs9.155 µs0.98
BlockDecompress64KbHtml.NET 11.08.994 µs8.728 µs0.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