Skip to content

Commit

Permalink
zstd: Replace bytes.Equal with faster comparisons
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
greatroar committed Nov 22, 2022
1 parent c1e79a0 commit d4db81d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
5 changes: 2 additions & 3 deletions zstd/decodeheader.go
Expand Up @@ -4,7 +4,6 @@
package zstd

import (
"bytes"
"encoding/binary"
"errors"
"io"
Expand Down Expand Up @@ -102,8 +101,8 @@ func (h *Header) Decode(in []byte) error {
}
h.HeaderSize += 4
b, in := in[:4], in[4:]
if !bytes.Equal(b, frameMagic) {
if !bytes.Equal(b[1:4], skippableFrameMagic) || b[0]&0xf0 != 0x50 {
if string(b) != frameMagic {
if string(b[1:4]) != skippableFrameMagic || b[0]&0xf0 != 0x50 {
return ErrMagicMismatch
}
if len(in) < 4 {
Expand Down
5 changes: 2 additions & 3 deletions zstd/dict.go
@@ -1,7 +1,6 @@
package zstd

import (
"bytes"
"encoding/binary"
"errors"
"fmt"
Expand All @@ -20,7 +19,7 @@ type dict struct {
content []byte
}

var dictMagic = [4]byte{0x37, 0xa4, 0x30, 0xec}
const dictMagic = "\x37\xa4\x30\xec"

// ID returns the dictionary id or 0 if d is nil.
func (d *dict) ID() uint32 {
Expand Down Expand Up @@ -50,7 +49,7 @@ func loadDict(b []byte) (*dict, error) {
ofDec: sequenceDec{fse: &fseDecoder{}},
mlDec: sequenceDec{fse: &fseDecoder{}},
}
if !bytes.Equal(b[:4], dictMagic[:]) {
if string(b[:4]) != dictMagic {
return nil, ErrMagicMismatch
}
d.id = binary.LittleEndian.Uint32(b[4:8])
Expand Down
33 changes: 14 additions & 19 deletions zstd/framedec.go
Expand Up @@ -5,7 +5,7 @@
package zstd

import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"io"
Expand Down Expand Up @@ -43,9 +43,9 @@ const (
MaxWindowSize = 1 << 29
)

var (
frameMagic = []byte{0x28, 0xb5, 0x2f, 0xfd}
skippableFrameMagic = []byte{0x2a, 0x4d, 0x18}
const (
frameMagic = "\x28\xb5\x2f\xfd"
skippableFrameMagic = "\x2a\x4d\x18"
)

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

if !bytes.Equal(signature[1:4], skippableFrameMagic) || signature[0]&0xf0 != 0x50 {
if string(signature[1:4]) != skippableFrameMagic || signature[0]&0xf0 != 0x50 {
if debugDecoder {
println("Not skippable", hex.EncodeToString(signature[:]), hex.EncodeToString(skippableFrameMagic))
println("Not skippable", hex.EncodeToString(signature[:]), hex.EncodeToString([]byte(skippableFrameMagic)))
}
// Break if not skippable frame.
break
Expand All @@ -114,9 +114,9 @@ func (d *frameDec) reset(br byteBuffer) error {
return err
}
}
if !bytes.Equal(signature[:], frameMagic) {
if string(signature[:]) != frameMagic {
if debugDecoder {
println("Got magic numbers: ", signature, "want:", frameMagic)
println("Got magic numbers: ", signature, "want:", []byte(frameMagic))
}
return ErrMagicMismatch
}
Expand Down Expand Up @@ -305,7 +305,7 @@ func (d *frameDec) checkCRC() error {
}

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

var tmp [4]byte
got := d.crc.Sum64()
// Flip to match file order.
tmp[0] = byte(got >> 0)
tmp[1] = byte(got >> 8)
tmp[2] = byte(got >> 16)
tmp[3] = byte(got >> 24)
want := binary.LittleEndian.Uint32(buf[:4])
got := uint32(d.crc.Sum64())

if !bytes.Equal(tmp[:], want) {
if got != want {
if debugDecoder {
println("CRC Check Failed:", tmp[:], "!=", want)
printf("CRC check failed: got %08x, want %08x\n", got, want)
}
return ErrCRCMismatch
}
if debugDecoder {
println("CRC ok", tmp[:])
printf("CRC ok %08x\n", got)
}
return nil
}
Expand Down

0 comments on commit d4db81d

Please sign in to comment.