level5.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package flate
  2. import "fmt"
  3. type fastEncL5 struct {
  4. fastGen
  5. table [tableSize]tableEntry
  6. bTable [tableSize]tableEntryPrev
  7. }
  8. func (e *fastEncL5) Encode(dst *tokens, src []byte) {
  9. const (
  10. inputMargin = 12 - 1
  11. minNonLiteralBlockSize = 1 + 1 + inputMargin
  12. hashShortBytes = 4
  13. )
  14. if debugDeflate && e.cur < 0 {
  15. panic(fmt.Sprint("e.cur < 0: ", e.cur))
  16. }
  17. // Protect against e.cur wraparound.
  18. for e.cur >= bufferReset {
  19. if len(e.hist) == 0 {
  20. for i := range e.table[:] {
  21. e.table[i] = tableEntry{}
  22. }
  23. for i := range e.bTable[:] {
  24. e.bTable[i] = tableEntryPrev{}
  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. for i := range e.bTable[:] {
  41. v := e.bTable[i]
  42. if v.Cur.offset <= minOff {
  43. v.Cur.offset = 0
  44. v.Prev.offset = 0
  45. } else {
  46. v.Cur.offset = v.Cur.offset - e.cur + maxMatchOffset
  47. if v.Prev.offset <= minOff {
  48. v.Prev.offset = 0
  49. } else {
  50. v.Prev.offset = v.Prev.offset - e.cur + maxMatchOffset
  51. }
  52. }
  53. e.bTable[i] = v
  54. }
  55. e.cur = maxMatchOffset
  56. }
  57. s := e.addBlock(src)
  58. // This check isn't in the Snappy implementation, but there, the caller
  59. // instead of the callee handles this case.
  60. if len(src) < minNonLiteralBlockSize {
  61. // We do not fill the token table.
  62. // This will be picked up by caller.
  63. dst.n = uint16(len(src))
  64. return
  65. }
  66. // Override src
  67. src = e.hist
  68. nextEmit := s
  69. // sLimit is when to stop looking for offset/length copies. The inputMargin
  70. // lets us use a fast path for emitLiteral in the main loop, while we are
  71. // looking for copies.
  72. sLimit := int32(len(src) - inputMargin)
  73. // nextEmit is where in src the next emitLiteral should start from.
  74. cv := load6432(src, s)
  75. for {
  76. const skipLog = 6
  77. const doEvery = 1
  78. nextS := s
  79. var l int32
  80. var t int32
  81. for {
  82. nextHashS := hashLen(cv, tableBits, hashShortBytes)
  83. nextHashL := hash7(cv, tableBits)
  84. s = nextS
  85. nextS = s + doEvery + (s-nextEmit)>>skipLog
  86. if nextS > sLimit {
  87. goto emitRemainder
  88. }
  89. // Fetch a short+long candidate
  90. sCandidate := e.table[nextHashS]
  91. lCandidate := e.bTable[nextHashL]
  92. next := load6432(src, nextS)
  93. entry := tableEntry{offset: s + e.cur}
  94. e.table[nextHashS] = entry
  95. eLong := &e.bTable[nextHashL]
  96. eLong.Cur, eLong.Prev = entry, eLong.Cur
  97. nextHashS = hashLen(next, tableBits, hashShortBytes)
  98. nextHashL = hash7(next, tableBits)
  99. t = lCandidate.Cur.offset - e.cur
  100. if s-t < maxMatchOffset {
  101. if uint32(cv) == load3232(src, lCandidate.Cur.offset-e.cur) {
  102. // Store the next match
  103. e.table[nextHashS] = tableEntry{offset: nextS + e.cur}
  104. eLong := &e.bTable[nextHashL]
  105. eLong.Cur, eLong.Prev = tableEntry{offset: nextS + e.cur}, eLong.Cur
  106. t2 := lCandidate.Prev.offset - e.cur
  107. if s-t2 < maxMatchOffset && uint32(cv) == load3232(src, lCandidate.Prev.offset-e.cur) {
  108. l = e.matchlen(s+4, t+4, src) + 4
  109. ml1 := e.matchlen(s+4, t2+4, src) + 4
  110. if ml1 > l {
  111. t = t2
  112. l = ml1
  113. break
  114. }
  115. }
  116. break
  117. }
  118. t = lCandidate.Prev.offset - e.cur
  119. if s-t < maxMatchOffset && uint32(cv) == load3232(src, lCandidate.Prev.offset-e.cur) {
  120. // Store the next match
  121. e.table[nextHashS] = tableEntry{offset: nextS + e.cur}
  122. eLong := &e.bTable[nextHashL]
  123. eLong.Cur, eLong.Prev = tableEntry{offset: nextS + e.cur}, eLong.Cur
  124. break
  125. }
  126. }
  127. t = sCandidate.offset - e.cur
  128. if s-t < maxMatchOffset && uint32(cv) == load3232(src, sCandidate.offset-e.cur) {
  129. // Found a 4 match...
  130. l = e.matchlen(s+4, t+4, src) + 4
  131. lCandidate = e.bTable[nextHashL]
  132. // Store the next match
  133. e.table[nextHashS] = tableEntry{offset: nextS + e.cur}
  134. eLong := &e.bTable[nextHashL]
  135. eLong.Cur, eLong.Prev = tableEntry{offset: nextS + e.cur}, eLong.Cur
  136. // If the next long is a candidate, use that...
  137. t2 := lCandidate.Cur.offset - e.cur
  138. if nextS-t2 < maxMatchOffset {
  139. if load3232(src, lCandidate.Cur.offset-e.cur) == uint32(next) {
  140. ml := e.matchlen(nextS+4, t2+4, src) + 4
  141. if ml > l {
  142. t = t2
  143. s = nextS
  144. l = ml
  145. break
  146. }
  147. }
  148. // If the previous long is a candidate, use that...
  149. t2 = lCandidate.Prev.offset - e.cur
  150. if nextS-t2 < maxMatchOffset && load3232(src, lCandidate.Prev.offset-e.cur) == uint32(next) {
  151. ml := e.matchlen(nextS+4, t2+4, src) + 4
  152. if ml > l {
  153. t = t2
  154. s = nextS
  155. l = ml
  156. break
  157. }
  158. }
  159. }
  160. break
  161. }
  162. cv = next
  163. }
  164. // A 4-byte match has been found. We'll later see if more than 4 bytes
  165. // match. But, prior to the match, src[nextEmit:s] are unmatched. Emit
  166. // them as literal bytes.
  167. if l == 0 {
  168. // Extend the 4-byte match as long as possible.
  169. l = e.matchlenLong(s+4, t+4, src) + 4
  170. } else if l == maxMatchLength {
  171. l += e.matchlenLong(s+l, t+l, src)
  172. }
  173. // Try to locate a better match by checking the end of best match...
  174. if sAt := s + l; l < 30 && sAt < sLimit {
  175. // Allow some bytes at the beginning to mismatch.
  176. // Sweet spot is 2/3 bytes depending on input.
  177. // 3 is only a little better when it is but sometimes a lot worse.
  178. // The skipped bytes are tested in Extend backwards,
  179. // and still picked up as part of the match if they do.
  180. const skipBeginning = 2
  181. eLong := e.bTable[hash7(load6432(src, sAt), tableBits)].Cur.offset
  182. t2 := eLong - e.cur - l + skipBeginning
  183. s2 := s + skipBeginning
  184. off := s2 - t2
  185. if t2 >= 0 && off < maxMatchOffset && off > 0 {
  186. if l2 := e.matchlenLong(s2, t2, src); l2 > l {
  187. t = t2
  188. l = l2
  189. s = s2
  190. }
  191. }
  192. }
  193. // Extend backwards
  194. for t > 0 && s > nextEmit && src[t-1] == src[s-1] {
  195. s--
  196. t--
  197. l++
  198. }
  199. if nextEmit < s {
  200. if false {
  201. emitLiteral(dst, src[nextEmit:s])
  202. } else {
  203. for _, v := range src[nextEmit:s] {
  204. dst.tokens[dst.n] = token(v)
  205. dst.litHist[v]++
  206. dst.n++
  207. }
  208. }
  209. }
  210. if debugDeflate {
  211. if t >= s {
  212. panic(fmt.Sprintln("s-t", s, t))
  213. }
  214. if (s - t) > maxMatchOffset {
  215. panic(fmt.Sprintln("mmo", s-t))
  216. }
  217. if l < baseMatchLength {
  218. panic("bml")
  219. }
  220. }
  221. dst.AddMatchLong(l, uint32(s-t-baseMatchOffset))
  222. s += l
  223. nextEmit = s
  224. if nextS >= s {
  225. s = nextS + 1
  226. }
  227. if s >= sLimit {
  228. goto emitRemainder
  229. }
  230. // Store every 3rd hash in-between.
  231. if true {
  232. const hashEvery = 3
  233. i := s - l + 1
  234. if i < s-1 {
  235. cv := load6432(src, i)
  236. t := tableEntry{offset: i + e.cur}
  237. e.table[hashLen(cv, tableBits, hashShortBytes)] = t
  238. eLong := &e.bTable[hash7(cv, tableBits)]
  239. eLong.Cur, eLong.Prev = t, eLong.Cur
  240. // Do an long at i+1
  241. cv >>= 8
  242. t = tableEntry{offset: t.offset + 1}
  243. eLong = &e.bTable[hash7(cv, tableBits)]
  244. eLong.Cur, eLong.Prev = t, eLong.Cur
  245. // We only have enough bits for a short entry at i+2
  246. cv >>= 8
  247. t = tableEntry{offset: t.offset + 1}
  248. e.table[hashLen(cv, tableBits, hashShortBytes)] = t
  249. // Skip one - otherwise we risk hitting 's'
  250. i += 4
  251. for ; i < s-1; i += hashEvery {
  252. cv := load6432(src, i)
  253. t := tableEntry{offset: i + e.cur}
  254. t2 := tableEntry{offset: t.offset + 1}
  255. eLong := &e.bTable[hash7(cv, tableBits)]
  256. eLong.Cur, eLong.Prev = t, eLong.Cur
  257. e.table[hashLen(cv>>8, tableBits, hashShortBytes)] = t2
  258. }
  259. }
  260. }
  261. // We could immediately start working at s now, but to improve
  262. // compression we first update the hash table at s-1 and at s.
  263. x := load6432(src, s-1)
  264. o := e.cur + s - 1
  265. prevHashS := hashLen(x, tableBits, hashShortBytes)
  266. prevHashL := hash7(x, tableBits)
  267. e.table[prevHashS] = tableEntry{offset: o}
  268. eLong := &e.bTable[prevHashL]
  269. eLong.Cur, eLong.Prev = tableEntry{offset: o}, eLong.Cur
  270. cv = x >> 8
  271. }
  272. emitRemainder:
  273. if int(nextEmit) < len(src) {
  274. // If nothing was added, don't encode literals.
  275. if dst.n == 0 {
  276. return
  277. }
  278. emitLiteral(dst, src[nextEmit:])
  279. }
  280. }