You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
5.8 KiB

  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. if false {
  120. emitLiteral(dst, src[nextEmit:s])
  121. } else {
  122. for _, v := range src[nextEmit:s] {
  123. dst.tokens[dst.n] = token(v)
  124. dst.litHist[v]++
  125. dst.n++
  126. }
  127. }
  128. }
  129. dst.AddMatchLong(l, uint32(s-t-baseMatchOffset))
  130. s += l
  131. nextEmit = s
  132. if nextS >= s {
  133. s = nextS + 1
  134. }
  135. if s >= sLimit {
  136. // Index first pair after match end.
  137. if int(s+l+4) < len(src) {
  138. cv := load3232(src, s)
  139. e.table[hash4u(cv, bTableBits)] = tableEntry{offset: s + e.cur}
  140. }
  141. goto emitRemainder
  142. }
  143. // Store every second hash in-between, but offset by 1.
  144. for i := s - l + 2; i < s-5; i += 7 {
  145. x := load6432(src, i)
  146. nextHash := hash4u(uint32(x), bTableBits)
  147. e.table[nextHash] = tableEntry{offset: e.cur + i}
  148. // Skip one
  149. x >>= 16
  150. nextHash = hash4u(uint32(x), bTableBits)
  151. e.table[nextHash] = tableEntry{offset: e.cur + i + 2}
  152. // Skip one
  153. x >>= 16
  154. nextHash = hash4u(uint32(x), bTableBits)
  155. e.table[nextHash] = tableEntry{offset: e.cur + i + 4}
  156. }
  157. // We could immediately start working at s now, but to improve
  158. // compression we first update the hash table at s-2 to s. If
  159. // another emitCopy is not our next move, also calculate nextHash
  160. // at s+1. At least on GOARCH=amd64, these three hash calculations
  161. // are faster as one load64 call (with some shifts) instead of
  162. // three load32 calls.
  163. x := load6432(src, s-2)
  164. o := e.cur + s - 2
  165. prevHash := hash4u(uint32(x), bTableBits)
  166. prevHash2 := hash4u(uint32(x>>8), bTableBits)
  167. e.table[prevHash] = tableEntry{offset: o}
  168. e.table[prevHash2] = tableEntry{offset: o + 1}
  169. currHash := hash4u(uint32(x>>16), bTableBits)
  170. candidate = e.table[currHash]
  171. e.table[currHash] = tableEntry{offset: o + 2}
  172. offset := s - (candidate.offset - e.cur)
  173. if offset > maxMatchOffset || uint32(x>>16) != load3232(src, candidate.offset-e.cur) {
  174. cv = uint32(x >> 24)
  175. s++
  176. break
  177. }
  178. }
  179. }
  180. emitRemainder:
  181. if int(nextEmit) < len(src) {
  182. // If nothing was added, don't encode literals.
  183. if dst.n == 0 {
  184. return
  185. }
  186. emitLiteral(dst, src[nextEmit:])
  187. }
  188. }