Skip to content

Speed up the Zstandard decoder hot paths and benchmark it - #74

Open
mahach666 wants to merge 1 commit into
feature/issue-22-las-exporterfrom
feature/issue-23-zstd-performance
Open

mahach666 wants to merge 1 commit into
feature/issue-22-las-exporterfrom
feature/issue-23-zstd-performance

Conversation

@mahach666

Copy link
Copy Markdown
Contributor

Closes #23

Based on #73 (the branch feature/issue-22-las-exporter); merge the earlier pull requests first.

What changed

  • Profiled the decoder with dotnet-trace (per-symbol Huffman decode and the sequence loop dominate) and sped up the hot
    paths without unsafe code and without weakening any check:
    • the four Huffman streams of a block now decode interleaved round-robin instead of one after another, so four
      independent bit readers overlap in the processor;
    • the per-symbol table mask is hoisted out of the bit-reader loop (PeekMasked);
    • Refill is marked no-inline so the hot Peek/Read stay small enough to inline; hot fields are hoisted to locals
      and CopyMatch carries an inline hint.
  • New in-repo benchmark ZstdBenchmarkTests decodes ~102 MiB of concatenated reference frames, asserts the output, and
    prints our MB/s next to a reference decoder (the managed C# port ZstdSharp.Port, test-only dependency, MIT).
  • A rejected idea is documented below so it is not retried blindly: pre-growing the output to the declared content size
    showed no win and would weaken the rule that memory is only obtained for data a block is about to produce.

Files changed

  • src/PclIO/Internal/ZstdHuffman.cs: DecodeInterleaved, mask hoisting.
  • src/PclIO/Internal/ZstdBitReaders.cs: PeekMasked, no-inline Refill.
  • src/PclIO/Internal/ZstdDecoder.cs: interleaved call site, hoisted locals, inline hint.
  • tests/PclIO.Tests/ZstdBenchmarkTests.cs: new benchmark (correctness-asserted, timing only reported).
  • tests/PclIO.Tests/PclIO.Tests.csproj, Directory.Packages.props: test-only ZstdSharp.Port 0.8.8 (exact pin).
  • tests/PclIO.Tests/ZstdDecoderTests.cs: ContentOf shared with the benchmark.
  • THIRD-PARTY-NOTICES.md, CHANGELOG.md: the new test dependency and the change.

Tests

  • dotnet build — 0 warnings, 0 errors.
  • dotnet test (Debug and Release) — 687 passed, 0 failed, 10 skipped (the tests that need private scan files) on
    .NET 8 and .NET 10, including all existing Zstandard vector, fuzz and damaged-input tests.
  • Benchmark on this machine (Release, best of 5 interleaved runs, 9.2 MiB in / 101.6 MiB out):
    before: ours ~61 ms (~1660 MiB/s out), reference ~31 ms (~3250 MiB/s out) — about 2x;
    after: ours ~52 ms (~1950 MiB/s out), reference ~31 ms — about 1.7x.

Limits

  • The remaining gap (~1.7x on this payload) was not closed: the hot loops are now close to what safe C# does per symbol
    (bounds-checked table loads, sequential bit dependencies), and going further would need unsafe code, which the issue
    rules out. A dotnet-trace profile during the work showed sequence execution ~64% and Huffman decode ~10% of decode time.
  • The benchmark payload replays reference frames, so branch predictors see repeated patterns; absolute numbers are
    machine-specific — the test asserts only correctness, never a time, so it stays green everywhere.
  • No allocation strategy was changed: output still grows by doubling and is only ever obtained for data a block is about
    to produce.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant