MCPcopy
hub / github.com/klauspost/compress / encodeBlockBetterGo64K

Function encodeBlockBetterGo64K

s2/encode_better.go:485–723  ·  view source on GitHub ↗
(dst, src []byte)

Source from the content-addressed store, hash-verified

483}
484
485func encodeBlockBetterGo64K(dst, src []byte) (d int) {
486 // sLimit is when to stop looking for offset/length copies. The inputMargin
487 // lets us use a fast path for emitLiteral in the main loop, while we are
488 // looking for copies.
489 sLimit := len(src) - inputMargin
490 if len(src) < minNonLiteralBlockSize {
491 return 0
492 }
493 // Initialize the hash tables.
494 // Use smaller tables for smaller blocks
495 const (
496 // Long hash matches.
497 lTableBits = 16
498 maxLTableSize = 1 << lTableBits
499
500 // Short hash matches.
501 sTableBits = 13
502 maxSTableSize = 1 << sTableBits
503 )
504
505 var lTable [maxLTableSize]uint16
506 var sTable [maxSTableSize]uint16
507
508 // Bail if we can't compress to at least this.
509 dstLimit := len(src) - len(src)>>5 - 6
510
511 // nextEmit is where in src the next emitLiteral should start from.
512 nextEmit := 0
513
514 // The encoded form must start with a literal, as there are no previous
515 // bytes to copy, so we start looking for hash matches at s == 1.
516 s := 1
517 cv := load64(src, s)
518
519 // We initialize repeat to 0, so we never match on first attempt
520 repeat := 0
521
522 for {
523 candidateL := 0
524 nextS := 0
525 for {
526 // Next src position to check
527 nextS = s + (s-nextEmit)>>6 + 1
528 if nextS > sLimit {
529 goto emitRemainder
530 }
531 hashL := hash7(cv, lTableBits)
532 hashS := hash4(cv, sTableBits)
533 candidateL = int(lTable[hashL])
534 candidateS := int(sTable[hashS])
535 lTable[hashL] = uint16(s)
536 sTable[hashS] = uint16(s)
537
538 valLong := load64(src, candidateL)
539 valShort := load64(src, candidateS)
540
541 // If long matches at least 8 bytes, use that.
542 if cv == valLong {

Callers 2

FuzzEncodingBlocksFunction · 0.85
encodeBlockBetterFunction · 0.85

Calls 7

load64Function · 0.70
hash7Function · 0.70
hash4Function · 0.70
emitLiteralFunction · 0.70
emitRepeatFunction · 0.70
load32Function · 0.70
emitCopyFunction · 0.70

Tested by 1

FuzzEncodingBlocksFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…