Skip to content

Commit

Permalink
zstd: Don't allocate dataStorage when using byteBuf (#741)
Browse files Browse the repository at this point in the history
Credit to @greatroar for finding this.

Alternative to #721
  • Loading branch information
klauspost authored Jan 18, 2023
1 parent fbae784 commit 4b0abf4
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions zstd/blockdec.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,14 @@ func (b *blockDec) reset(br byteBuffer, windowSize uint64) error {
}

// Read block data.
if cap(b.dataStorage) < cSize {
if _, ok := br.(*byteBuf); !ok && cap(b.dataStorage) < cSize {
// byteBuf doesn't need a destination buffer.
if b.lowMem || cSize > maxCompressedBlockSize {
b.dataStorage = make([]byte, 0, cSize+compressedBlockOverAlloc)
} else {
b.dataStorage = make([]byte, 0, maxCompressedBlockSizeAlloc)
}
}
if cap(b.dst) <= maxSize {
b.dst = make([]byte, 0, maxSize+1)
}
b.data, err = br.readBig(cSize, b.dataStorage)
if err != nil {
if debugDecoder {
Expand All @@ -210,6 +208,9 @@ func (b *blockDec) reset(br byteBuffer, windowSize uint64) error {
}
return err
}
if cap(b.dst) <= maxSize {
b.dst = make([]byte, 0, maxSize+1)
}
return nil
}

Expand Down

0 comments on commit 4b0abf4

Please sign in to comment.