-
Notifications
You must be signed in to change notification settings - Fork 99
Decompress: reuse the caller-supplied buffer for unknown-size frames #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
KrisKennawayDD
merged 1 commit into
1.x
from
kris.kennaway/decompress-reuse-caller-buffer
Jul 24, 2026
+187
−30
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| package zstd | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "testing" | ||
| ) | ||
|
|
||
| // unknownSizeFrame returns a zstd frame that does not advertise its decompressed | ||
| // size in the header (as produced by the streaming writer, and by legacy zstd | ||
| // v0.5 frames), verifying that is the case so callers exercise the intended path. | ||
| func unknownSizeFrame(t *testing.T, payload []byte) []byte { | ||
| t.Helper() | ||
| var b bytes.Buffer | ||
| w := NewWriter(&b) | ||
| if _, err := w.Write(payload); err != nil { | ||
| t.Fatalf("write: %v", err) | ||
| } | ||
| if err := w.Close(); err != nil { | ||
| t.Fatalf("close: %v", err) | ||
| } | ||
| frame := b.Bytes() | ||
| if _, found := decompressSizeHint(frame); found { | ||
| t.Fatal("streaming frame unexpectedly advertises its size") | ||
| } | ||
| return frame | ||
| } | ||
|
|
||
| type namedDecompressor struct { | ||
| name string | ||
| fn func(dst, src []byte) ([]byte, error) | ||
| } | ||
|
|
||
| // decompressors are the entry points that share the caller-buffer-reuse logic. | ||
| // ctx.Decompress uses a fresh context per call so tests don't share state. | ||
| func decompressors() []namedDecompressor { | ||
| return []namedDecompressor{ | ||
| {"Decompress", Decompress}, | ||
| {"ctx.Decompress", func(dst, src []byte) ([]byte, error) { return NewCtx().Decompress(dst, src) }}, | ||
| } | ||
| } | ||
|
|
||
| // sameBuffer reports whether out was decoded into buf's backing array. | ||
| func sameBuffer(out, buf []byte) bool { | ||
| return len(out) > 0 && len(buf) > 0 && &out[0] == &buf[0] | ||
| } | ||
|
|
||
| // TestDecompressReusesCallerBufferUnknownSize verifies that for an unknown-size | ||
| // frame, an adequate caller buffer is decoded into rather than discarded in | ||
| // favour of allocating decompressSizeBufferLimit. | ||
| func TestDecompressReusesCallerBufferUnknownSize(t *testing.T) { | ||
| payload := bytes.Repeat([]byte("datadog-"), 525) // 4200 bytes | ||
| frame := unknownSizeFrame(t, payload) | ||
|
|
||
| for _, d := range decompressors() { | ||
| t.Run(d.name, func(t *testing.T) { | ||
| buf := make([]byte, 8192) // adequate for the payload, far below the bound | ||
| out, err := d.fn(buf, frame) | ||
| if err != nil { | ||
| t.Fatalf("decompress: %v", err) | ||
| } | ||
| if !bytes.Equal(out, payload) { | ||
| t.Fatalf("round-trip mismatch") | ||
| } | ||
| if !sameBuffer(out, buf) { | ||
| t.Fatalf("caller buffer should have been reused") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestDecompressUnknownSizeTooSmallBuffer verifies the fallback still works when | ||
| // the caller buffer is too small for an unknown-size frame. | ||
| func TestDecompressUnknownSizeTooSmallBuffer(t *testing.T) { | ||
| payload := bytes.Repeat([]byte("datadog-"), 525) | ||
| frame := unknownSizeFrame(t, payload) | ||
|
|
||
| for _, d := range decompressors() { | ||
| t.Run(d.name, func(t *testing.T) { | ||
| out, err := d.fn(make([]byte, 8), frame) // too small; must fall back | ||
| if err != nil { | ||
| t.Fatalf("decompress: %v", err) | ||
| } | ||
| if !bytes.Equal(out, payload) { | ||
| t.Fatalf("round-trip mismatch on fallback") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestDecompressUnknownSizeNilBuffer verifies nil dst still decompresses. | ||
| func TestDecompressUnknownSizeNilBuffer(t *testing.T) { | ||
| payload := bytes.Repeat([]byte("datadog-"), 525) | ||
| frame := unknownSizeFrame(t, payload) | ||
|
|
||
| for _, d := range decompressors() { | ||
| t.Run(d.name, func(t *testing.T) { | ||
| out, err := d.fn(nil, frame) | ||
| if err != nil { | ||
| t.Fatalf("decompress: %v", err) | ||
| } | ||
| if !bytes.Equal(out, payload) { | ||
| t.Fatalf("round-trip mismatch on nil dst") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestDecompressKnownSizeReusesCallerBuffer verifies that for frames that do | ||
| // advertise their size, an adequate caller buffer is still reused. | ||
| func TestDecompressKnownSizeReusesCallerBuffer(t *testing.T) { | ||
| payload := bytes.Repeat([]byte("datadog-"), 525) | ||
| frame, err := Compress(nil, payload) | ||
| if err != nil { | ||
| t.Fatalf("Compress: %v", err) | ||
| } | ||
|
|
||
| for _, d := range decompressors() { | ||
| t.Run(d.name, func(t *testing.T) { | ||
| buf := make([]byte, 8192) | ||
| out, err := d.fn(buf, frame) | ||
| if err != nil { | ||
| t.Fatalf("decompress: %v", err) | ||
| } | ||
| if !bytes.Equal(out, payload) { | ||
| t.Fatalf("round-trip mismatch") | ||
| } | ||
| if !sameBuffer(out, buf) { | ||
| t.Fatalf("caller buffer should have been reused") | ||
| } | ||
| }) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should make it the same logic here to be consistent ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a comment about why this diverges