Skip to content

Commit

Permalink
Use a bit for extra copy length in TagCopy4.
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost committed Dec 11, 2023
1 parent 617aeec commit c928a8d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 5 additions & 1 deletion s2/encode_best.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,11 @@ emitRemainder:
func emitCopySize(offset, length int) int {
if offset >= 65536 {
// 3 Byte offset + Variable length (base length 4).
return 3 + emitRepeatSize(offset, length-3)
length -= 3
if length > 28 {
length -= 28
}
return 3 + emitRepeatSize(offset, length)
}

// Offset no more than 2 bytes.
Expand Down
7 changes: 6 additions & 1 deletion s2/encode_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,13 @@ func encodeLength60(dst []byte, tag uint8, length int) int {
func emitCopy(dst []byte, offset, length int) int {
offset--
if offset >= 65536 {
length = length - 3
// Encode tag+length as up to 4 bytes.
n := encodeLength(dst, tagCopy4, length-3)
if length > 28 {
offset |= 1 << 23
length -= 28
}
n := encodeLength(dst, tagCopy4, length)
// Encode offset as 3 bytes.
dst[n+2] = uint8(offset >> 16)
dst[n+1] = uint8(offset >> 8)
Expand Down

0 comments on commit c928a8d

Please sign in to comment.