Skip to content

Expose reusable block codec lifecycle - #148

Closed
thomhurst wants to merge 1 commit into
brantburnett:mainfrom
thomhurst:reusable-block-codecs
Closed

thomhurst wants to merge 1 commit into
brantburnett:mainfrom
thomhurst:reusable-block-codecs

Conversation

@thomhurst

Copy link
Copy Markdown

Expose reusable raw-block compressor and decompressor lifecycles.

Motivation

High-throughput callers currently use the static block APIs, which construct a codec driver per block. Dekaf measured 48-50 B for compression and 80-81 B for decompression per call. Reflection or unsafe access to the internal engines is incompatible with Native AOT and trimming.

This is the upstream prerequisite for Dekaf #2352.

Solution

  • Make SnappyCompressor and SnappyDecompressor supported public sealed types.
  • Retain compressor working memory across calls.
  • Add decompressor reset with a replaceable IBufferWriter<byte> destination.
  • Add sequence-to-writer decompression that resets automatically.
  • Guard destination replacement during an active block.
  • Document disposal and non-concurrent ownership.
  • Keep benchmark-only and duplicate helper APIs internal.

The static Snappy APIs retain their existing behavior and delegate to the reusable engine.

Validation

  • dotnet build Snappier.slnx --configuration Release: 0 warnings/errors.
  • Tests: net48 268/268; net8.0, net9.0, net10.0 each 284/284.
  • Package validation passes using next-version assembly metadata (1.3.2).
  • Existing IsAotCompatible analyzers pass cleanly.

BenchmarkDotNet 0.15.2, .NET 10, DefaultJob, 64 KiB HTML block:

Operation Per-call Reusable Delta Allocation
Compress 34.81 us 35.16 us +1.0%, same rank 50 B -> 0 B
Decompress 10.355 us 9.411 us -9.1% 81 B -> 0 B

A package release containing this API would let downstream callers consume it without reflection or vendoring.

@Kuinox

Kuinox commented Aug 27, 2026

Copy link
Copy Markdown

Ohhh, yes, that's exactly the stuff I need for my 0 allocations parquet library.

@brantburnett

brantburnett commented Aug 28, 2026 •

Copy link
Copy Markdown
Owner

I'm sorry for my late reply, I've been meaning to get back to this.

I do like the idea here, but I have one concern. Originally, those backing classes were kept internal to control the public API surface. Once public, it becomes a breaking change if we perform any refactoring. I'm not in love with the current API surface of them for a public-facing scenario.

I think we should spend some time looking at it and cleaning it up some to make sure we're 100% happy before making it public. In particular, I think we should consider methods that return OpertionStatus with values like NeedMoreData to support a cleaner data streaming story within block operations.

@thomhurst

Copy link
Copy Markdown
Author

No worries! And agreed — exposing the internal classes as-is locks things in more than it's worth. How about this instead: keep SnappyCompressor/SnappyDecompressor internal and add two small public wrapper types, so the public contract is just the wrapper and the internals stay free to change.

For decompression, something shaped like BrotliDecoder:

public sealed class SnappyBlockDecoder : IDisposable
{
    public OperationStatus Decompress(ReadOnlySpan<byte> source, Span<byte> destination,
        out int bytesConsumed, out int bytesWritten);

    // one-shot convenience: resets automatically, throws on bad data
    public void Decompress(ReadOnlySequence<byte> source, IBufferWriter<byte> destination);

    public void Reset(); // start a new block, keep working memory
    public void Dispose();
}

NeedMoreData = source consumed but the block isn't finished; DestinationTooSmall = more output pending, call again (empty source is fine); Done; InvalidData = corrupt block, Reset() required before reuse. The internals are already partway there — the varint path uses OperationStatus — and since the lookback buffer holds the whole block, output backpressure never blocks input consumption, so the state machine stays simple.

For compression I'd deliberately not use OperationStatus: the block format leads with the uncompressed-length varint so input can't stream, which makes NeedMoreData unreachable — a status return would just be a Try-pattern in disguise. Brotli does the same (one-shot is bool TryCompress). So:

public sealed class SnappyBlockEncoder : IDisposable
{
    public int Compress(ReadOnlySpan<byte> source, Span<byte> destination); // throws if too small
    public bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten);
    public void Compress(ReadOnlySequence<byte> source, IBufferWriter<byte> destination);
    public void Dispose();
}

(I did consider skipping new types entirely and just caching the working memory inside the static methods — that fixes the per-call allocations but gives no streaming story, so I dropped it.)

One question: after Done, should the next Decompress call auto-reset, or require an explicit Reset()? I lean explicit, but auto-reset is nicer when decoding blocks back-to-back. Happy either way.

If this direction works for you I'll rework the PR to match.

@brantburnett

Copy link
Copy Markdown
Owner

Overall, I think this is a solid plan. My one recommendation is that we should do a PR just for the internal refactoring to use OperationStatus, without the new public APIs. Once merged, we can expose the public APIs that use it. This keeps the scope of the change more reasonable and easier to review/understand.

@thomhurst thomhurst closed this Aug 31, 2026
thomhurst added a commit to thomhurst/Snappier that referenced this pull request Aug 31, 2026
Expose stable encoder and decoder wrappers while keeping codec internals free to evolve.

Refs brantburnett#148
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.

3 participants