Skip to content

Commit

Permalink
Return smaller slices when compressing
Browse files Browse the repository at this point in the history
Fixes #21
  • Loading branch information
karelbilek committed Dec 2, 2024
1 parent be494b5 commit 44664fd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions native/compressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ func (c *Compressor) Compress(in, out []byte, f compress) (int, []byte, error) {
if err == errorShortBuffer { // if still doesn't fit (shouldn't happen at all)
out = make([]byte, 1000+len(in)*2)
n, _, _ := c.compress(in, out, f)
return n, out[:n], errors.New("libdeflate: native: compressed data is much larger than uncompressed")
outSmallCap := make([]byte, len(out))
copy(outSmallCap, out)
return n, outSmallCap, errors.New("libdeflate: native: compressed data is much larger than uncompressed")
}

return n, out[:n], nil
outSmallCap := make([]byte, len(out))
copy(outSmallCap, out)
return n, outSmallCap, nil
}

func (c *Compressor) compress(in, out []byte, f compress) (int, []byte, error) {
Expand Down
8 changes: 6 additions & 2 deletions v2/native/compressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ func (c *Compressor) Compress(in, out []byte, f compress) (int, []byte, error) {
if err == errorShortBuffer { // if still doesn't fit (shouldn't happen at all)
out = make([]byte, 1000+len(in)*2)
n, _, _ := c.compress(in, out, f)
return n, out[:n], errors.New("libdeflate: native: compressed data is much larger than uncompressed")
outSmallCap := make([]byte, len(out))
copy(outSmallCap, out)
return n, outSmallCap, errors.New("libdeflate: native: compressed data is much larger than uncompressed")
}

return n, out[:n], nil
outSmallCap := make([]byte, len(out))
copy(outSmallCap, out)
return n, outSmallCap, nil
}

func (c *Compressor) compress(in, out []byte, f compress) (int, []byte, error) {
Expand Down

0 comments on commit 44664fd

Please sign in to comment.