Repository navigation
Conversation
Prepare internal block decoding for a streaming public API while keeping the public surface unchanged.
Preserve consumed offsets when tag decoding fails and reject literals that exceed the declared output length.
brantburnett
left a comment
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Good catch — fixed. NeedMoreData now returns bytesConsumed == 0 for a partial length, and segmented callers retain and replay those bytes.
| } | ||
| catch (InvalidDataException) | ||
| { | ||
| bytesConsumed += tagBytesConsumed; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Agreed — simplified this so the try/catch only wraps DecompressAllTags, with bytesConsumed updated immediately after a successful call.
| private static void Decompress(SnappyDecompressor decompressor, ReadOnlySpan<byte> input) | ||
| { | ||
| OperationStatus status = decompressor.Decompress(input, out int bytesConsumed); | ||
| if (status == OperationStatus.InvalidData || bytesConsumed != input.Length) |
There was a problem hiding this comment.
I believe this is inconsistent with the current behavior, which ignores extra trailing bytes after the block?
There was a problem hiding this comment.
You're right — I removed the full-input-consumption check, so trailing bytes continue to be ignored.
|
|
||
| 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()); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Good point — the write boundary now uses the smaller of ExpectedLength and the actual lookback buffer length.
|
Merged the latest main, so the workflow fix is included too. |
| return bytesConsumed; | ||
| } | ||
|
|
||
| private static void Decompress(SnappyDecompressor decompressor, ReadOnlySequence<byte> input) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| 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)); | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Summary
OperationStatusand exactbytesConsumedfrom the internal block decompressorInvalidDatauntilReset()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