Debian Codegraph @

s2/encode_better.go

raw
1 // Copyright 2016 The Snappy-Go Authors. All rights reserved.
2 // Copyright (c) 2019 Klaus Post. All rights reserved.
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 package s2
7
8 import (
9 "bytes"
10 "fmt"
11 "math/bits"
12 )
13
14 // hash4 returns the hash of the lowest 4 bytes of u to fit in a hash table with h bits.
15 // Preferably h should be a constant and should always be <32.
16 func hash4(u uint64, h uint8) uint32 {
17 const prime4bytes = 2654435761
18 return (uint32(u) * prime4bytes) >> ((32 - h) & 31)
19 }
20
21 // hash5 returns the hash of the lowest 5 bytes of u to fit in a hash table with h bits.
22 // Preferably h should be a constant and should always be <64.
23 func hash5(u uint64, h uint8) uint32 {
24 const prime5bytes = 889523592379
25 return uint32(((u << (64 - 40)) * prime5bytes) >> ((64 - h) & 63))
26 }
27
28 // hash7 returns the hash of the lowest 7 bytes of u to fit in a hash table with h bits.
29 // Preferably h should be a constant and should always be <64.
30 func hash7(u uint64, h uint8) uint32 {
31 const prime7bytes = 58295818150454627
32 return uint32(((u << (64 - 56)) * prime7bytes) >> ((64 - h) & 63))
33 }
34
35 // hash8 returns the hash of u to fit in a hash table with h bits.
36 // Preferably h should be a constant and should always be <64.
37 func hash8(u uint64, h uint8) uint32 {
38 const prime8bytes = 0xcf1bbcdcb7a56463
39 return uint32((u * prime8bytes) >> ((64 - h) & 63))
40 }
41
42 // encodeBlockBetter encodes a non-empty src to a guaranteed-large-enough dst. It
43 // assumes that the varint-encoded length of the decompressed bytes has already
44 // been written.
45 //
46 // It also assumes that:
47 //
48 // len(dst) >= MaxEncodedLen(len(src)) &&
49 // minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize
50 func 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 // Cap the adaptive skip, as encodeBetterBlockAsm and the snappy Go encoder
90 // below already do. Without a cap, a long stretch with no matches lets the
91 // skip grow geometrically until it strides past the start of the next
92 // compressible region and loses the matches there.
93 const maxSkip = 100
94
95 for {
96 candidateL := 0
97 nextS := 0
98 for {
99 // Next src position to check
100 nextS = s + min((s-nextEmit)>>7+1, maxSkip)
101 if nextS > sLimit {
102 goto emitRemainder
103 }
104 hashL := hash7(cv, lTableBits)
105 hashS := hash4(cv, sTableBits)
106 candidateL = int(lTable[hashL])
107 candidateS := int(sTable[hashS])
108 lTable[hashL] = uint32(s)
109 sTable[hashS] = uint32(s)
110
111 valLong := load64(src, candidateL)
112 valShort := load64(src, candidateS)
113
114 // If long matches at least 8 bytes, use that.
115 if cv == valLong {
116 break
117 }
118 if cv == valShort {
119 candidateL = candidateS
120 break
121 }
122
123 // Check repeat at offset checkRep.
124 const checkRep = 1
125 // Minimum length of a repeat. Tested with various values.
126 // While 4-5 offers improvements in some, 6 reduces
127 // regressions significantly.
128 const wantRepeatBytes = 6
129 const repeatMask = ((1 << (wantRepeatBytes * 8)) - 1) << (8 * checkRep)
130 if false && repeat > 0 && cv&repeatMask == load64(src, s-repeat)&repeatMask {
131 base := s + checkRep
132 // Extend back
133 for i := base - repeat; base > nextEmit && i > 0 && src[i-1] == src[base-1]; {
134 i--
135 base--
136 }
137 d += emitLiteral(dst[d:], src[nextEmit:base])
138
139 // Extend forward
140 candidate := s - repeat + wantRepeatBytes + checkRep
141 s += wantRepeatBytes + checkRep
142 for s < len(src) {
143 if len(src)-s < 8 {
144 if src[s] == src[candidate] {
145 s++
146 candidate++
147 continue
148 }
149 break
150 }
151 if diff := load64(src, s) ^ load64(src, candidate); diff != 0 {
152 s += bits.TrailingZeros64(diff) >> 3
153 break
154 }
155 s += 8
156 candidate += 8
157 }
158 // same as `add := emitCopy(dst[d:], repeat, s-base)` but skips storing offset.
159 d += emitRepeat(dst[d:], repeat, s-base)
160 nextEmit = s
161 if s >= sLimit {
162 goto emitRemainder
163 }
164 // Index in-between
165 index0 := base + 1
166 index1 := s - 2
167
168 for index0 < index1 {
169 cv0 := load64(src, index0)
170 cv1 := load64(src, index1)
171 lTable[hash7(cv0, lTableBits)] = uint32(index0)
172 sTable[hash4(cv0>>8, sTableBits)] = uint32(index0 + 1)
173
174 lTable[hash7(cv1, lTableBits)] = uint32(index1)
175 sTable[hash4(cv1>>8, sTableBits)] = uint32(index1 + 1)
176 index0 += 2
177 index1 -= 2
178 }
179
180 cv = load64(src, s)
181 continue
182 }
183
184 // Long likely matches 7, so take that.
185 if uint32(cv) == uint32(valLong) {
186 break
187 }
188
189 // Check our short candidate
190 if uint32(cv) == uint32(valShort) {
191 // Try a long candidate at s+1
192 hashL = hash7(cv>>8, lTableBits)
193 candidateL = int(lTable[hashL])
194 lTable[hashL] = uint32(s + 1)
195 if uint32(cv>>8) == load32(src, candidateL) {
196 s++
197 break
198 }
199 // Use our short candidate.
200 candidateL = candidateS
201 break
202 }
203
204 cv = load64(src, nextS)
205 s = nextS
206 }
207
208 // Extend backwards
209 for candidateL > 0 && s > nextEmit && src[candidateL-1] == src[s-1] {
210 candidateL--
211 s--
212 }
213
214 // Bail if we exceed the maximum size.
215 if d+(s-nextEmit) > dstLimit {
216 return 0
217 }
218
219 base := s
220 offset := base - candidateL
221
222 // Extend the 4-byte match as long as possible.
223 s += 4
224 candidateL += 4
225 for s < len(src) {
226 if len(src)-s < 8 {
227 if src[s] == src[candidateL] {
228 s++
229 candidateL++
230 continue
231 }
232 break
233 }
234 if diff := load64(src, s) ^ load64(src, candidateL); diff != 0 {
235 s += bits.TrailingZeros64(diff) >> 3
236 break
237 }
238 s += 8
239 candidateL += 8
240 }
241
242 if offset > 65535 && s-base <= 5 && repeat != offset {
243 // Bail if the match is equal or worse to the encoding.
244 s = nextS + 1
245 if s >= sLimit {
246 goto emitRemainder
247 }
248 cv = load64(src, s)
249 continue
250 }
251
252 d += emitLiteral(dst[d:], src[nextEmit:base])
253 if repeat == offset {
254 d += emitRepeat(dst[d:], offset, s-base)
255 } else {
256 d += emitCopy(dst[d:], offset, s-base)
257 repeat = offset
258 }
259
260 nextEmit = s
261 if s >= sLimit {
262 goto emitRemainder
263 }
264
265 if d > dstLimit {
266 // Do we have space for more, if not bail.
267 return 0
268 }
269
270 // Index short & long
271 index0 := base + 1
272 index1 := s - 2
273
274 cv0 := load64(src, index0)
275 cv1 := load64(src, index1)
276 lTable[hash7(cv0, lTableBits)] = uint32(index0)
277 sTable[hash4(cv0>>8, sTableBits)] = uint32(index0 + 1)
278
279 // lTable could be postponed, but very minor difference.
280 lTable[hash7(cv1, lTableBits)] = uint32(index1)
281 sTable[hash4(cv1>>8, sTableBits)] = uint32(index1 + 1)
282 index0 += 1
283 index1 -= 1
284 cv = load64(src, s)
285
286 // Index large values sparsely in between.
287 // We do two starting from different offsets for speed.
288 index2 := (index0 + index1 + 1) >> 1
289 for index2 < index1 {
290 lTable[hash7(load64(src, index0), lTableBits)] = uint32(index0)
291 lTable[hash7(load64(src, index2), lTableBits)] = uint32(index2)
292 index0 += 2
293 index2 += 2
294 }
295 }
296
297 emitRemainder:
298 if nextEmit < len(src) {
299 // Bail if we exceed the maximum size.
300 if d+len(src)-nextEmit > dstLimit {
301 return 0
302 }
303 d += emitLiteral(dst[d:], src[nextEmit:])
304 }
305 return d
306 }
307
308 // encodeBlockBetterSnappyGo encodes a non-empty src to a guaranteed-large-enough dst. It
309 // assumes that the varint-encoded length of the decompressed bytes has already
310 // been written.
311 //
312 // It also assumes that:
313 //
314 // len(dst) >= MaxEncodedLen(len(src)) &&
315 // minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize
316 func encodeBlockBetterSnappyGo(dst, src []byte) (d int) {
317 // sLimit is when to stop looking for offset/length copies. The inputMargin
318 // lets us use a fast path for emitLiteral in the main loop, while we are
319 // looking for copies.
320 sLimit := len(src) - inputMargin
321 if len(src) < minNonLiteralBlockSize {
322 return 0
323 }
324
325 // Initialize the hash tables.
326 const (
327 // Long hash matches.
328 lTableBits = betterSnappyLongTableBits
329 maxLTableSize = betterSnappyLongTableSize
330
331 // Short hash matches.
332 sTableBits = betterShortTableBits
333 maxSTableSize = betterShortTableSize
334 )
335
336 tbl := getBetterSnappyTables()
337 lTable := &tbl.lTable
338 sTable := &tbl.sTable
339 defer betterSnappyTablePool.Put(tbl)
340
341 // Bail if we can't compress to at least this.
342 dstLimit := len(src) - len(src)>>5 - 6
343
344 // nextEmit is where in src the next emitLiteral should start from.
345 nextEmit := 0
346
347 // The encoded form must start with a literal, as there are no previous
348 // bytes to copy, so we start looking for hash matches at s == 1.
349 s := 1
350 cv := load64(src, s)
351
352 // We initialize repeat to 0, so we never match on first attempt
353 repeat := 0
354 const maxSkip = 100
355
356 for {
357 candidateL := 0
358 nextS := 0
359 for {
360 // Next src position to check
361 nextS = min(s+(s-nextEmit)>>7+1, s+maxSkip)
362
363 if nextS > sLimit {
364 goto emitRemainder
365 }
366 hashL := hash7(cv, lTableBits)
367 hashS := hash4(cv, sTableBits)
368 candidateL = int(lTable[hashL])
369 candidateS := int(sTable[hashS])
370 lTable[hashL] = uint32(s)
371 sTable[hashS] = uint32(s)
372
373 if uint32(cv) == load32(src, candidateL) {
374 break
375 }
376
377 // Check our short candidate
378 if uint32(cv) == load32(src, candidateS) {
379 // Try a long candidate at s+1
380 hashL = hash7(cv>>8, lTableBits)
381 candidateL = int(lTable[hashL])
382 lTable[hashL] = uint32(s + 1)
383 if uint32(cv>>8) == load32(src, candidateL) {
384 s++
385 break
386 }
387 // Use our short candidate.
388 candidateL = candidateS
389 break
390 }
391
392 cv = load64(src, nextS)
393 s = nextS
394 }
395
396 // Extend backwards
397 for candidateL > 0 && s > nextEmit && src[candidateL-1] == src[s-1] {
398 candidateL--
399 s--
400 }
401
402 // Bail if we exceed the maximum size.
403 if d+(s-nextEmit) > dstLimit {
404 return 0
405 }
406
407 base := s
408 offset := base - candidateL
409
410 // Extend the 4-byte match as long as possible.
411 s += 4
412 candidateL += 4
413 for s < len(src) {
414 if len(src)-s < 8 {
415 if src[s] == src[candidateL] {
416 s++
417 candidateL++
418 continue
419 }
420 break
421 }
422 if diff := load64(src, s) ^ load64(src, candidateL); diff != 0 {
423 s += bits.TrailingZeros64(diff) >> 3
424 break
425 }
426 s += 8
427 candidateL += 8
428 }
429
430 if offset > 65535 && s-base <= 5 && repeat != offset {
431 // Bail if the match is equal or worse to the encoding.
432 s = nextS + 1
433 if s >= sLimit {
434 goto emitRemainder
435 }
436 cv = load64(src, s)
437 continue
438 }
439
440 d += emitLiteral(dst[d:], src[nextEmit:base])
441 d += emitCopyNoRepeat(dst[d:], offset, s-base)
442 repeat = offset
443
444 nextEmit = s
445 if s >= sLimit {
446 goto emitRemainder
447 }
448
449 if d > dstLimit {
450 // Do we have space for more, if not bail.
451 return 0
452 }
453
454 // Index short & long
455 index0 := base + 1
456 index1 := s - 2
457
458 cv0 := load64(src, index0)
459 cv1 := load64(src, index1)
460 lTable[hash7(cv0, lTableBits)] = uint32(index0)
461 sTable[hash4(cv0>>8, sTableBits)] = uint32(index0 + 1)
462
463 lTable[hash7(cv1, lTableBits)] = uint32(index1)
464 sTable[hash4(cv1>>8, sTableBits)] = uint32(index1 + 1)
465 index0 += 1
466 index1 -= 1
467 cv = load64(src, s)
468
469 // Index large values sparsely in between.
470 // We do two starting from different offsets for speed.
471 index2 := (index0 + index1 + 1) >> 1
472 for index2 < index1 {
473 lTable[hash7(load64(src, index0), lTableBits)] = uint32(index0)
474 lTable[hash7(load64(src, index2), lTableBits)] = uint32(index2)
475 index0 += 2
476 index2 += 2
477 }
478 }
479
480 emitRemainder:
481 if nextEmit < len(src) {
482 // Bail if we exceed the maximum size.
483 if d+len(src)-nextEmit > dstLimit {
484 return 0
485 }
486 d += emitLiteral(dst[d:], src[nextEmit:])
487 }
488 return d
489 }
490
491 func encodeBlockBetterGo64K(dst, src []byte) (d int) {
492 // sLimit is when to stop looking for offset/length copies. The inputMargin
493 // lets us use a fast path for emitLiteral in the main loop, while we are
494 // looking for copies.
495 sLimit := len(src) - inputMargin
496 if len(src) < minNonLiteralBlockSize {
497 return 0
498 }
499 // Initialize the hash tables.
500 // Use smaller tables for smaller blocks
501 const (
502 // Long hash matches.
503 lTableBits = 16
504 maxLTableSize = 1 << lTableBits
505
506 // Short hash matches.
507 sTableBits = 13
508 maxSTableSize = 1 << sTableBits
509 )
510
511 var lTable [maxLTableSize]uint16
512 var sTable [maxSTableSize]uint16
513
514 // Bail if we can't compress to at least this.
515 dstLimit := len(src) - len(src)>>5 - 6
516
517 // nextEmit is where in src the next emitLiteral should start from.
518 nextEmit := 0
519
520 // The encoded form must start with a literal, as there are no previous
521 // bytes to copy, so we start looking for hash matches at s == 1.
522 s := 1
523 cv := load64(src, s)
524
525 // We initialize repeat to 0, so we never match on first attempt
526 repeat := 0
527
528 for {
529 candidateL := 0
530 nextS := 0
531 for {
532 // Next src position to check
533 nextS = s + (s-nextEmit)>>6 + 1
534 if nextS > sLimit {
535 goto emitRemainder
536 }
537 hashL := hash7(cv, lTableBits)
538 hashS := hash4(cv, sTableBits)
539 candidateL = int(lTable[hashL])
540 candidateS := int(sTable[hashS])
541 lTable[hashL] = uint16(s)
542 sTable[hashS] = uint16(s)
543
544 valLong := load64(src, candidateL)
545 valShort := load64(src, candidateS)
546
547 // If long matches at least 8 bytes, use that.
548 if cv == valLong {
549 break
550 }
551 if cv == valShort {
552 candidateL = candidateS
553 break
554 }
555
556 // Check repeat at offset checkRep.
557 const checkRep = 1
558 // Minimum length of a repeat. Tested with various values.
559 // While 4-5 offers improvements in some, 6 reduces
560 // regressions significantly.
561 const wantRepeatBytes = 6
562 const repeatMask = ((1 << (wantRepeatBytes * 8)) - 1) << (8 * checkRep)
563 if false && repeat > 0 && cv&repeatMask == load64(src, s-repeat)&repeatMask {
564 base := s + checkRep
565 // Extend back
566 for i := base - repeat; base > nextEmit && i > 0 && src[i-1] == src[base-1]; {
567 i--
568 base--
569 }
570 d += emitLiteral(dst[d:], src[nextEmit:base])
571
572 // Extend forward
573 candidate := s - repeat + wantRepeatBytes + checkRep
574 s += wantRepeatBytes + checkRep
575 for s < len(src) {
576 if len(src)-s < 8 {
577 if src[s] == src[candidate] {
578 s++
579 candidate++
580 continue
581 }
582 break
583 }
584 if diff := load64(src, s) ^ load64(src, candidate); diff != 0 {
585 s += bits.TrailingZeros64(diff) >> 3
586 break
587 }
588 s += 8
589 candidate += 8
590 }
591 // same as `add := emitCopy(dst[d:], repeat, s-base)` but skips storing offset.
592 d += emitRepeat(dst[d:], repeat, s-base)
593 nextEmit = s
594 if s >= sLimit {
595 goto emitRemainder
596 }
597 // Index in-between
598 index0 := base + 1
599 index1 := s - 2
600
601 for index0 < index1 {
602 cv0 := load64(src, index0)
603 cv1 := load64(src, index1)
604 lTable[hash7(cv0, lTableBits)] = uint16(index0)
605 sTable[hash4(cv0>>8, sTableBits)] = uint16(index0 + 1)
606
607 lTable[hash7(cv1, lTableBits)] = uint16(index1)
608 sTable[hash4(cv1>>8, sTableBits)] = uint16(index1 + 1)
609 index0 += 2
610 index1 -= 2
611 }
612
613 cv = load64(src, s)
614 continue
615 }
616
617 // Long likely matches 7, so take that.
618 if uint32(cv) == uint32(valLong) {
619 break
620 }
621
622 // Check our short candidate
623 if uint32(cv) == uint32(valShort) {
624 // Try a long candidate at s+1
625 hashL = hash7(cv>>8, lTableBits)
626 candidateL = int(lTable[hashL])
627 lTable[hashL] = uint16(s + 1)
628 if uint32(cv>>8) == load32(src, candidateL) {
629 s++
630 break
631 }
632 // Use our short candidate.
633 candidateL = candidateS
634 break
635 }
636
637 cv = load64(src, nextS)
638 s = nextS
639 }
640
641 // Extend backwards
642 for candidateL > 0 && s > nextEmit && src[candidateL-1] == src[s-1] {
643 candidateL--
644 s--
645 }
646
647 // Bail if we exceed the maximum size.
648 if d+(s-nextEmit) > dstLimit {
649 return 0
650 }
651
652 base := s
653 offset := base - candidateL
654
655 // Extend the 4-byte match as long as possible.
656 s += 4
657 candidateL += 4
658 for s < len(src) {
659 if len(src)-s < 8 {
660 if src[s] == src[candidateL] {
661 s++
662 candidateL++
663 continue
664 }
665 break
666 }
667 if diff := load64(src, s) ^ load64(src, candidateL); diff != 0 {
668 s += bits.TrailingZeros64(diff) >> 3
669 break
670 }
671 s += 8
672 candidateL += 8
673 }
674
675 d += emitLiteral(dst[d:], src[nextEmit:base])
676 if repeat == offset {
677 d += emitRepeat(dst[d:], offset, s-base)
678 } else {
679 d += emitCopy(dst[d:], offset, s-base)
680 repeat = offset
681 }
682
683 nextEmit = s
684 if s >= sLimit {
685 goto emitRemainder
686 }
687
688 if d > dstLimit {
689 // Do we have space for more, if not bail.
690 return 0
691 }
692
693 // Index short & long
694 index0 := base + 1
695 index1 := s - 2
696
697 cv0 := load64(src, index0)
698 cv1 := load64(src, index1)
699 lTable[hash7(cv0, lTableBits)] = uint16(index0)
700 sTable[hash4(cv0>>8, sTableBits)] = uint16(index0 + 1)
701
702 // lTable could be postponed, but very minor difference.
703 lTable[hash7(cv1, lTableBits)] = uint16(index1)
704 sTable[hash4(cv1>>8, sTableBits)] = uint16(index1 + 1)
705 index0 += 1
706 index1 -= 1
707 cv = load64(src, s)
708
709 // Index large values sparsely in between.
710 // We do two starting from different offsets for speed.
711 index2 := (index0 + index1 + 1) >> 1
712 for index2 < index1 {
713 lTable[hash7(load64(src, index0), lTableBits)] = uint16(index0)
714 lTable[hash7(load64(src, index2), lTableBits)] = uint16(index2)
715 index0 += 2
716 index2 += 2
717 }
718 }
719
720 emitRemainder:
721 if nextEmit < len(src) {
722 // Bail if we exceed the maximum size.
723 if d+len(src)-nextEmit > dstLimit {
724 return 0
725 }
726 d += emitLiteral(dst[d:], src[nextEmit:])
727 }
728 return d
729 }
730
731 // encodeBlockBetterSnappyGo encodes a non-empty src to a guaranteed-large-enough dst. It
732 // assumes that the varint-encoded length of the decompressed bytes has already
733 // been written.
734 //
735 // It also assumes that:
736 //
737 // len(dst) >= MaxEncodedLen(len(src)) &&
738 // minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize
739 func encodeBlockBetterSnappyGo64K(dst, src []byte) (d int) {
740 // sLimit is when to stop looking for offset/length copies. The inputMargin
741 // lets us use a fast path for emitLiteral in the main loop, while we are
742 // looking for copies.
743 sLimit := len(src) - inputMargin
744 if len(src) < minNonLiteralBlockSize {
745 return 0
746 }
747
748 // Initialize the hash tables.
749 // Use smaller tables for smaller blocks
750 const (
751 // Long hash matches.
752 lTableBits = 15
753 maxLTableSize = 1 << lTableBits
754
755 // Short hash matches.
756 sTableBits = 13
757 maxSTableSize = 1 << sTableBits
758 )
759
760 var lTable [maxLTableSize]uint16
761 var sTable [maxSTableSize]uint16
762
763 // Bail if we can't compress to at least this.
764 dstLimit := len(src) - len(src)>>5 - 6
765
766 // nextEmit is where in src the next emitLiteral should start from.
767 nextEmit := 0
768
769 // The encoded form must start with a literal, as there are no previous
770 // bytes to copy, so we start looking for hash matches at s == 1.
771 s := 1
772 cv := load64(src, s)
773
774 const maxSkip = 100
775
776 for {
777 candidateL := 0
778 nextS := 0
779 for {
780 // Next src position to check
781 nextS = min(s+(s-nextEmit)>>6+1, s+maxSkip)
782
783 if nextS > sLimit {
784 goto emitRemainder
785 }
786 hashL := hash7(cv, lTableBits)
787 hashS := hash4(cv, sTableBits)
788 candidateL = int(lTable[hashL])
789 candidateS := int(sTable[hashS])
790 lTable[hashL] = uint16(s)
791 sTable[hashS] = uint16(s)
792
793 if uint32(cv) == load32(src, candidateL) {
794 break
795 }
796
797 // Check our short candidate
798 if uint32(cv) == load32(src, candidateS) {
799 // Try a long candidate at s+1
800 hashL = hash7(cv>>8, lTableBits)
801 candidateL = int(lTable[hashL])
802 lTable[hashL] = uint16(s + 1)
803 if uint32(cv>>8) == load32(src, candidateL) {
804 s++
805 break
806 }
807 // Use our short candidate.
808 candidateL = candidateS
809 break
810 }
811
812 cv = load64(src, nextS)
813 s = nextS
814 }
815
816 // Extend backwards
817 for candidateL > 0 && s > nextEmit && src[candidateL-1] == src[s-1] {
818 candidateL--
819 s--
820 }
821
822 // Bail if we exceed the maximum size.
823 if d+(s-nextEmit) > dstLimit {
824 return 0
825 }
826
827 base := s
828 offset := base - candidateL
829
830 // Extend the 4-byte match as long as possible.
831 s += 4
832 candidateL += 4
833 for s < len(src) {
834 if len(src)-s < 8 {
835 if src[s] == src[candidateL] {
836 s++
837 candidateL++
838 continue
839 }
840 break
841 }
842 if diff := load64(src, s) ^ load64(src, candidateL); diff != 0 {
843 s += bits.TrailingZeros64(diff) >> 3
844 break
845 }
846 s += 8
847 candidateL += 8
848 }
849
850 d += emitLiteral(dst[d:], src[nextEmit:base])
851 d += emitCopyNoRepeat(dst[d:], offset, s-base)
852
853 nextEmit = s
854 if s >= sLimit {
855 goto emitRemainder
856 }
857
858 if d > dstLimit {
859 // Do we have space for more, if not bail.
860 return 0
861 }
862
863 // Index short & long
864 index0 := base + 1
865 index1 := s - 2
866
867 cv0 := load64(src, index0)
868 cv1 := load64(src, index1)
869 lTable[hash7(cv0, lTableBits)] = uint16(index0)
870 sTable[hash4(cv0>>8, sTableBits)] = uint16(index0 + 1)
871
872 lTable[hash7(cv1, lTableBits)] = uint16(index1)
873 sTable[hash4(cv1>>8, sTableBits)] = uint16(index1 + 1)
874 index0 += 1
875 index1 -= 1
876 cv = load64(src, s)
877
878 // Index large values sparsely in between.
879 // We do two starting from different offsets for speed.
880 index2 := (index0 + index1 + 1) >> 1
881 for index2 < index1 {
882 lTable[hash7(load64(src, index0), lTableBits)] = uint16(index0)
883 lTable[hash7(load64(src, index2), lTableBits)] = uint16(index2)
884 index0 += 2
885 index2 += 2
886 }
887 }
888
889 emitRemainder:
890 if nextEmit < len(src) {
891 // Bail if we exceed the maximum size.
892 if d+len(src)-nextEmit > dstLimit {
893 return 0
894 }
895 d += emitLiteral(dst[d:], src[nextEmit:])
896 }
897 return d
898 }
899
900 // encodeBlockBetterDict encodes a non-empty src to a guaranteed-large-enough dst. It
901 // assumes that the varint-encoded length of the decompressed bytes has already
902 // been written.
903 //
904 // It also assumes that:
905 //
906 // len(dst) >= MaxEncodedLen(len(src)) &&
907 // minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize
908 func encodeBlockBetterDict(dst, src []byte, dict *Dict) (d int) {
909 // sLimit is when to stop looking for offset/length copies. The inputMargin
910 // lets us use a fast path for emitLiteral in the main loop, while we are
911 // looking for copies.
912 // Initialize the hash tables.
913 const (
914 // Long hash matches.
915 lTableBits = betterLongTableBits
916 maxLTableSize = betterLongTableSize
917
918 // Short hash matches.
919 sTableBits = betterShortTableBits
920 maxSTableSize = betterShortTableSize
921
922 maxAhead = 8 // maximum bytes ahead without checking sLimit
923
924 debug = false
925 )
926
927 sLimit := min(len(src)-inputMargin, MaxDictSrcOffset-maxAhead)
928 if len(src) < minNonLiteralBlockSize {
929 return 0
930 }
931
932 dict.initBetter()
933
934 tbl := getBetterTables()
935 lTable := &tbl.lTable
936 sTable := &tbl.sTable
937 defer betterTablePool.Put(tbl)
938
939 // Bail if we can't compress to at least this.
940 dstLimit := len(src) - len(src)>>5 - 6
941
942 // nextEmit is where in src the next emitLiteral should start from.
943 nextEmit := 0
944
945 // The encoded form must start with a literal, as there are no previous
946 // bytes to copy, so we start looking for hash matches at s == 1.
947 s := 0
948 cv := load64(src, s)
949
950 // We initialize repeat to 0, so we never match on first attempt
951 repeat := len(dict.dict) - dict.repeat
952
953 // While in dict
954 searchDict:
955 for {
956 candidateL := 0
957 nextS := 0
958 for {
959 // Next src position to check
960 nextS = s + (s-nextEmit)>>7 + 1
961 if nextS > sLimit {
962 break searchDict
963 }
964 hashL := hash7(cv, lTableBits)
965 hashS := hash4(cv, sTableBits)
966 candidateL = int(lTable[hashL])
967 candidateS := int(sTable[hashS])
968 dictL := int(dict.betterTableLong[hashL])
969 dictS := int(dict.betterTableShort[hashS])
970 lTable[hashL] = uint32(s)
971 sTable[hashS] = uint32(s)
972
973 valLong := load64(src, candidateL)
974 valShort := load64(src, candidateS)
975
976 // If long matches at least 8 bytes, use that.
977 if s != 0 {
978 if cv == valLong {
979 goto emitMatch
980 }
981 if cv == valShort {
982 candidateL = candidateS
983 goto emitMatch
984 }
985 }
986
987 // Check dict repeat.
988 if repeat >= s+4 {
989 candidate := len(dict.dict) - repeat + s
990 if candidate > 0 && uint32(cv) == load32(dict.dict, candidate) {
991 // Extend back
992 base := s
993 for i := candidate; base > nextEmit && i > 0 && dict.dict[i-1] == src[base-1]; {
994 i--
995 base--
996 }
997 d += emitLiteral(dst[d:], src[nextEmit:base])
998 if debug && nextEmit != base {
999 fmt.Println("emitted ", base-nextEmit, "literals")
1000 }
1001 s += 4
1002 candidate += 4
1003 for candidate < len(dict.dict)-8 && s <= len(src)-8 {
1004 if diff := load64(src, s) ^ load64(dict.dict, candidate); diff != 0 {
1005 s += bits.TrailingZeros64(diff) >> 3
1006 break
1007 }
1008 s += 8
1009 candidate += 8
1010 }
1011 d += emitRepeat(dst[d:], repeat, s-base)
1012 if debug {
1013 fmt.Println("emitted dict repeat length", s-base, "offset:", repeat, "s:", s)
1014 }
1015 nextEmit = s
1016 if s >= sLimit {
1017 break searchDict
1018 }
1019 // Index in-between
1020 index0 := base + 1
1021 index1 := s - 2
1022
1023 cv = load64(src, s)
1024 for index0 < index1 {
1025 cv0 := load64(src, index0)
1026 cv1 := load64(src, index1)
1027 lTable[hash7(cv0, lTableBits)] = uint32(index0)
1028 sTable[hash4(cv0>>8, sTableBits)] = uint32(index0 + 1)
1029
1030 lTable[hash7(cv1, lTableBits)] = uint32(index1)
1031 sTable[hash4(cv1>>8, sTableBits)] = uint32(index1 + 1)
1032 index0 += 2
1033 index1 -= 2
1034 }
1035 continue
1036 }
1037 }
1038 // Don't try to find match at s==0
1039 if s == 0 {
1040 cv = load64(src, nextS)
1041 s = nextS
1042 continue
1043 }
1044
1045 // Long likely matches 7, so take that.
1046 if uint32(cv) == uint32(valLong) {
1047 goto emitMatch
1048 }
1049
1050 // Long dict...
1051 if uint32(cv) == load32(dict.dict, dictL) {
1052 candidateL = dictL
1053 goto emitDict
1054 }
1055
1056 // Check our short candidate
1057 if uint32(cv) == uint32(valShort) {
1058 // Try a long candidate at s+1
1059 hashL = hash7(cv>>8, lTableBits)
1060 candidateL = int(lTable[hashL])
1061 lTable[hashL] = uint32(s + 1)
1062 if uint32(cv>>8) == load32(src, candidateL) {
1063 s++
1064 goto emitMatch
1065 }
1066 // Use our short candidate.
1067 candidateL = candidateS
1068 goto emitMatch
1069 }
1070 if uint32(cv) == load32(dict.dict, dictS) {
1071 // Try a long candidate at s+1
1072 hashL = hash7(cv>>8, lTableBits)
1073 candidateL = int(lTable[hashL])
1074 lTable[hashL] = uint32(s + 1)
1075 if uint32(cv>>8) == load32(src, candidateL) {
1076 s++
1077 goto emitMatch
1078 }
1079 candidateL = dictS
1080 goto emitDict
1081 }
1082 cv = load64(src, nextS)
1083 s = nextS
1084 }
1085 emitDict:
1086 {
1087 if debug {
1088 if load32(dict.dict, candidateL) != load32(src, s) {
1089 panic("dict emit mismatch")
1090 }
1091 }
1092 // Extend backwards.
1093 // The top bytes will be rechecked to get the full match.
1094 for candidateL > 0 && s > nextEmit && dict.dict[candidateL-1] == src[s-1] {
1095 candidateL--
1096 s--
1097 }
1098
1099 // Bail if we exceed the maximum size.
1100 if d+(s-nextEmit) > dstLimit {
1101 return 0
1102 }
1103
1104 // A 4-byte match has been found. We'll later see if more than 4 bytes
1105 // match. But, prior to the match, src[nextEmit:s] are unmatched. Emit
1106 // them as literal bytes.
1107
1108 d += emitLiteral(dst[d:], src[nextEmit:s])
1109 if debug && nextEmit != s {
1110 fmt.Println("emitted ", s-nextEmit, "literals")
1111 }
1112 {
1113 // Invariant: we have a 4-byte match at s, and no need to emit any
1114 // literal bytes prior to s.
1115 base := s
1116 offset := s + (len(dict.dict)) - candidateL
1117
1118 // Extend the 4-byte match as long as possible.
1119 s += 4
1120 candidateL += 4
1121 for s <= len(src)-8 && len(dict.dict)-candidateL >= 8 {
1122 if diff := load64(src, s) ^ load64(dict.dict, candidateL); diff != 0 {
1123 s += bits.TrailingZeros64(diff) >> 3
1124 break
1125 }
1126 s += 8
1127 candidateL += 8
1128 }
1129
1130 if repeat == offset {
1131 if debug {
1132 fmt.Println("emitted dict repeat, length", s-base, "offset:", offset, "s:", s, "dict offset:", candidateL)
1133 }
1134 d += emitRepeat(dst[d:], offset, s-base)
1135 } else {
1136 if debug {
1137 fmt.Println("emitted dict copy, length", s-base, "offset:", offset, "s:", s, "dict offset:", candidateL)
1138 }
1139 // Matches longer than 64 are split.
1140 if s <= sLimit || s-base < 8 {
1141 d += emitCopy(dst[d:], offset, s-base)
1142 } else {
1143 // Split to ensure we don't start a copy within next block.
1144 d += emitCopy(dst[d:], offset, 4)
1145 d += emitRepeat(dst[d:], offset, s-base-4)
1146 }
1147 repeat = offset
1148 }
1149 if false {
1150 // Validate match.
1151 if s <= candidateL {
1152 panic("s <= candidate")
1153 }
1154 a := src[base:s]
1155 b := dict.dict[base-repeat : base-repeat+(s-base)]
1156 if !bytes.Equal(a, b) {
1157 panic("mismatch")
1158 }
1159 }
1160
1161 nextEmit = s
1162 if s >= sLimit {
1163 break searchDict
1164 }
1165
1166 if d > dstLimit {
1167 // Do we have space for more, if not bail.
1168 return 0
1169 }
1170
1171 // Index short & long
1172 index0 := base + 1
1173 index1 := s - 2
1174
1175 cv0 := load64(src, index0)
1176 cv1 := load64(src, index1)
1177 lTable[hash7(cv0, lTableBits)] = uint32(index0)
1178 sTable[hash4(cv0>>8, sTableBits)] = uint32(index0 + 1)
1179
1180 lTable[hash7(cv1, lTableBits)] = uint32(index1)
1181 sTable[hash4(cv1>>8, sTableBits)] = uint32(index1 + 1)
1182 index0 += 1
1183 index1 -= 1
1184 cv = load64(src, s)
1185
1186 // index every second long in between.
1187 for index0 < index1 {
1188 lTable[hash7(load64(src, index0), lTableBits)] = uint32(index0)
1189 lTable[hash7(load64(src, index1), lTableBits)] = uint32(index1)
1190 index0 += 2
1191 index1 -= 2
1192 }
1193 }
1194 continue
1195 }
1196 emitMatch:
1197
1198 // Extend backwards
1199 for candidateL > 0 && s > nextEmit && src[candidateL-1] == src[s-1] {
1200 candidateL--
1201 s--
1202 }
1203
1204 // Bail if we exceed the maximum size.
1205 if d+(s-nextEmit) > dstLimit {
1206 return 0
1207 }
1208
1209 base := s
1210 offset := base - candidateL
1211
1212 // Extend the 4-byte match as long as possible.
1213 s += 4
1214 candidateL += 4
1215 for s < len(src) {
1216 if len(src)-s < 8 {
1217 if src[s] == src[candidateL] {
1218 s++
1219 candidateL++
1220 continue
1221 }
1222 break
1223 }
1224 if diff := load64(src, s) ^ load64(src, candidateL); diff != 0 {
1225 s += bits.TrailingZeros64(diff) >> 3
1226 break
1227 }
1228 s += 8
1229 candidateL += 8
1230 }
1231
1232 if offset > 65535 && s-base <= 5 && repeat != offset {
1233 // Bail if the match is equal or worse to the encoding.
1234 s = nextS + 1
1235 if s >= sLimit {
1236 goto emitRemainder
1237 }
1238 cv = load64(src, s)
1239 continue
1240 }
1241
1242 d += emitLiteral(dst[d:], src[nextEmit:base])
1243 if debug && nextEmit != s {
1244 fmt.Println("emitted ", s-nextEmit, "literals")
1245 }
1246 if repeat == offset {
1247 if debug {
1248 fmt.Println("emitted match repeat, length", s-base, "offset:", offset, "s:", s)
1249 }
1250 d += emitRepeat(dst[d:], offset, s-base)
1251 } else {
1252 if debug {
1253 fmt.Println("emitted match copy, length", s-base, "offset:", offset, "s:", s)
1254 }
1255 d += emitCopy(dst[d:], offset, s-base)
1256 repeat = offset
1257 }
1258
1259 nextEmit = s
1260 if s >= sLimit {
1261 goto emitRemainder
1262 }
1263
1264 if d > dstLimit {
1265 // Do we have space for more, if not bail.
1266 return 0
1267 }
1268
1269 // Index short & long
1270 index0 := base + 1
1271 index1 := s - 2
1272
1273 cv0 := load64(src, index0)
1274 cv1 := load64(src, index1)
1275 lTable[hash7(cv0, lTableBits)] = uint32(index0)
1276 sTable[hash4(cv0>>8, sTableBits)] = uint32(index0 + 1)
1277
1278 lTable[hash7(cv1, lTableBits)] = uint32(index1)
1279 sTable[hash4(cv1>>8, sTableBits)] = uint32(index1 + 1)
1280 index0 += 1
1281 index1 -= 1
1282 cv = load64(src, s)
1283
1284 // Index large values sparsely in between.
1285 // We do two starting from different offsets for speed.
1286 index2 := (index0 + index1 + 1) >> 1
1287 for index2 < index1 {
1288 lTable[hash7(load64(src, index0), lTableBits)] = uint32(index0)
1289 lTable[hash7(load64(src, index2), lTableBits)] = uint32(index2)
1290 index0 += 2
1291 index2 += 2
1292 }
1293 }
1294
1295 // Search without dict:
1296 if repeat > s {
1297 repeat = 0
1298 }
1299
1300 // No more dict
1301 sLimit = len(src) - inputMargin
1302 if s >= sLimit {
1303 goto emitRemainder
1304 }
1305 cv = load64(src, s)
1306 if debug {
1307 fmt.Println("now", s, "->", sLimit, "out:", d, "left:", len(src)-s, "nextemit:", nextEmit, "dstLimit:", dstLimit, "s:", s)
1308 }
1309 for {
1310 candidateL := 0
1311 nextS := 0
1312 for {
1313 // Next src position to check
1314 nextS = s + (s-nextEmit)>>7 + 1
1315 if nextS > sLimit {
1316 goto emitRemainder
1317 }
1318 hashL := hash7(cv, lTableBits)
1319 hashS := hash4(cv, sTableBits)
1320 candidateL = int(lTable[hashL])
1321 candidateS := int(sTable[hashS])
1322 lTable[hashL] = uint32(s)
1323 sTable[hashS] = uint32(s)
1324
1325 valLong := load64(src, candidateL)
1326 valShort := load64(src, candidateS)
1327
1328 // If long matches at least 8 bytes, use that.
1329 if cv == valLong {
1330 break
1331 }
1332 if cv == valShort {
1333 candidateL = candidateS
1334 break
1335 }
1336
1337 // Check repeat at offset checkRep.
1338 const checkRep = 1
1339 // Minimum length of a repeat. Tested with various values.
1340 // While 4-5 offers improvements in some, 6 reduces
1341 // regressions significantly.
1342 const wantRepeatBytes = 6
1343 const repeatMask = ((1 << (wantRepeatBytes * 8)) - 1) << (8 * checkRep)
1344 if false && repeat > 0 && cv&repeatMask == load64(src, s-repeat)&repeatMask {
1345 base := s + checkRep
1346 // Extend back
1347 for i := base - repeat; base > nextEmit && i > 0 && src[i-1] == src[base-1]; {
1348 i--
1349 base--
1350 }
1351 d += emitLiteral(dst[d:], src[nextEmit:base])
1352
1353 // Extend forward
1354 candidate := s - repeat + wantRepeatBytes + checkRep
1355 s += wantRepeatBytes + checkRep
1356 for s < len(src) {
1357 if len(src)-s < 8 {
1358 if src[s] == src[candidate] {
1359 s++
1360 candidate++
1361 continue
1362 }
1363 break
1364 }
1365 if diff := load64(src, s) ^ load64(src, candidate); diff != 0 {
1366 s += bits.TrailingZeros64(diff) >> 3
1367 break
1368 }
1369 s += 8
1370 candidate += 8
1371 }
1372 // same as `add := emitCopy(dst[d:], repeat, s-base)` but skips storing offset.
1373 d += emitRepeat(dst[d:], repeat, s-base)
1374 nextEmit = s
1375 if s >= sLimit {
1376 goto emitRemainder
1377 }
1378 // Index in-between
1379 index0 := base + 1
1380 index1 := s - 2
1381
1382 for index0 < index1 {
1383 cv0 := load64(src, index0)
1384 cv1 := load64(src, index1)
1385 lTable[hash7(cv0, lTableBits)] = uint32(index0)
1386 sTable[hash4(cv0>>8, sTableBits)] = uint32(index0 + 1)
1387
1388 lTable[hash7(cv1, lTableBits)] = uint32(index1)
1389 sTable[hash4(cv1>>8, sTableBits)] = uint32(index1 + 1)
1390 index0 += 2
1391 index1 -= 2
1392 }
1393
1394 cv = load64(src, s)
1395 continue
1396 }
1397
1398 // Long likely matches 7, so take that.
1399 if uint32(cv) == uint32(valLong) {
1400 break
1401 }
1402
1403 // Check our short candidate
1404 if uint32(cv) == uint32(valShort) {
1405 // Try a long candidate at s+1
1406 hashL = hash7(cv>>8, lTableBits)
1407 candidateL = int(lTable[hashL])
1408 lTable[hashL] = uint32(s + 1)
1409 if uint32(cv>>8) == load32(src, candidateL) {
1410 s++
1411 break
1412 }
1413 // Use our short candidate.
1414 candidateL = candidateS
1415 break
1416 }
1417
1418 cv = load64(src, nextS)
1419 s = nextS
1420 }
1421
1422 // Extend backwards
1423 for candidateL > 0 && s > nextEmit && src[candidateL-1] == src[s-1] {
1424 candidateL--
1425 s--
1426 }
1427
1428 // Bail if we exceed the maximum size.
1429 if d+(s-nextEmit) > dstLimit {
1430 return 0
1431 }
1432
1433 base := s
1434 offset := base - candidateL
1435
1436 // Extend the 4-byte match as long as possible.
1437 s += 4
1438 candidateL += 4
1439 for s < len(src) {
1440 if len(src)-s < 8 {
1441 if src[s] == src[candidateL] {
1442 s++
1443 candidateL++
1444 continue
1445 }
1446 break
1447 }
1448 if diff := load64(src, s) ^ load64(src, candidateL); diff != 0 {
1449 s += bits.TrailingZeros64(diff) >> 3
1450 break
1451 }
1452 s += 8
1453 candidateL += 8
1454 }
1455
1456 if offset > 65535 && s-base <= 5 && repeat != offset {
1457 // Bail if the match is equal or worse to the encoding.
1458 s = nextS + 1
1459 if s >= sLimit {
1460 goto emitRemainder
1461 }
1462 cv = load64(src, s)
1463 continue
1464 }
1465
1466 d += emitLiteral(dst[d:], src[nextEmit:base])
1467 if repeat == offset {
1468 d += emitRepeat(dst[d:], offset, s-base)
1469 } else {
1470 d += emitCopy(dst[d:], offset, s-base)
1471 repeat = offset
1472 }
1473
1474 nextEmit = s
1475 if s >= sLimit {
1476 goto emitRemainder
1477 }
1478
1479 if d > dstLimit {
1480 // Do we have space for more, if not bail.
1481 return 0
1482 }
1483
1484 // Index short & long
1485 index0 := base + 1
1486 index1 := s - 2
1487
1488 cv0 := load64(src, index0)
1489 cv1 := load64(src, index1)
1490 lTable[hash7(cv0, lTableBits)] = uint32(index0)
1491 sTable[hash4(cv0>>8, sTableBits)] = uint32(index0 + 1)
1492
1493 lTable[hash7(cv1, lTableBits)] = uint32(index1)
1494 sTable[hash4(cv1>>8, sTableBits)] = uint32(index1 + 1)
1495 index0 += 1
1496 index1 -= 1
1497 cv = load64(src, s)
1498
1499 // Index large values sparsely in between.
1500 // We do two starting from different offsets for speed.
1501 index2 := (index0 + index1 + 1) >> 1
1502 for index2 < index1 {
1503 lTable[hash7(load64(src, index0), lTableBits)] = uint32(index0)
1504 lTable[hash7(load64(src, index2), lTableBits)] = uint32(index2)
1505 index0 += 2
1506 index2 += 2
1507 }
1508 }
1509
1510 emitRemainder:
1511 if nextEmit < len(src) {
1512 // Bail if we exceed the maximum size.
1513 if d+len(src)-nextEmit > dstLimit {
1514 return 0
1515 }
1516 d += emitLiteral(dst[d:], src[nextEmit:])
1517 }
1518 return d
1519 }