Skip to content

Commit c09b86f

Browse files
committed
zstd: Replace bytes.Equal with smaller comparisons
This replaces some bytes.Equal calls, which compile down to calls to runtime.memequal, with uint32 or string comparisons that become CMPL instructions (or a series of CMPBs, in one case).
1 parent c1e79a0 commit c09b86f

File tree

3 files changed

+18
-25
lines changed

3 files changed

+18
-25
lines changed

zstd/decodeheader.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package zstd
55

66
import (
7-
"bytes"
87
"encoding/binary"
98
"errors"
109
"io"
@@ -102,8 +101,8 @@ func (h *Header) Decode(in []byte) error {
102101
}
103102
h.HeaderSize += 4
104103
b, in := in[:4], in[4:]
105-
if !bytes.Equal(b, frameMagic) {
106-
if !bytes.Equal(b[1:4], skippableFrameMagic) || b[0]&0xf0 != 0x50 {
104+
if string(b) != frameMagic {
105+
if string(b[1:4]) != skippableFrameMagic || b[0]&0xf0 != 0x50 {
107106
return ErrMagicMismatch
108107
}
109108
if len(in) < 4 {

zstd/dict.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package zstd
22

33
import (
4-
"bytes"
54
"encoding/binary"
65
"errors"
76
"fmt"
@@ -20,7 +19,7 @@ type dict struct {
2019
content []byte
2120
}
2221

23-
var dictMagic = [4]byte{0x37, 0xa4, 0x30, 0xec}
22+
const dictMagic = "\x37\xa4\x30\xec"
2423

2524
// ID returns the dictionary id or 0 if d is nil.
2625
func (d *dict) ID() uint32 {
@@ -50,7 +49,7 @@ func loadDict(b []byte) (*dict, error) {
5049
ofDec: sequenceDec{fse: &fseDecoder{}},
5150
mlDec: sequenceDec{fse: &fseDecoder{}},
5251
}
53-
if !bytes.Equal(b[:4], dictMagic[:]) {
52+
if string(b[:4]) != dictMagic {
5453
return nil, ErrMagicMismatch
5554
}
5655
d.id = binary.LittleEndian.Uint32(b[4:8])

zstd/framedec.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
package zstd
66

77
import (
8-
"bytes"
8+
"encoding/binary"
99
"encoding/hex"
1010
"errors"
1111
"io"
@@ -43,9 +43,9 @@ const (
4343
MaxWindowSize = 1 << 29
4444
)
4545

46-
var (
47-
frameMagic = []byte{0x28, 0xb5, 0x2f, 0xfd}
48-
skippableFrameMagic = []byte{0x2a, 0x4d, 0x18}
46+
const (
47+
frameMagic = "\x28\xb5\x2f\xfd"
48+
skippableFrameMagic = "\x2a\x4d\x18"
4949
)
5050

5151
func newFrameDec(o decoderOptions) *frameDec {
@@ -89,9 +89,9 @@ func (d *frameDec) reset(br byteBuffer) error {
8989
copy(signature[1:], b)
9090
}
9191

92-
if !bytes.Equal(signature[1:4], skippableFrameMagic) || signature[0]&0xf0 != 0x50 {
92+
if string(signature[1:4]) != skippableFrameMagic || signature[0]&0xf0 != 0x50 {
9393
if debugDecoder {
94-
println("Not skippable", hex.EncodeToString(signature[:]), hex.EncodeToString(skippableFrameMagic))
94+
println("Not skippable", hex.EncodeToString(signature[:]), hex.EncodeToString([]byte(skippableFrameMagic)))
9595
}
9696
// Break if not skippable frame.
9797
break
@@ -114,9 +114,9 @@ func (d *frameDec) reset(br byteBuffer) error {
114114
return err
115115
}
116116
}
117-
if !bytes.Equal(signature[:], frameMagic) {
117+
if string(signature[:]) != frameMagic {
118118
if debugDecoder {
119-
println("Got magic numbers: ", signature, "want:", frameMagic)
119+
println("Got magic numbers: ", signature, "want:", []byte(frameMagic))
120120
}
121121
return ErrMagicMismatch
122122
}
@@ -305,7 +305,7 @@ func (d *frameDec) checkCRC() error {
305305
}
306306

307307
// We can overwrite upper tmp now
308-
want, err := d.rawInput.readSmall(4)
308+
buf, err := d.rawInput.readSmall(4)
309309
if err != nil {
310310
println("CRC missing?", err)
311311
return err
@@ -315,22 +315,17 @@ func (d *frameDec) checkCRC() error {
315315
return nil
316316
}
317317

318-
var tmp [4]byte
319-
got := d.crc.Sum64()
320-
// Flip to match file order.
321-
tmp[0] = byte(got >> 0)
322-
tmp[1] = byte(got >> 8)
323-
tmp[2] = byte(got >> 16)
324-
tmp[3] = byte(got >> 24)
318+
want := binary.LittleEndian.Uint32(buf[:4])
319+
got := uint32(d.crc.Sum64())
325320

326-
if !bytes.Equal(tmp[:], want) {
321+
if got != want {
327322
if debugDecoder {
328-
println("CRC Check Failed:", tmp[:], "!=", want)
323+
printf("CRC check failed: got %08x, want %08x\n", got, want)
329324
}
330325
return ErrCRCMismatch
331326
}
332327
if debugDecoder {
333-
println("CRC ok", tmp[:])
328+
printf("CRC ok %08x\n", got)
334329
}
335330
return nil
336331
}

0 commit comments

Comments
 (0)