Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

303 wiersze
7.5 KiB

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