Move hot-path exception messages into NoInlining throw helpers

Merged
BR
18d ago
+81-21

What changed

  • Adds [MethodImpl(MethodImplOptions.NoInlining)] helpers to ThrowHelper. Each holds its own message and forwards to the general throw helpers (ThrowInvalidDataException, ThrowInvalidOperationException, ThrowArgumentException, ThrowArgumentOutOfRangeException). This follows the existing ThrowArgumentExceptionInsufficientOutputBuffer and ThrowInvalidDataExceptionIncompleteSnappyBlock pattern.
  • Replaces the inline-message throws in SnappyCompressor, SnappyDecompressor, VarIntEncoding.Read and HashTable with calls to the new helpers.

Why

The hot methods still loaded the message strings and set up the arguments for each ThrowHelper call. With the messages moved into non-inlined helpers, a throw site in the hot method is a single call with no arguments. The input-too-large check in SnappyCompressor.Compress(ReadOnlySequence<byte>, IBufferWriter<byte>) also built an interpolated string ({uint.MaxValue} is not a constant), and that string building now happens only inside the helper.

Reviewer notes

  • Exception types and message text are unchanged.
  • The two argument-exception helpers still take nameof(...) from the caller, matching ThrowArgumentExceptionInsufficientOutputBuffer. That leaves one constant string load at those two sites.
  • SnappyDecompressor.ThrowCannotUseWithBufferWriter was already a NoInlining wrapper and is unchanged.
  • Build has no warnings. All 1232 tests pass on net8.0, net9.0, net10.0 and net481. I haven't inspected the JIT disassembly.

šŸ¤– Generated with Claude Code