level6.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. package flate
  2. import "fmt"
  3. type fastEncL6 struct {
  4. fastGen
  5. table [tableSize]tableEntry
  6. bTable [tableSize]tableEntryPrev
  7. }
  8. func (e *fastEncL6) 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. // Repeat MUST be > 1 and within range
  76. repeat := int32(1)
  77. for {
  78. const skipLog = 7
  79. const doEvery = 1
  80. nextS := s
  81. var l int32
  82. var t int32
  83. for {
  84. nextHashS := hashLen(cv, tableBits, hashShortBytes)
  85. nextHashL := hash7(cv, tableBits)
  86. s = nextS
  87. nextS = s + doEvery + (s-nextEmit)>>skipLog
  88. if nextS > sLimit {
  89. goto emitRemainder
  90. }
  91. // Fetch a short+long candidate
  92. sCandidate := e.table[nextHashS]
  93. lCandidate := e.bTable[nextHashL]
  94. next := load6432(src, nextS)
  95. entry := tableEntry{offset: s + e.cur}
  96. e.table[nextHashS] = entry
  97. eLong := &e.bTable[nextHashL]
  98. eLong.Cur, eLong.Prev = entry, eLong.Cur
  99. // Calculate hashes of 'next'
  100. nextHashS = hashLen(next, tableBits, hashShortBytes)
  101. nextHashL = hash7(next, tableBits)
  102. t = lCandidate.Cur.offset - e.cur
  103. if s-t < maxMatchOffset {
  104. if uint32(cv) == load3232(src, lCandidate.Cur.offset-e.cur) {
  105. // Long candidate matches at least 4 bytes.
  106. // Store the next match
  107. e.table[nextHashS] = tableEntry{offset: nextS + e.cur}
  108. eLong := &e.bTable[nextHashL]
  109. eLong.Cur, eLong.Prev = tableEntry{offset: nextS + e.cur}, eLong.Cur
  110. // Check the previous long candidate as well.
  111. t2 := lCandidate.Prev.offset - e.cur
  112. if s-t2 < maxMatchOffset && uint32(cv) == load3232(src, lCandidate.Prev.offset-e.cur) {
  113. l = e.matchlen(s+4, t+4, src) + 4
  114. ml1 := e.matchlen(s+4, t2+4, src) + 4
  115. if ml1 > l {
  116. t = t2
  117. l = ml1
  118. break
  119. }
  120. }
  121. break
  122. }
  123. // Current value did not match, but check if previous long value does.
  124. t = lCandidate.Prev.offset - e.cur
  125. if s-t < maxMatchOffset && uint32(cv) == load3232(src, lCandidate.Prev.offset-e.cur) {
  126. // Store the next match
  127. e.table[nextHashS] = tableEntry{offset: nextS + e.cur}
  128. eLong := &e.bTable[nextHashL]
  129. eLong.Cur, eLong.Prev = tableEntry{offset: nextS + e.cur}, eLong.Cur
  130. break
  131. }
  132. }
  133. t = sCandidate.offset - e.cur
  134. if s-t < maxMatchOffset && uint32(cv) == load3232(src, sCandidate.offset-e.cur) {
  135. // Found a 4 match...
  136. l = e.matchlen(s+4, t+4, src) + 4
  137. // Look up next long candidate (at nextS)
  138. lCandidate = e.bTable[nextHashL]
  139. // Store the next match
  140. e.table[nextHashS] = tableEntry{offset: nextS + e.cur}
  141. eLong := &e.bTable[nextHashL]
  142. eLong.Cur, eLong.Prev = tableEntry{offset: nextS + e.cur}, eLong.Cur
  143. // Check repeat at s + repOff
  144. const repOff = 1
  145. t2 := s - repeat + repOff
  146. if load3232(src, t2) == uint32(cv>>(8*repOff)) {
  147. ml := e.matchlen(s+4+repOff, t2+4, src) + 4
  148. if ml > l {
  149. t = t2
  150. l = ml
  151. s += repOff
  152. // Not worth checking more.
  153. break
  154. }
  155. }
  156. // If the next long is a candidate, use that...
  157. t2 = lCandidate.Cur.offset - e.cur
  158. if nextS-t2 < maxMatchOffset {
  159. if load3232(src, lCandidate.Cur.offset-e.cur) == uint32(next) {
  160. ml := e.matchlen(nextS+4, t2+4, src) + 4
  161. if ml > l {
  162. t = t2
  163. s = nextS
  164. l = ml
  165. // This is ok, but check previous as well.
  166. }
  167. }
  168. // If the previous long is a candidate, use that...
  169. t2 = lCandidate.Prev.offset - e.cur
  170. if nextS-t2 < maxMatchOffset && load3232(src, lCandidate.Prev.offset-e.cur) == uint32(next) {
  171. ml := e.matchlen(nextS+4, t2+4, src) + 4
  172. if ml > l {
  173. t = t2
  174. s = nextS
  175. l = ml
  176. break
  177. }
  178. }
  179. }
  180. break
  181. }
  182. cv = next
  183. }
  184. // A 4-byte match has been found. We'll later see if more than 4 bytes
  185. // match. But, prior to the match, src[nextEmit:s] are unmatched. Emit
  186. // them as literal bytes.
  187. // Extend the 4-byte match as long as possible.
  188. if l == 0 {
  189. l = e.matchlenLong(s+4, t+4, src) + 4
  190. } else if l == maxMatchLength {
  191. l += e.matchlenLong(s+l, t+l, src)
  192. }
  193. // Try to locate a better match by checking the end-of-match...
  194. if sAt := s + l; sAt < sLimit {
  195. // Allow some bytes at the beginning to mismatch.
  196. // Sweet spot is 2/3 bytes depending on input.
  197. // 3 is only a little better when it is but sometimes a lot worse.
  198. // The skipped bytes are tested in Extend backwards,
  199. // and still picked up as part of the match if they do.
  200. const skipBeginning = 2
  201. eLong := &e.bTable[hash7(load6432(src, sAt), tableBits)]
  202. // Test current
  203. t2 := eLong.Cur.offset - e.cur - l + skipBeginning
  204. s2 := s + skipBeginning
  205. off := s2 - t2
  206. if off < maxMatchOffset {
  207. if off > 0 && t2 >= 0 {
  208. if l2 := e.matchlenLong(s2, t2, src); l2 > l {
  209. t = t2
  210. l = l2
  211. s = s2
  212. }
  213. }
  214. // Test next:
  215. t2 = eLong.Prev.offset - e.cur - l + skipBeginning
  216. off := s2 - t2
  217. if off > 0 && off < maxMatchOffset && t2 >= 0 {
  218. if l2 := e.matchlenLong(s2, t2, src); l2 > l {
  219. t = t2
  220. l = l2
  221. s = s2
  222. }
  223. }
  224. }
  225. }
  226. // Extend backwards
  227. for t > 0 && s > nextEmit && src[t-1] == src[s-1] {
  228. s--
  229. t--
  230. l++
  231. }
  232. if nextEmit < s {
  233. if false {
  234. emitLiteral(dst, src[nextEmit:s])
  235. } else {
  236. for _, v := range src[nextEmit:s] {
  237. dst.tokens[dst.n] = token(v)
  238. dst.litHist[v]++
  239. dst.n++
  240. }
  241. }
  242. }
  243. if false {
  244. if t >= s {
  245. panic(fmt.Sprintln("s-t", s, t))
  246. }
  247. if (s - t) > maxMatchOffset {
  248. panic(fmt.Sprintln("mmo", s-t))
  249. }
  250. if l < baseMatchLength {
  251. panic("bml")
  252. }
  253. }
  254. dst.AddMatchLong(l, uint32(s-t-baseMatchOffset))
  255. repeat = s - t
  256. s += l
  257. nextEmit = s
  258. if nextS >= s {
  259. s = nextS + 1
  260. }
  261. if s >= sLimit {
  262. // Index after match end.
  263. for i := nextS + 1; i < int32(len(src))-8; i += 2 {
  264. cv := load6432(src, i)
  265. e.table[hashLen(cv, tableBits, hashShortBytes)] = tableEntry{offset: i + e.cur}
  266. eLong := &e.bTable[hash7(cv, tableBits)]
  267. eLong.Cur, eLong.Prev = tableEntry{offset: i + e.cur}, eLong.Cur
  268. }
  269. goto emitRemainder
  270. }
  271. // Store every long hash in-between and every second short.
  272. if true {
  273. for i := nextS + 1; i < s-1; i += 2 {
  274. cv := load6432(src, i)
  275. t := tableEntry{offset: i + e.cur}
  276. t2 := tableEntry{offset: t.offset + 1}
  277. eLong := &e.bTable[hash7(cv, tableBits)]
  278. eLong2 := &e.bTable[hash7(cv>>8, tableBits)]
  279. e.table[hashLen(cv, tableBits, hashShortBytes)] = t
  280. eLong.Cur, eLong.Prev = t, eLong.Cur
  281. eLong2.Cur, eLong2.Prev = t2, eLong2.Cur
  282. }
  283. }
  284. // We could immediately start working at s now, but to improve
  285. // compression we first update the hash table at s-1 and at s.
  286. cv = load6432(src, s)
  287. }
  288. emitRemainder:
  289. if int(nextEmit) < len(src) {
  290. // If nothing was added, don't encode literals.
  291. if dst.n == 0 {
  292. return
  293. }
  294. emitLiteral(dst, src[nextEmit:])
  295. }
  296. }