Skip to content

Commit

Permalink
Merge pull request #22 from karelbilek/master
Browse files Browse the repository at this point in the history
Return smaller slices when compressing
  • Loading branch information
4kills authored Dec 6, 2024
2 parents be494b5 + 44664fd commit 25e4748
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 25e4748

Please sign in to comment.