Skip to content

Commit

Permalink
Tweak emit priority.
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost committed Sep 10, 2023
1 parent d1416ed commit 4100709
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 51 deletions.
27 changes: 8 additions & 19 deletions s2/encode_best.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,29 +713,18 @@ emitRemainder:
// 4 <= length && length <= 1 << 24
func emitCopySize(offset, length int) int {
if offset >= 65536 {
i := 0
if length > 64 {
length -= 64
if length >= 4 {
// Emit remaining as repeats
return 4 + emitRepeatSize(offset, length)
}
i = 4
}
if length == 0 {
return i
}
return i + 4
// Emit remaining as repeats
return 4 + emitRepeatSize(offset, length-64)
}

// Offset no more than 2 bytes.
if length > 64 {
if offset < 2048 {
// Emit 8 bytes, then rest as repeats...
return 2 + emitRepeatSize(offset, length-8)
return 2 + emitRepeatSize(offset, length-12)
}
// Emit remaining as repeats, at least 4 bytes remain.
return 3 + emitRepeatSize(offset, length-60)
return 3 + emitRepeatSize(offset, length-64)
}
if length >= 12 || offset >= 2048 {
return 3
Expand Down Expand Up @@ -770,6 +759,9 @@ func emitCopyNoRepeatSize(offset, length int) int {
// emitRepeatSize returns the number of bytes required to encode a repeat.
// Length must be at least 4 and < 1<<24
func emitRepeatSize(offset, length int) int {
if length <= 0 {
return 0
}
if length <= 256 {
return 2
}
Expand All @@ -781,8 +773,5 @@ func emitRepeatSize(offset, length int) int {
if length > maxRepeat {
left = length - maxRepeat
}
if left > 0 {
return 5 + emitRepeatSize(offset, left)
}
return 5
return 5 + emitRepeatSize(offset, left)
}
58 changes: 26 additions & 32 deletions s2/encode_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func emitRepeat(dst []byte, offset, length int) int {
dst[0] = 63 | tagLiteral
return 2
}
length--

if length < 65536 {
dst[3] = uint8(length >> 8)
dst[2] = uint8(length >> 0)
Expand Down Expand Up @@ -140,51 +140,45 @@ func emitRepeat(dst []byte, offset, length int) int {
// 4 <= length && length <= 1 << 24
func emitCopy(dst []byte, offset, length int) int {
if offset >= 65536 {
i := 0
if length > 64 {
// Emit a length 64 copy, encoded as 4 bytes.
dst[3] = uint8(offset >> 16)
dst[2] = uint8(offset >> 8)
// Emit a length 64 copy, encoded as 4 bytes.
if length <= 64 {
// Emit a copy, offset encoded as 3 bytes.
dst[0] = uint8(length-1)<<2 | tagCopy4
dst[1] = uint8(offset)
dst[0] = 63<<2 | tagCopy4
length -= 64
if length >= 4 {
// Emit remaining as repeats
return 4 + emitRepeat(dst[4:], offset, length)
}
i = 4
}
if length == 0 {
return i
dst[2] = uint8(offset >> 8)
dst[3] = uint8(offset >> 16)
return 4
}
// Emit a copy, offset encoded as 3 bytes.
dst[i+0] = uint8(length-1)<<2 | tagCopy4
dst[i+1] = uint8(offset)
dst[i+2] = uint8(offset >> 8)
dst[i+3] = uint8(offset >> 16)
return i + 4

dst[3] = uint8(offset >> 16)
dst[2] = uint8(offset >> 8)
dst[1] = uint8(offset)
dst[0] = 63<<2 | tagCopy4
length -= 64
// Emit remaining as repeats
return 4 + emitRepeat(dst[4:], offset, length)
}

// Offset no more than 2 bytes.
if length > 64 {
off := 3
if offset < 2048 {
// emit 8 bytes as tagCopy1, rest as repeats.
dst[1] = uint8(offset)
dst[0] = uint8(offset>>8)<<5 | uint8(8-4)<<2 | tagCopy1
length -= 8
off = 2
dst[0] = uint8(offset>>8)<<5 | uint8(7)<<2 | tagCopy1
length -= 12
return 2 + emitRepeat(dst[2:], offset, length)
} else {
// Emit a length 60 copy, encoded as 3 bytes.
// Emit remaining as repeat value (minimum 4 bytes).
// Emit a length 64 copy, encoded as 3 bytes.
// Emit remaining as repeat value.
dst[2] = uint8(offset >> 8)
dst[1] = uint8(offset)
dst[0] = 59<<2 | tagCopy2
length -= 60
dst[0] = 63<<2 | tagCopy2
length -= 64
// Emit remaining as repeats. At least 1 byte.
return 3 + emitRepeat(dst[3:], offset, length)
}
// Emit remaining as repeats, at least 4 bytes remain.
return off + emitRepeat(dst[off:], offset, length)
}

if length >= 12 || offset >= 2048 {
// Emit the remaining copy, encoded as 3 bytes.
dst[2] = uint8(offset >> 8)
Expand Down
1 change: 1 addition & 0 deletions s2/s2.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const (
magicChunkSnappy = "\xff\x06\x00\x00" + magicBodySnappy
magicBodySnappy = "sNaPpY"
magicBody = "S2sTwO"
magicBodyPP = "S2s2++"

// maxBlockSize is the maximum size of the input to encodeBlock.
//
Expand Down

0 comments on commit 4100709

Please sign in to comment.