From c928a8d246b99596f55f9b3480dddf2dd182aa89 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Mon, 11 Dec 2023 14:21:02 +0100 Subject: [PATCH] Use a bit for extra copy length in TagCopy4. --- s2/encode_best.go | 6 +++++- s2/encode_go.go | 7 ++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/s2/encode_best.go b/s2/encode_best.go index 1e66d332c8..22e741e88f 100644 --- a/s2/encode_best.go +++ b/s2/encode_best.go @@ -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. diff --git a/s2/encode_go.go b/s2/encode_go.go index 8fe687b702..967611d371 100644 --- a/s2/encode_go.go +++ b/s2/encode_go.go @@ -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)