Skip to content

Commit ff17f80

Browse files
authored
Merge pull request #519 from mykaul/len_compress
(improvement) remove redundant len() calls from compression code
2 parents 2c1c37b + 5d866da commit ff17f80

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

frame.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,8 @@ func (f *framer) setLength(length int) {
746746
}
747747

748748
func (f *framer) finish() error {
749-
if len(f.buf) > maxFrameSize {
749+
bufLen := len(f.buf)
750+
if bufLen > maxFrameSize {
750751
// huge app frame, lets remove it so it doesn't bloat the heap
751752
f.buf = make([]byte, defaultBufSize)
752753
return ErrFrameTooBig
@@ -764,8 +765,9 @@ func (f *framer) finish() error {
764765
}
765766

766767
f.buf = append(f.buf[:headSize], compressed...)
768+
bufLen = len(f.buf)
767769
}
768-
length := len(f.buf) - headSize
770+
length := bufLen - headSize
769771
f.setLength(length)
770772

771773
return nil

lz4/lz4.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ func (s LZ4Compressor) Name() string {
4848
}
4949

5050
func (s LZ4Compressor) Encode(data []byte) ([]byte, error) {
51-
buf := make([]byte, lz4.CompressBlockBound(len(data)+4))
51+
dataLen := len(data)
52+
buf := make([]byte, lz4.CompressBlockBound(dataLen)+4)
5253
var compressor lz4.Compressor
5354
n, err := compressor.CompressBlock(data, buf[4:])
5455
// According to lz4.CompressBlock doc, it doesn't fail as long as the dst
@@ -57,7 +58,7 @@ func (s LZ4Compressor) Encode(data []byte) ([]byte, error) {
5758
if err != nil {
5859
return nil, err
5960
}
60-
binary.BigEndian.PutUint32(buf, uint32(len(data)))
61+
binary.BigEndian.PutUint32(buf, uint32(dataLen))
6162
return buf[:n+4], nil
6263
}
6364

0 commit comments

Comments
 (0)