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

Function encodeBlockBetterGo

s2/encode_better.go:50–300  ·  view source on GitHub ↗

encodeBlockBetter encodes a non-empty src to a guaranteed-large-enough dst. It assumes that the varint-encoded length of the decompressed bytes has already been written. It also assumes that: len(dst) >= MaxEncodedLen(len(src)) && minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize

(dst, src []byte)

Source from the content-addressed store, hash-verified

48// len(dst) >= MaxEncodedLen(len(src)) &&
49// minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize
50func encodeBlockBetterGo(dst, src []byte) (d int) {
51 // sLimit is when to stop looking for offset/length copies. The inputMargin
52 // lets us use a fast path for emitLiteral in the main loop, while we are
53 // looking for copies.
54 sLimit := len(src) - inputMargin
55 if len(src) < minNonLiteralBlockSize {
56 return 0
57 }
58
59 // Initialize the hash tables.
60 const (
61 // Long hash matches.
62 lTableBits = betterLongTableBits
63 maxLTableSize = betterLongTableSize
64
65 // Short hash matches.
66 sTableBits = betterShortTableBits
67 maxSTableSize = betterShortTableSize
68 )
69
70 tbl := getBetterTables()
71 lTable := &tbl.lTable
72 sTable := &tbl.sTable
73 defer betterTablePool.Put(tbl)
74
75 // Bail if we can't compress to at least this.
76 dstLimit := len(src) - len(src)>>5 - 6
77
78 // nextEmit is where in src the next emitLiteral should start from.
79 nextEmit := 0
80
81 // The encoded form must start with a literal, as there are no previous
82 // bytes to copy, so we start looking for hash matches at s == 1.
83 s := 1
84 cv := load64(src, s)
85
86 // We initialize repeat to 0, so we never match on first attempt
87 repeat := 0
88
89 for {
90 candidateL := 0
91 nextS := 0
92 for {
93 // Next src position to check
94 nextS = s + (s-nextEmit)>>7 + 1
95 if nextS > sLimit {
96 goto emitRemainder
97 }
98 hashL := hash7(cv, lTableBits)
99 hashS := hash4(cv, sTableBits)
100 candidateL = int(lTable[hashL])
101 candidateS := int(sTable[hashS])
102 lTable[hashL] = uint32(s)
103 sTable[hashS] = uint32(s)
104
105 valLong := load64(src, candidateL)
106 valShort := load64(src, candidateS)
107

Callers 2

FuzzEncodingBlocksFunction · 0.85
encodeBlockBetterFunction · 0.85

Calls 8

getBetterTablesFunction · 0.85
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…