Skip to content

Refactor block decompression to use OperationStatus - #149

Open
thomhurst wants to merge 5 commits into
brantburnett:mainfrom
thomhurst:refactor-block-decompression-status
Open

thomhurst wants to merge 5 commits into
brantburnett:mainfrom
thomhurst:refactor-block-decompression-status

Conversation

@thomhurst

@thomhurst thomhurst commented Aug 31, 2026 •

Copy link
Copy Markdown

Summary

  • return OperationStatus and exact bytesConsumed from the internal block decompressor
  • stop at the declared block boundary and preserve InvalidData until Reset()
  • adapt existing internal callers without adding or changing public APIs

This is the internal-refactoring precursor requested in the latest discussion on #148. Public encoder/decoder APIs remain for a follow-up PR after this lands.

Validation

  • dotnet build Snappier.slnx --configuration Release: 0 warnings/errors
  • net48: 273/273 tests passed
  • net8.0, net9.0, net10.0: 289/289 tests passed on each target

Prepare internal block decoding for a streaming public API while keeping the public surface unchanged.
Copilot AI lite review requested due to automatic review settings August 31, 2026 13:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Preserve consumed offsets when tag decoding fails and reject literals that exceed the declared output length.

@brantburnett brantburnett left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you merge in the latest main it should fix the build issue.

{
OperationStatus status = TryReadUncompressedLength(input, out int bytesConsumed);
OperationStatus status = TryReadUncompressedLength(input, out int lengthBytesConsumed);
bytesConsumed = lengthBytesConsumed;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that in the NeedsMoreData scenario, we need to return bytesConsumed of 0. In the case where the buffer only has part of the length, we're not caching the initial bytes for the next call. Instead, we need to ensure that the initial bytes are passed in again on the next call.

@thomhurst thomhurst Aug 31, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — fixed. NeedMoreData now returns bytesConsumed == 0 for a partial length, and segmented callers retain and replay those bytes.

Comment thread Snappier/Internal/SnappyDecompressor.cs Outdated
}
catch (InvalidDataException)
{
bytesConsumed += tagBytesConsumed;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the exception case, tagBytesConsumed will always be 0 because DecompressAllTags never returns. I think we can simplify this by simply bytesConsumed += tagBytesConsumed directly after the call to DecompressAllTags and letting the variable be in a narrower scope. We can also narrow the scope of the try..catch to be only around DecompressAllTags.

@thomhurst thomhurst Aug 31, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed — simplified this so the try/catch only wraps DecompressAllTags, with bytesConsumed updated immediately after a successful call.

Comment thread Snappier/Snappy.cs Outdated
private static void Decompress(SnappyDecompressor decompressor, ReadOnlySpan<byte> input)
{
OperationStatus status = decompressor.Decompress(input, out int bytesConsumed);
if (status == OperationStatus.InvalidData || bytesConsumed != input.Length)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is inconsistent with the current behavior, which ignores extra trailing bytes after the block?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right — I removed the full-input-consumption check, so trailing bytes continue to be ignored.

Comment thread Snappier/Internal/SnappyDecompressor.cs Outdated

ref byte buffer = ref _lookbackBuffer.Span[0];
ref byte bufferEnd = ref Unsafe.Add(ref buffer, _lookbackBuffer.Length);
ref byte bufferEnd = ref Unsafe.Add(ref buffer, ExpectedLength.GetValueOrDefault());

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels unsafe to me from a buffer management perspective, we're indexing into buffer in a way that could allow writing past the end of the buffer if ExpectedLength isn't correct with the lookback buffer size.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point — the write boundary now uses the smaller of ExpectedLength and the actual lookback buffer length.

@thomhurst

Copy link
Copy Markdown
Author

Merged the latest main, so the workflow fix is included too.

@thomhurst

Copy link
Copy Markdown
Author

@brantburnett ?

Comment thread Snappier/Snappy.cs
return bytesConsumed;
}

private static void Decompress(SnappyDecompressor decompressor, ReadOnlySequence<byte> input)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this make more sense as an instance method on SnappyDecompressor, especially if we plan to make SnappyDecompressor public? That way, callers working with a ReadOnlySequence<byte> and a reusable decompressor don't need to replicate this logic.

If we do that, it probably makes sense to let it return OperationStatus as well. I can see scenarios where incomplete sequences are passed in that require more data.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed — moved this onto SnappyDecompressor as OperationStatus Decompress(ReadOnlySequence<byte> input, out long bytesConsumed) in f9be9dc. It walks the segments, stops at Done/InvalidData, and returns NeedMoreData for incomplete sequences, so callers can keep feeding it more data. Snappy.Decompress(ReadOnlySequence<byte>, ...) / DecompressToMemory(ReadOnlySequence<byte>) now just call it and throw on InvalidData. I added tests for various segment sizes, incomplete sequences resumed later, and trailing data after the block.

Comment thread Snappier/Snappy.cs Outdated
Comment on lines +253 to +258
if (bytesConsumed == 0 && !input.IsSingleSegment)
{
int bytesToCopy = (int)Math.Min(input.Length, scratch.Length);
input.Slice(0, bytesToCopy).CopyTo(scratch);
bytesConsumed = Decompress(decompressor, scratch.Slice(0, bytesToCopy));
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the number of spots we need to do this logic (here and in the stream), and therefore consumers may need to do this logic, I'm wondering if the scratch should just be part of the SnappyDecompressor. Instead of leaving bytes in the source buffer, treat them as consumed and put them in a scratch within SnappyDecompressor. This could also help with performance, as this byte-copying approach could be quite expensive.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call — done in f9be9dc. Partial varint lengths are now kept in the decompressor's existing internal scratch (the same one used for partial tags) and reported as consumed, so NeedMoreData always consumes all of the provided input. That let me drop the stackalloc/copy-and-replay logic here, and the equivalent scratch handling in SnappyStreamDecompressor.DecompressBlock, which now passes the chunk bytes straight through. This reverses my earlier change for your first comment, where partial lengths returned bytesConsumed == 0. Doing it internally is cleaner for consumers.

Partial varint lengths are now buffered in the decompressor's internal
scratch and reported as consumed, so callers no longer need to retain and
replay leftover bytes. Adds a ReadOnlySequence<byte> overload of
SnappyDecompressor.Decompress returning OperationStatus, and simplifies
Snappy and SnappyStreamDecompressor to use it.

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.

3 participants