Skip to content

Commit

Permalink
Tweak encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost committed Dec 11, 2023
1 parent fb2efcb commit 617aeec
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
11 changes: 8 additions & 3 deletions s2/encode_best.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,14 @@ func emitCopySize(offset, length int) int {
}

// Offset no more than 2 bytes.
if offset < 2048 && length < 12 {
// Emit up to 11 bytes with short offset.
return 2
if offset < 1024 {
if length < 11+8 {
// Emit up to 18 bytes with short offset.
return 2
}
if length < 18+256 {
return 3
}
}
// 2 byte offset + Variable length (base length 4).
return emitCopy2Size(length)
Expand Down
21 changes: 15 additions & 6 deletions s2/encode_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func encodeLength60(dst []byte, tag uint8, length int) int {
// 1 <= offset && offset <= math.MaxUint32
// 4 <= length && length <= 1 << 24
func emitCopy(dst []byte, offset, length int) int {
offset--
if offset >= 65536 {
// Encode tag+length as up to 4 bytes.
n := encodeLength(dst, tagCopy4, length-3)
Expand All @@ -208,12 +209,20 @@ func emitCopy(dst []byte, offset, length int) int {
}

// Offset no more than 2 bytes and length less than 12.
if offset < 2048 && length < 12 {
// emit 12 bytes as tagCopy1, rest as repeats.
dst[1] = uint8(offset)
dst[0] = uint8(offset>>8)<<5 | uint8(7)<<2 | tagCopy1
length -= 12
return 2
if offset < 1024 {
if length < 12+8 {
// FIXME: Incorrect encoding
dst[1] = uint8(offset)
dst[0] = uint8(offset>>8)<<5 | uint8(length-4)<<2 | tagCopy1
return 2
}
if length < 18+256 {
// FIXME: Incorrect encoding
dst[2] = uint8(length - 18)
dst[1] = uint8(offset)
dst[0] = uint8(offset>>8)<<5 | uint8(11+8)<<2 | tagCopy1
return 3
}
}

// 2 byte offset, with variable length.
Expand Down
19 changes: 19 additions & 0 deletions s2/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package s2

import (
"bytes"
"encoding/binary"
"fmt"
"math"
"testing"
Expand Down Expand Up @@ -67,3 +68,21 @@ func TestEncodeHuge(t *testing.T) {
}
test(t, make([]byte, MaxBlockSize))
}

func TestSizes(t *testing.T) {
var src [2]byte
src[0] = 123
src[1] = 57
s := 2

want := int(uint32(src[s-2])&0xe0<<3 | uint32(src[s-1]))
//got := bits.RotateLeft16(binary.LittleEndian.Uint16(src[:]), 16-5) & 2047
got := binary.LittleEndian.Uint16(src[:])
t.Logf("w:%012b G:%016b", want, got)
for i := 4; i < 100; i++ {
if i == 99 {
i = (1 << 24) - 1
}
t.Logf("%d: short:%d medium: %d long: %d repeat: %d", i, emitCopySize(10, i), emitCopySize(4000, i), emitCopySize(70000, i), emitRepeatSize(0, i))
}
}

0 comments on commit 617aeec

Please sign in to comment.