source: code/trunk/vendor/github.com/klauspost/compress/flate/level5.go@ 145

Last change on this file since 145 was 145, checked in by Izuru Yakumo, 22 months ago

Updated the Makefile and vendored depedencies

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

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