Debian Codegraph @

s2/encode_best.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 "fmt"
10 "math"
11 "math/bits"
12 )
13
14 // encodeBlockBest encodes a non-empty src to a guaranteed-large-enough dst. It
15 // assumes that the varint-encoded length of the decompressed bytes has already
16 // been written.
17 //
18 // It also assumes that:
19 //
20 // len(dst) >= MaxEncodedLen(len(src)) &&
21 // minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize
22 func encodeBlockBest(dst, src []byte, dict *Dict) (d int) {
23 // Initialize the hash tables.
24 const (
25 // Long hash matches.
26 lTableBits = bestLongTableBits
27 maxLTableSize = bestLongTableSize
28
29 // Short hash matches.
30 sTableBits = bestShortTableBits
31 maxSTableSize = bestShortTableSize
32
33 inputMargin = 8 + 2
34
35 debug = false
36 )
37
38 // sLimit is when to stop looking for offset/length copies. The inputMargin
39 // lets us use a fast path for emitLiteral in the main loop, while we are
40 // looking for copies.
41 sLimit := len(src) - inputMargin
42 if len(src) < minNonLiteralBlockSize {
43 return 0
44 }
45 sLimitDict := min(len(src)-inputMargin, MaxDictSrcOffset-inputMargin)
46
47 tbl := getBestTables()
48 lTable := &tbl.lTable
49 sTable := &tbl.sTable
50 defer bestTablePool.Put(tbl)
51
52 // Bail if we can't compress to at least this.
53 dstLimit := len(src) - 5
54
55 // nextEmit is where in src the next emitLiteral should start from.
56 nextEmit := 0
57
58 // The encoded form must start with a literal, as there are no previous
59 // bytes to copy, so we start looking for hash matches at s == 1.
60 s := 1
61 repeat := 1
62 if dict != nil {
63 dict.initBest()
64 s = 0
65 repeat = len(dict.dict) - dict.repeat
66 }
67 cv := load64(src, s)
68
69 // We search for a repeat at -1, but don't output repeats when nextEmit == 0
70 const lowbitMask = 0xffffffff
71 getCur := func(x uint64) int {
72 return int(x & lowbitMask)
73 }
74 getPrev := func(x uint64) int {
75 return int(x >> 32)
76 }
77 const maxSkip = 64
78
79 for {
80 type match struct {
81 offset int
82 s int
83 length int
84 score int
85 rep, dict bool
86 }
87 var best match
88 for {
89 // Next src position to check
90 nextS := (s-nextEmit)>>8 + 1
91 if nextS > maxSkip {
92 nextS = s + maxSkip
93 } else {
94 nextS += s
95 }
96 if nextS > sLimit {
97 goto emitRemainder
98 }
99 if dict != nil && s >= MaxDictSrcOffset {
100 dict = nil
101 if repeat > s {
102 repeat = math.MinInt32
103 }
104 }
105 hashL := hash8(cv, lTableBits)
106 hashS := hash4(cv, sTableBits)
107 candidateL := lTable[hashL]
108 candidateS := sTable[hashS]
109
110 score := func(m match) int {
111 // Matches that are longer forward are penalized since we must emit it as a literal.
112 score := m.length - m.s
113 if nextEmit == m.s {
114 // If we do not have to emit literals, we save 1 byte
115 score++
116 }
117 offset := m.s - m.offset
118 if m.rep {
119 return score - emitRepeatSize(offset, m.length)
120 }
121 return score - emitCopySize(offset, m.length)
122 }
123
124 matchAt := func(offset, s int, first uint32, rep bool) match {
125 if best.length != 0 && best.s-best.offset == s-offset {
126 // Don't retest if we have the same offset.
127 return match{offset: offset, s: s}
128 }
129 if load32(src, offset) != first {
130 return match{offset: offset, s: s}
131 }
132 m := match{offset: offset, s: s, length: 4 + offset, rep: rep}
133 s += 4
134 for s < len(src) {
135 if len(src)-s < 8 {
136 if src[s] == src[m.length] {
137 m.length++
138 s++
139 continue
140 }
141 break
142 }
143 if diff := load64(src, s) ^ load64(src, m.length); diff != 0 {
144 m.length += bits.TrailingZeros64(diff) >> 3
145 break
146 }
147 s += 8
148 m.length += 8
149 }
150 m.length -= offset
151 m.score = score(m)
152 if m.score <= -m.s {
153 // Eliminate if no savings, we might find a better one.
154 m.length = 0
155 }
156 return m
157 }
158 matchDict := func(candidate, s int, first uint32, rep bool) match {
159 if s >= MaxDictSrcOffset {
160 return match{offset: candidate, s: s}
161 }
162 // Calculate offset as if in continuous array with s
163 offset := -len(dict.dict) + candidate
164 if best.length != 0 && best.s-best.offset == s-offset && !rep {
165 // Don't retest if we have the same offset.
166 return match{offset: offset, s: s}
167 }
168
169 if load32(dict.dict, candidate) != first {
170 return match{offset: offset, s: s}
171 }
172 m := match{offset: offset, s: s, length: 4 + candidate, rep: rep, dict: true}
173 s += 4
174 if !rep {
175 for s < sLimitDict && m.length < len(dict.dict) {
176 if len(src)-s < 8 || len(dict.dict)-m.length < 8 {
177 if src[s] == dict.dict[m.length] {
178 m.length++
179 s++
180 continue
181 }
182 break
183 }
184 if diff := load64(src, s) ^ load64(dict.dict, m.length); diff != 0 {
185 m.length += bits.TrailingZeros64(diff) >> 3
186 break
187 }
188 s += 8
189 m.length += 8
190 }
191 } else {
192 for s < len(src) && m.length < len(dict.dict) {
193 if len(src)-s < 8 || len(dict.dict)-m.length < 8 {
194 if src[s] == dict.dict[m.length] {
195 m.length++
196 s++
197 continue
198 }
199 break
200 }
201 if diff := load64(src, s) ^ load64(dict.dict, m.length); diff != 0 {
202 m.length += bits.TrailingZeros64(diff) >> 3
203 break
204 }
205 s += 8
206 m.length += 8
207 }
208 }
209 m.length -= candidate
210 m.score = score(m)
211 if m.score <= -m.s {
212 // Eliminate if no savings, we might find a better one.
213 m.length = 0
214 }
215 return m
216 }
217
218 bestOf := func(a, b match) match {
219 if b.length == 0 {
220 return a
221 }
222 if a.length == 0 {
223 return b
224 }
225 as := a.score + b.s
226 bs := b.score + a.s
227 if as >= bs {
228 return a
229 }
230 return b
231 }
232
233 if s > 0 {
234 best = bestOf(matchAt(getCur(candidateL), s, uint32(cv), false), matchAt(getPrev(candidateL), s, uint32(cv), false))
235 best = bestOf(best, matchAt(getCur(candidateS), s, uint32(cv), false))
236 best = bestOf(best, matchAt(getPrev(candidateS), s, uint32(cv), false))
237 }
238 if dict != nil {
239 candidateL := dict.bestTableLong[hashL]
240 candidateS := dict.bestTableShort[hashS]
241 best = bestOf(best, matchDict(int(candidateL&0xffff), s, uint32(cv), false))
242 best = bestOf(best, matchDict(int(candidateL>>16), s, uint32(cv), false))
243 best = bestOf(best, matchDict(int(candidateS&0xffff), s, uint32(cv), false))
244 best = bestOf(best, matchDict(int(candidateS>>16), s, uint32(cv), false))
245 }
246 {
247 if (dict == nil || repeat <= s) && repeat > 0 {
248 best = bestOf(best, matchAt(s-repeat+1, s+1, uint32(cv>>8), true))
249 } else if s-repeat < -4 && dict != nil {
250 candidate := len(dict.dict) - (repeat - s)
251 best = bestOf(best, matchDict(candidate, s, uint32(cv), true))
252 candidate++
253 best = bestOf(best, matchDict(candidate, s+1, uint32(cv>>8), true))
254 }
255
256 if best.length > 0 {
257 hashS := hash4(cv>>8, sTableBits)
258 // s+1
259 nextShort := sTable[hashS]
260 s := s + 1
261 cv := load64(src, s)
262 hashL := hash8(cv, lTableBits)
263 nextLong := lTable[hashL]
264 best = bestOf(best, matchAt(getCur(nextShort), s, uint32(cv), false))
265 best = bestOf(best, matchAt(getPrev(nextShort), s, uint32(cv), false))
266 best = bestOf(best, matchAt(getCur(nextLong), s, uint32(cv), false))
267 best = bestOf(best, matchAt(getPrev(nextLong), s, uint32(cv), false))
268
269 // Dict at + 1
270 if dict != nil {
271 candidateL := dict.bestTableLong[hashL]
272 candidateS := dict.bestTableShort[hashS]
273
274 best = bestOf(best, matchDict(int(candidateL&0xffff), s, uint32(cv), false))
275 best = bestOf(best, matchDict(int(candidateS&0xffff), s, uint32(cv), false))
276 }
277
278 // s+2
279 if true {
280 hashS := hash4(cv>>8, sTableBits)
281
282 nextShort = sTable[hashS]
283 s++
284 cv = load64(src, s)
285 hashL := hash8(cv, lTableBits)
286 nextLong = lTable[hashL]
287
288 if (dict == nil || repeat <= s) && repeat > 0 {
289 // Repeat at + 2
290 best = bestOf(best, matchAt(s-repeat, s, uint32(cv), true))
291 } else if repeat-s > 4 && dict != nil {
292 candidate := len(dict.dict) - (repeat - s)
293 best = bestOf(best, matchDict(candidate, s, uint32(cv), true))
294 }
295 best = bestOf(best, matchAt(getCur(nextShort), s, uint32(cv), false))
296 best = bestOf(best, matchAt(getPrev(nextShort), s, uint32(cv), false))
297 best = bestOf(best, matchAt(getCur(nextLong), s, uint32(cv), false))
298 best = bestOf(best, matchAt(getPrev(nextLong), s, uint32(cv), false))
299
300 // Dict at +2
301 // Very small gain
302 if dict != nil {
303 candidateL := dict.bestTableLong[hashL]
304 candidateS := dict.bestTableShort[hashS]
305
306 best = bestOf(best, matchDict(int(candidateL&0xffff), s, uint32(cv), false))
307 best = bestOf(best, matchDict(int(candidateS&0xffff), s, uint32(cv), false))
308 }
309 }
310 // Search for a match at best match end, see if that is better.
311 // Allow some bytes at the beginning to mismatch.
312 // Sweet spot is around 1-2 bytes, but depends on input.
313 // The skipped bytes are tested in Extend backwards,
314 // and still picked up as part of the match if they do.
315 const skipBeginning = 2
316 const skipEnd = 1
317 if sAt := best.s + best.length - skipEnd; sAt < sLimit {
318
319 sBack := best.s + skipBeginning - skipEnd
320 backL := best.length - skipBeginning
321 // Load initial values
322 cv = load64(src, sBack)
323
324 // Grab candidates...
325 next := lTable[hash8(load64(src, sAt), lTableBits)]
326
327 if checkAt := getCur(next) - backL; checkAt > 0 {
328 best = bestOf(best, matchAt(checkAt, sBack, uint32(cv), false))
329 }
330 if checkAt := getPrev(next) - backL; checkAt > 0 {
331 best = bestOf(best, matchAt(checkAt, sBack, uint32(cv), false))
332 }
333 // Disabled: Extremely small gain
334 if false {
335 next = sTable[hash4(load64(src, sAt), sTableBits)]
336 if checkAt := getCur(next) - backL; checkAt > 0 {
337 best = bestOf(best, matchAt(checkAt, sBack, uint32(cv), false))
338 }
339 if checkAt := getPrev(next) - backL; checkAt > 0 {
340 best = bestOf(best, matchAt(checkAt, sBack, uint32(cv), false))
341 }
342 }
343 }
344 }
345 }
346
347 // Update table
348 lTable[hashL] = uint64(s) | candidateL<<32
349 sTable[hashS] = uint64(s) | candidateS<<32
350
351 if best.length > 0 {
352 break
353 }
354
355 cv = load64(src, nextS)
356 s = nextS
357 }
358
359 // Extend backwards, not needed for repeats...
360 s = best.s
361 if !best.rep && !best.dict {
362 for best.offset > 0 && s > nextEmit && src[best.offset-1] == src[s-1] {
363 best.offset--
364 best.length++
365 s--
366 }
367 }
368 if false && best.offset >= s {
369 panic(fmt.Errorf("t %d >= s %d", best.offset, s))
370 }
371 // Bail if we exceed the maximum size.
372 if d+(s-nextEmit) > dstLimit {
373 return 0
374 }
375
376 base := s
377 offset := s - best.offset
378 s += best.length
379
380 if offset > 65535 && s-base <= 5 && !best.rep {
381 // Bail if the match is equal or worse to the encoding.
382 s = best.s + 1
383 if s >= sLimit {
384 goto emitRemainder
385 }
386 cv = load64(src, s)
387 continue
388 }
389 if debug && nextEmit != base {
390 fmt.Println("EMIT", base-nextEmit, "literals. base-after:", base)
391 }
392 d += emitLiteral(dst[d:], src[nextEmit:base])
393 if best.rep {
394 if nextEmit > 0 || best.dict {
395 if debug {
396 fmt.Println("REPEAT, length", best.length, "offset:", offset, "s-after:", s, "dict:", best.dict, "best:", best)
397 }
398 // same as `add := emitCopy(dst[d:], repeat, s-base)` but skips storing offset.
399 d += emitRepeat(dst[d:], offset, best.length)
400 } else {
401 // First match without dict cannot be a repeat.
402 if debug {
403 fmt.Println("COPY, length", best.length, "offset:", offset, "s-after:", s, "dict:", best.dict, "best:", best)
404 }
405 d += emitCopy(dst[d:], offset, best.length)
406 }
407 } else {
408 if debug {
409 fmt.Println("COPY, length", best.length, "offset:", offset, "s-after:", s, "dict:", best.dict, "best:", best)
410 }
411 d += emitCopy(dst[d:], offset, best.length)
412 }
413 repeat = offset
414
415 nextEmit = s
416 if s >= sLimit {
417 goto emitRemainder
418 }
419
420 if d > dstLimit {
421 // Do we have space for more, if not bail.
422 return 0
423 }
424 // Fill tables...
425 for i := best.s + 1; i < s; i++ {
426 cv0 := load64(src, i)
427 long0 := hash8(cv0, lTableBits)
428 short0 := hash4(cv0, sTableBits)
429 lTable[long0] = uint64(i) | lTable[long0]<<32
430 sTable[short0] = uint64(i) | sTable[short0]<<32
431 }
432 cv = load64(src, s)
433 }
434
435 emitRemainder:
436 if nextEmit < len(src) {
437 // Bail if we exceed the maximum size.
438 if d+len(src)-nextEmit > dstLimit {
439 return 0
440 }
441 if debug && nextEmit != s {
442 fmt.Println("emitted ", len(src)-nextEmit, "literals")
443 }
444 d += emitLiteral(dst[d:], src[nextEmit:])
445 }
446 return d
447 }
448
449 // encodeBlockBestSnappy encodes a non-empty src to a guaranteed-large-enough dst. It
450 // assumes that the varint-encoded length of the decompressed bytes has already
451 // been written.
452 //
453 // It also assumes that:
454 //
455 // len(dst) >= MaxEncodedLen(len(src)) &&
456 // minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize
457 func encodeBlockBestSnappy(dst, src []byte) (d int) {
458 // Initialize the hash tables.
459 const (
460 // Long hash matches.
461 lTableBits = bestLongTableBits
462 maxLTableSize = bestLongTableSize
463
464 // Short hash matches.
465 sTableBits = bestShortTableBits
466 maxSTableSize = bestShortTableSize
467
468 inputMargin = 8 + 2
469 )
470
471 // sLimit is when to stop looking for offset/length copies. The inputMargin
472 // lets us use a fast path for emitLiteral in the main loop, while we are
473 // looking for copies.
474 sLimit := len(src) - inputMargin
475 if len(src) < minNonLiteralBlockSize {
476 return 0
477 }
478
479 tbl := getBestTables()
480 lTable := &tbl.lTable
481 sTable := &tbl.sTable
482 defer bestTablePool.Put(tbl)
483
484 // Bail if we can't compress to at least this.
485 dstLimit := len(src) - 5
486
487 // nextEmit is where in src the next emitLiteral should start from.
488 nextEmit := 0
489
490 // The encoded form must start with a literal, as there are no previous
491 // bytes to copy, so we start looking for hash matches at s == 1.
492 s := 1
493 cv := load64(src, s)
494
495 // We search for a repeat at -1, but don't output repeats when nextEmit == 0
496 repeat := 1
497 const lowbitMask = 0xffffffff
498 getCur := func(x uint64) int {
499 return int(x & lowbitMask)
500 }
501 getPrev := func(x uint64) int {
502 return int(x >> 32)
503 }
504 const maxSkip = 64
505
506 for {
507 type match struct {
508 offset int
509 s int
510 length int
511 score int
512 }
513 var best match
514 for {
515 // Next src position to check
516 nextS := (s-nextEmit)>>8 + 1
517 if nextS > maxSkip {
518 nextS = s + maxSkip
519 } else {
520 nextS += s
521 }
522 if nextS > sLimit {
523 goto emitRemainder
524 }
525 hashL := hash8(cv, lTableBits)
526 hashS := hash4(cv, sTableBits)
527 candidateL := lTable[hashL]
528 candidateS := sTable[hashS]
529
530 score := func(m match) int {
531 // Matches that are longer forward are penalized since we must emit it as a literal.
532 score := m.length - m.s
533 if nextEmit == m.s {
534 // If we do not have to emit literals, we save 1 byte
535 score++
536 }
537 offset := m.s - m.offset
538
539 return score - emitCopyNoRepeatSize(offset, m.length)
540 }
541
542 matchAt := func(offset, s int, first uint32) match {
543 if best.length != 0 && best.s-best.offset == s-offset {
544 // Don't retest if we have the same offset.
545 return match{offset: offset, s: s}
546 }
547 if load32(src, offset) != first {
548 return match{offset: offset, s: s}
549 }
550 m := match{offset: offset, s: s, length: 4 + offset}
551 s += 4
552 for s <= sLimit {
553 if diff := load64(src, s) ^ load64(src, m.length); diff != 0 {
554 m.length += bits.TrailingZeros64(diff) >> 3
555 break
556 }
557 s += 8
558 m.length += 8
559 }
560 m.length -= offset
561 m.score = score(m)
562 if m.score <= -m.s {
563 // Eliminate if no savings, we might find a better one.
564 m.length = 0
565 }
566 return m
567 }
568
569 bestOf := func(a, b match) match {
570 if b.length == 0 {
571 return a
572 }
573 if a.length == 0 {
574 return b
575 }
576 as := a.score + b.s
577 bs := b.score + a.s
578 if as >= bs {
579 return a
580 }
581 return b
582 }
583
584 best = bestOf(matchAt(getCur(candidateL), s, uint32(cv)), matchAt(getPrev(candidateL), s, uint32(cv)))
585 best = bestOf(best, matchAt(getCur(candidateS), s, uint32(cv)))
586 best = bestOf(best, matchAt(getPrev(candidateS), s, uint32(cv)))
587
588 {
589 best = bestOf(best, matchAt(s-repeat+1, s+1, uint32(cv>>8)))
590 if best.length > 0 {
591 // s+1
592 nextShort := sTable[hash4(cv>>8, sTableBits)]
593 s := s + 1
594 cv := load64(src, s)
595 nextLong := lTable[hash8(cv, lTableBits)]
596 best = bestOf(best, matchAt(getCur(nextShort), s, uint32(cv)))
597 best = bestOf(best, matchAt(getPrev(nextShort), s, uint32(cv)))
598 best = bestOf(best, matchAt(getCur(nextLong), s, uint32(cv)))
599 best = bestOf(best, matchAt(getPrev(nextLong), s, uint32(cv)))
600 // Repeat at + 2
601 best = bestOf(best, matchAt(s-repeat+1, s+1, uint32(cv>>8)))
602
603 // s+2
604 if true {
605 nextShort = sTable[hash4(cv>>8, sTableBits)]
606 s++
607 cv = load64(src, s)
608 nextLong = lTable[hash8(cv, lTableBits)]
609 best = bestOf(best, matchAt(getCur(nextShort), s, uint32(cv)))
610 best = bestOf(best, matchAt(getPrev(nextShort), s, uint32(cv)))
611 best = bestOf(best, matchAt(getCur(nextLong), s, uint32(cv)))
612 best = bestOf(best, matchAt(getPrev(nextLong), s, uint32(cv)))
613 }
614 // Search for a match at best match end, see if that is better.
615 if sAt := best.s + best.length; sAt < sLimit {
616 sBack := best.s
617 backL := best.length
618 // Load initial values
619 cv = load64(src, sBack)
620 // Search for mismatch
621 next := lTable[hash8(load64(src, sAt), lTableBits)]
622 //next := sTable[hash4(load64(src, sAt), sTableBits)]
623
624 if checkAt := getCur(next) - backL; checkAt > 0 {
625 best = bestOf(best, matchAt(checkAt, sBack, uint32(cv)))
626 }
627 if checkAt := getPrev(next) - backL; checkAt > 0 {
628 best = bestOf(best, matchAt(checkAt, sBack, uint32(cv)))
629 }
630 }
631 }
632 }
633
634 // Update table
635 lTable[hashL] = uint64(s) | candidateL<<32
636 sTable[hashS] = uint64(s) | candidateS<<32
637
638 if best.length > 0 {
639 break
640 }
641
642 cv = load64(src, nextS)
643 s = nextS
644 }
645
646 // Extend backwards, not needed for repeats...
647 s = best.s
648 if true {
649 for best.offset > 0 && s > nextEmit && src[best.offset-1] == src[s-1] {
650 best.offset--
651 best.length++
652 s--
653 }
654 }
655 if false && best.offset >= s {
656 panic(fmt.Errorf("t %d >= s %d", best.offset, s))
657 }
658 // Bail if we exceed the maximum size.
659 if d+(s-nextEmit) > dstLimit {
660 return 0
661 }
662
663 base := s
664 offset := s - best.offset
665
666 s += best.length
667
668 if offset > 65535 && s-base <= 5 {
669 // Bail if the match is equal or worse to the encoding.
670 s = best.s + 1
671 if s >= sLimit {
672 goto emitRemainder
673 }
674 cv = load64(src, s)
675 continue
676 }
677 d += emitLiteral(dst[d:], src[nextEmit:base])
678 d += emitCopyNoRepeat(dst[d:], offset, best.length)
679 repeat = offset
680
681 nextEmit = s
682 if s >= sLimit {
683 goto emitRemainder
684 }
685
686 if d > dstLimit {
687 // Do we have space for more, if not bail.
688 return 0
689 }
690 // Fill tables...
691 for i := best.s + 1; i < s; i++ {
692 cv0 := load64(src, i)
693 long0 := hash8(cv0, lTableBits)
694 short0 := hash4(cv0, sTableBits)
695 lTable[long0] = uint64(i) | lTable[long0]<<32
696 sTable[short0] = uint64(i) | sTable[short0]<<32
697 }
698 cv = load64(src, s)
699 }
700
701 emitRemainder:
702 if nextEmit < len(src) {
703 // Bail if we exceed the maximum size.
704 if d+len(src)-nextEmit > dstLimit {
705 return 0
706 }
707 d += emitLiteral(dst[d:], src[nextEmit:])
708 }
709 return d
710 }
711
712 // emitCopySize returns the size to encode the offset+length
713 //
714 // It assumes that:
715 //
716 // 1 <= offset && offset <= math.MaxUint32
717 // 4 <= length && length <= 1 << 24
718 func emitCopySize(offset, length int) int {
719 if offset >= 65536 {
720 i := 0
721 if length > 64 {
722 length -= 64
723 if length >= 4 {
724 // Emit remaining as repeats
725 return 5 + emitRepeatSize(offset, length)
726 }
727 i = 5
728 }
729 if length == 0 {
730 return i
731 }
732 return i + 5
733 }
734
735 // Offset no more than 2 bytes.
736 if length > 64 {
737 if offset < 2048 {
738 // Emit 8 bytes, then rest as repeats...
739 return 2 + emitRepeatSize(offset, length-8)
740 }
741 // Emit remaining as repeats, at least 4 bytes remain.
742 return 3 + emitRepeatSize(offset, length-60)
743 }
744 if length >= 12 || offset >= 2048 {
745 return 3
746 }
747 // Emit the remaining copy, encoded as 2 bytes.
748 return 2
749 }
750
751 // emitCopyNoRepeatSize returns the size to encode the offset+length
752 //
753 // It assumes that:
754 //
755 // 1 <= offset && offset <= math.MaxUint32
756 // 4 <= length && length <= 1 << 24
757 func emitCopyNoRepeatSize(offset, length int) int {
758 if offset >= 65536 {
759 return 5 + 5*(length/64)
760 }
761
762 // Offset no more than 2 bytes.
763 if length > 64 {
764 // Emit remaining as repeats, at least 4 bytes remain.
765 return 3 + 3*(length/60)
766 }
767 if length >= 12 || offset >= 2048 {
768 return 3
769 }
770 // Emit the remaining copy, encoded as 2 bytes.
771 return 2
772 }
773
774 // emitRepeatSize returns the number of bytes required to encode a repeat.
775 // Length must be at least 4 and < 1<<24
776 func emitRepeatSize(offset, length int) int {
777 // Repeat offset, make length cheaper
778 if length <= 4+4 || (length < 8+4 && offset < 2048) {
779 return 2
780 }
781 if length < (1<<8)+4+4 {
782 return 3
783 }
784 if length < (1<<16)+(1<<8)+4 {
785 return 4
786 }
787 const maxRepeat = (1 << 24) - 1
788 length -= (1 << 16) - 4
789 left := 0
790 if length > maxRepeat {
791 left = length - maxRepeat + 4
792 }
793 if left > 0 {
794 return 5 + emitRepeatSize(offset, left)
795 }
796 return 5
797 }