level2.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package flate
  2. import "fmt"
  3. // fastGen maintains the table for matches,
  4. // and the previous byte block for level 2.
  5. // This is the generic implementation.
  6. type fastEncL2 struct {
  7. fastGen
  8. table [bTableSize]tableEntry
  9. }
  10. // EncodeL2 uses a similar algorithm to level 1, but is capable
  11. // of matching across blocks giving better compression at a small slowdown.
  12. func (e *fastEncL2) Encode(dst *tokens, src []byte) {
  13. const (
  14. inputMargin = 12 - 1
  15. minNonLiteralBlockSize = 1 + 1 + inputMargin
  16. )
  17. if debugDeflate && e.cur < 0 {
  18. panic(fmt.Sprint("e.cur < 0: ", e.cur))
  19. }
  20. // Protect against e.cur wraparound.
  21. for e.cur >= bufferReset {
  22. if len(e.hist) == 0 {
  23. for i := range e.table[:] {
  24. e.table[i] = tableEntry{}
  25. }
  26. e.cur = maxMatchOffset
  27. break
  28. }
  29. // Shift down everything in the table that isn't already too far away.
  30. minOff := e.cur + int32(len(e.hist)) - maxMatchOffset
  31. for i := range e.table[:] {
  32. v := e.table[i].offset
  33. if v <= minOff {
  34. v = 0
  35. } else {
  36. v = v - e.cur + maxMatchOffset
  37. }
  38. e.table[i].offset = v
  39. }
  40. e.cur = maxMatchOffset
  41. }
  42. s := e.addBlock(src)
  43. // This check isn't in the Snappy implementation, but there, the caller
  44. // instead of the callee handles this case.
  45. if len(src) < minNonLiteralBlockSize {
  46. // We do not fill the token table.
  47. // This will be picked up by caller.
  48. dst.n = uint16(len(src))
  49. return
  50. }
  51. // Override src
  52. src = e.hist
  53. nextEmit := s
  54. // sLimit is when to stop looking for offset/length copies. The inputMargin
  55. // lets us use a fast path for emitLiteral in the main loop, while we are
  56. // looking for copies.
  57. sLimit := int32(len(src) - inputMargin)
  58. // nextEmit is where in src the next emitLiteral should start from.
  59. cv := load3232(src, s)
  60. for {
  61. // When should we start skipping if we haven't found matches in a long while.
  62. const skipLog = 5
  63. const doEvery = 2
  64. nextS := s
  65. var candidate tableEntry
  66. for {
  67. nextHash := hash4u(cv, bTableBits)
  68. s = nextS
  69. nextS = s + doEvery + (s-nextEmit)>>skipLog
  70. if nextS > sLimit {
  71. goto emitRemainder
  72. }
  73. candidate = e.table[nextHash]
  74. now := load6432(src, nextS)
  75. e.table[nextHash] = tableEntry{offset: s + e.cur}
  76. nextHash = hash4u(uint32(now), bTableBits)
  77. offset := s - (candidate.offset - e.cur)
  78. if offset < maxMatchOffset && cv == load3232(src, candidate.offset-e.cur) {
  79. e.table[nextHash] = tableEntry{offset: nextS + e.cur}
  80. break
  81. }
  82. // Do one right away...
  83. cv = uint32(now)
  84. s = nextS
  85. nextS++
  86. candidate = e.table[nextHash]
  87. now >>= 8
  88. e.table[nextHash] = tableEntry{offset: s + e.cur}
  89. offset = s - (candidate.offset - e.cur)
  90. if offset < maxMatchOffset && cv == load3232(src, candidate.offset-e.cur) {
  91. break
  92. }
  93. cv = uint32(now)
  94. }
  95. // A 4-byte match has been found. We'll later see if more than 4 bytes
  96. // match. But, prior to the match, src[nextEmit:s] are unmatched. Emit
  97. // them as literal bytes.
  98. // Call emitCopy, and then see if another emitCopy could be our next
  99. // move. Repeat until we find no match for the input immediately after
  100. // what was consumed by the last emitCopy call.
  101. //
  102. // If we exit this loop normally then we need to call emitLiteral next,
  103. // though we don't yet know how big the literal will be. We handle that
  104. // by proceeding to the next iteration of the main loop. We also can
  105. // exit this loop via goto if we get close to exhausting the input.
  106. for {
  107. // Invariant: we have a 4-byte match at s, and no need to emit any
  108. // literal bytes prior to s.
  109. // Extend the 4-byte match as long as possible.
  110. t := candidate.offset - e.cur
  111. l := e.matchlenLong(s+4, t+4, src) + 4
  112. // Extend backwards
  113. for t > 0 && s > nextEmit && src[t-1] == src[s-1] {
  114. s--
  115. t--
  116. l++
  117. }
  118. if nextEmit < s {
  119. emitLiteral(dst, src[nextEmit:s])
  120. }
  121. dst.AddMatchLong(l, uint32(s-t-baseMatchOffset))
  122. s += l
  123. nextEmit = s
  124. if nextS >= s {
  125. s = nextS + 1
  126. }
  127. if s >= sLimit {
  128. // Index first pair after match end.
  129. if int(s+l+4) < len(src) {
  130. cv := load3232(src, s)
  131. e.table[hash4u(cv, bTableBits)] = tableEntry{offset: s + e.cur}
  132. }
  133. goto emitRemainder
  134. }
  135. // Store every second hash in-between, but offset by 1.
  136. for i := s - l + 2; i < s-5; i += 7 {
  137. x := load6432(src, int32(i))
  138. nextHash := hash4u(uint32(x), bTableBits)
  139. e.table[nextHash] = tableEntry{offset: e.cur + i}
  140. // Skip one
  141. x >>= 16
  142. nextHash = hash4u(uint32(x), bTableBits)
  143. e.table[nextHash] = tableEntry{offset: e.cur + i + 2}
  144. // Skip one
  145. x >>= 16
  146. nextHash = hash4u(uint32(x), bTableBits)
  147. e.table[nextHash] = tableEntry{offset: e.cur + i + 4}
  148. }
  149. // We could immediately start working at s now, but to improve
  150. // compression we first update the hash table at s-2 to s. If
  151. // another emitCopy is not our next move, also calculate nextHash
  152. // at s+1. At least on GOARCH=amd64, these three hash calculations
  153. // are faster as one load64 call (with some shifts) instead of
  154. // three load32 calls.
  155. x := load6432(src, s-2)
  156. o := e.cur + s - 2
  157. prevHash := hash4u(uint32(x), bTableBits)
  158. prevHash2 := hash4u(uint32(x>>8), bTableBits)
  159. e.table[prevHash] = tableEntry{offset: o}
  160. e.table[prevHash2] = tableEntry{offset: o + 1}
  161. currHash := hash4u(uint32(x>>16), bTableBits)
  162. candidate = e.table[currHash]
  163. e.table[currHash] = tableEntry{offset: o + 2}
  164. offset := s - (candidate.offset - e.cur)
  165. if offset > maxMatchOffset || uint32(x>>16) != load3232(src, candidate.offset-e.cur) {
  166. cv = uint32(x >> 24)
  167. s++
  168. break
  169. }
  170. }
  171. }
  172. emitRemainder:
  173. if int(nextEmit) < len(src) {
  174. // If nothing was added, don't encode literals.
  175. if dst.n == 0 {
  176. return
  177. }
  178. emitLiteral(dst, src[nextEmit:])
  179. }
  180. }