Skip to content

Commit

Permalink
[zstd] Remove Decompress multiple retries
Browse files Browse the repository at this point in the history
Since zstd 1.3.0+, the zstd header has size hint so you should be able to decompress the first time directly with that hint OR directly fallback to streaming. No need to try 3 times
  • Loading branch information
Viq111 committed Apr 6, 2022
1 parent 30c4b29 commit 489b911
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions zstd.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,34 +125,25 @@ func Decompress(dst, src []byte) ([]byte, error) {
if len(src) == 0 {
return []byte{}, ErrEmptySlice
}
decompress := func(dst, src []byte) ([]byte, error) {

cWritten := C.ZSTD_decompress(
unsafe.Pointer(&dst[0]),
C.size_t(len(dst)),
unsafe.Pointer(&src[0]),
C.size_t(len(src)))

written := int(cWritten)
// Check error
if err := getError(written); err != nil {
return nil, err
}
return dst[:written], nil
}

bound := decompressSizeHint(src)
if cap(dst) >= bound {
dst = dst[0:cap(dst)]
} else {
dst = make([]byte, bound)
}
for i := 0; i < 3; i++ { // 3 tries to allocate a bigger buffer
result, err := decompress(dst, src)
if !IsDstSizeTooSmallError(err) {
return result, err
}
dst = make([]byte, len(dst)*2) // Grow buffer by 2

written := int(C.ZSTD_decompress(
unsafe.Pointer(&dst[0]),
C.size_t(len(dst)),
unsafe.Pointer(&src[0]),
C.size_t(len(src))))
err := getError(written)
if err == nil {
return dst[:written], nil
}
if !IsDstSizeTooSmallError(err) {
return nil, err
}

// We failed getting a dst buffer of correct size, use stream API
Expand Down

0 comments on commit 489b911

Please sign in to comment.