Skip to content

Commit ece2ca7

Browse files
committed
Small performance optimization in lz4 compressor implementation.
Patch by João Reis; reviewed by TBD for CASSGO-90
1 parent 722707e commit ece2ca7

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

lz4/lz4.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,17 @@ func (s LZ4Compressor) AppendCompressedWithLength(dst, src []byte) ([]byte, erro
5353
maxLength := lz4.CompressBlockBound(len(src))
5454
oldDstLen := len(dst)
5555
dst = grow(dst, maxLength+dataLengthSize)
56+
slicedDst := sliceDest(dst, oldDstLen)
5657

5758
var compressor lz4.Compressor
58-
n, err := compressor.CompressBlock(src, dst[oldDstLen+dataLengthSize:])
59+
n, err := compressor.CompressBlock(src, slicedDst[dataLengthSize:])
5960
// According to lz4.CompressBlock doc, it doesn't fail as long as the dst
6061
// buffer length is at least lz4.CompressBlockBound(len(data))) bytes, but
6162
// we check for error anyway just to be thorough.
6263
if err != nil {
6364
return nil, err
6465
}
65-
binary.BigEndian.PutUint32(dst[oldDstLen:oldDstLen+dataLengthSize], uint32(len(src)))
66+
binary.BigEndian.PutUint32(slicedDst[:dataLengthSize], uint32(len(src)))
6667
return dst[:oldDstLen+n+dataLengthSize], nil
6768
}
6869

@@ -76,7 +77,8 @@ func (s LZ4Compressor) AppendDecompressedWithLength(dst, src []byte) ([]byte, er
7677
}
7778
oldDstLen := len(dst)
7879
dst = grow(dst, int(uncompressedLength))
79-
n, err := lz4.UncompressBlock(src[dataLengthSize:], dst[oldDstLen:])
80+
slicedDst := sliceDest(dst, oldDstLen)
81+
n, err := lz4.UncompressBlock(src[dataLengthSize:], slicedDst)
8082
return dst[:oldDstLen+n], err
8183

8284
}
@@ -85,9 +87,10 @@ func (s LZ4Compressor) AppendCompressed(dst, src []byte) ([]byte, error) {
8587
maxLength := lz4.CompressBlockBound(len(src))
8688
oldDstLen := len(dst)
8789
dst = grow(dst, maxLength)
90+
slicedDst := sliceDest(dst, oldDstLen)
8891

8992
var compressor lz4.Compressor
90-
n, err := compressor.CompressBlock(src, dst[oldDstLen:])
93+
n, err := compressor.CompressBlock(src, slicedDst)
9194
if err != nil {
9295
return nil, err
9396
}
@@ -101,7 +104,8 @@ func (s LZ4Compressor) AppendDecompressed(dst, src []byte, uncompressedLength ui
101104
}
102105
oldDstLen := len(dst)
103106
dst = grow(dst, int(uncompressedLength))
104-
n, err := lz4.UncompressBlock(src, dst[oldDstLen:])
107+
slicedDst := sliceDest(dst, oldDstLen)
108+
n, err := lz4.UncompressBlock(src, slicedDst)
105109
return dst[:oldDstLen+n], err
106110
}
107111

@@ -110,8 +114,18 @@ func grow(b []byte, n int) []byte {
110114
oldLen := len(b)
111115
if cap(b)-oldLen < n {
112116
newBuf := make([]byte, oldLen+n)
113-
copy(newBuf, b)
114-
b = newBuf
117+
if oldLen > 0 {
118+
copy(newBuf, b)
119+
}
120+
return newBuf
115121
}
116122
return b[:oldLen+n]
117123
}
124+
125+
func sliceDest(dst []byte, oldDstLen int) []byte {
126+
if oldDstLen > 0 {
127+
return dst[oldDstLen:]
128+
} else {
129+
return dst
130+
}
131+
}

0 commit comments

Comments
 (0)