Skip to content

Commit 4a97174

Browse files
authored
snappy: Use dedicated function for scoring (#614)
* snappy: Use dedicated function for scoring Saves a byte here and there, nothing significant. A bit faster
1 parent 72cae76 commit 4a97174

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

s2/encode_best.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ func encodeBlockBestSnappy(dst, src []byte) (d int) {
370370
}
371371
offset := m.s - m.offset
372372

373-
return score - emitCopySize(offset, m.length)
373+
return score - emitCopyNoRepeatSize(offset, m.length)
374374
}
375375

376376
matchAt := func(offset, s int, first uint32) match {
@@ -581,6 +581,28 @@ func emitCopySize(offset, length int) int {
581581
return 2
582582
}
583583

584+
// emitCopyNoRepeatSize returns the size to encode the offset+length
585+
//
586+
// It assumes that:
587+
// 1 <= offset && offset <= math.MaxUint32
588+
// 4 <= length && length <= 1 << 24
589+
func emitCopyNoRepeatSize(offset, length int) int {
590+
if offset >= 65536 {
591+
return 5 + 5*(length/64)
592+
}
593+
594+
// Offset no more than 2 bytes.
595+
if length > 64 {
596+
// Emit remaining as repeats, at least 4 bytes remain.
597+
return 3 + 3*(length/60)
598+
}
599+
if length >= 12 || offset >= 2048 {
600+
return 3
601+
}
602+
// Emit the remaining copy, encoded as 2 bytes.
603+
return 2
604+
}
605+
584606
// emitRepeatSize returns the number of bytes required to encode a repeat.
585607
// Length must be at least 4 and < 1<<24
586608
func emitRepeatSize(offset, length int) int {

0 commit comments

Comments
 (0)