1 | package flate
|
---|
2 |
|
---|
3 | import (
|
---|
4 | "encoding/binary"
|
---|
5 | "fmt"
|
---|
6 | "math/bits"
|
---|
7 | )
|
---|
8 |
|
---|
9 | // fastGen maintains the table for matches,
|
---|
10 | // and the previous byte block for level 2.
|
---|
11 | // This is the generic implementation.
|
---|
12 | type fastEncL1 struct {
|
---|
13 | fastGen
|
---|
14 | table [tableSize]tableEntry
|
---|
15 | }
|
---|
16 |
|
---|
17 | // EncodeL1 uses a similar algorithm to level 1
|
---|
18 | func (e *fastEncL1) Encode(dst *tokens, src []byte) {
|
---|
19 | const (
|
---|
20 | inputMargin = 12 - 1
|
---|
21 | minNonLiteralBlockSize = 1 + 1 + inputMargin
|
---|
22 | )
|
---|
23 | if debugDeflate && e.cur < 0 {
|
---|
24 | panic(fmt.Sprint("e.cur < 0: ", e.cur))
|
---|
25 | }
|
---|
26 |
|
---|
27 | // Protect against e.cur wraparound.
|
---|
28 | for e.cur >= bufferReset {
|
---|
29 | if len(e.hist) == 0 {
|
---|
30 | for i := range e.table[:] {
|
---|
31 | e.table[i] = tableEntry{}
|
---|
32 | }
|
---|
33 | e.cur = maxMatchOffset
|
---|
34 | break
|
---|
35 | }
|
---|
36 | // Shift down everything in the table that isn't already too far away.
|
---|
37 | minOff := e.cur + int32(len(e.hist)) - maxMatchOffset
|
---|
38 | for i := range e.table[:] {
|
---|
39 | v := e.table[i].offset
|
---|
40 | if v <= minOff {
|
---|
41 | v = 0
|
---|
42 | } else {
|
---|
43 | v = v - e.cur + maxMatchOffset
|
---|
44 | }
|
---|
45 | e.table[i].offset = v
|
---|
46 | }
|
---|
47 | e.cur = maxMatchOffset
|
---|
48 | }
|
---|
49 |
|
---|
50 | s := e.addBlock(src)
|
---|
51 |
|
---|
52 | // This check isn't in the Snappy implementation, but there, the caller
|
---|
53 | // instead of the callee handles this case.
|
---|
54 | if len(src) < minNonLiteralBlockSize {
|
---|
55 | // We do not fill the token table.
|
---|
56 | // This will be picked up by caller.
|
---|
57 | dst.n = uint16(len(src))
|
---|
58 | return
|
---|
59 | }
|
---|
60 |
|
---|
61 | // Override src
|
---|
62 | src = e.hist
|
---|
63 | nextEmit := s
|
---|
64 |
|
---|
65 | // sLimit is when to stop looking for offset/length copies. The inputMargin
|
---|
66 | // lets us use a fast path for emitLiteral in the main loop, while we are
|
---|
67 | // looking for copies.
|
---|
68 | sLimit := int32(len(src) - inputMargin)
|
---|
69 |
|
---|
70 | // nextEmit is where in src the next emitLiteral should start from.
|
---|
71 | cv := load3232(src, s)
|
---|
72 |
|
---|
73 | for {
|
---|
74 | const skipLog = 5
|
---|
75 | const doEvery = 2
|
---|
76 |
|
---|
77 | nextS := s
|
---|
78 | var candidate tableEntry
|
---|
79 | for {
|
---|
80 | nextHash := hash(cv)
|
---|
81 | candidate = e.table[nextHash]
|
---|
82 | nextS = s + doEvery + (s-nextEmit)>>skipLog
|
---|
83 | if nextS > sLimit {
|
---|
84 | goto emitRemainder
|
---|
85 | }
|
---|
86 |
|
---|
87 | now := load6432(src, nextS)
|
---|
88 | e.table[nextHash] = tableEntry{offset: s + e.cur}
|
---|
89 | nextHash = hash(uint32(now))
|
---|
90 |
|
---|
91 | offset := s - (candidate.offset - e.cur)
|
---|
92 | if offset < maxMatchOffset && cv == load3232(src, candidate.offset-e.cur) {
|
---|
93 | e.table[nextHash] = tableEntry{offset: nextS + e.cur}
|
---|
94 | break
|
---|
95 | }
|
---|
96 |
|
---|
97 | // Do one right away...
|
---|
98 | cv = uint32(now)
|
---|
99 | s = nextS
|
---|
100 | nextS++
|
---|
101 | candidate = e.table[nextHash]
|
---|
102 | now >>= 8
|
---|
103 | e.table[nextHash] = tableEntry{offset: s + e.cur}
|
---|
104 |
|
---|
105 | offset = s - (candidate.offset - e.cur)
|
---|
106 | if offset < maxMatchOffset && cv == load3232(src, candidate.offset-e.cur) {
|
---|
107 | e.table[nextHash] = tableEntry{offset: nextS + e.cur}
|
---|
108 | break
|
---|
109 | }
|
---|
110 | cv = uint32(now)
|
---|
111 | s = nextS
|
---|
112 | }
|
---|
113 |
|
---|
114 | // A 4-byte match has been found. We'll later see if more than 4 bytes
|
---|
115 | // match. But, prior to the match, src[nextEmit:s] are unmatched. Emit
|
---|
116 | // them as literal bytes.
|
---|
117 | for {
|
---|
118 | // Invariant: we have a 4-byte match at s, and no need to emit any
|
---|
119 | // literal bytes prior to s.
|
---|
120 |
|
---|
121 | // Extend the 4-byte match as long as possible.
|
---|
122 | t := candidate.offset - e.cur
|
---|
123 | var l = int32(4)
|
---|
124 | if false {
|
---|
125 | l = e.matchlenLong(s+4, t+4, src) + 4
|
---|
126 | } else {
|
---|
127 | // inlined:
|
---|
128 | a := src[s+4:]
|
---|
129 | b := src[t+4:]
|
---|
130 | for len(a) >= 8 {
|
---|
131 | if diff := binary.LittleEndian.Uint64(a) ^ binary.LittleEndian.Uint64(b); diff != 0 {
|
---|
132 | l += int32(bits.TrailingZeros64(diff) >> 3)
|
---|
133 | break
|
---|
134 | }
|
---|
135 | l += 8
|
---|
136 | a = a[8:]
|
---|
137 | b = b[8:]
|
---|
138 | }
|
---|
139 | if len(a) < 8 {
|
---|
140 | b = b[:len(a)]
|
---|
141 | for i := range a {
|
---|
142 | if a[i] != b[i] {
|
---|
143 | break
|
---|
144 | }
|
---|
145 | l++
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | // Extend backwards
|
---|
151 | for t > 0 && s > nextEmit && src[t-1] == src[s-1] {
|
---|
152 | s--
|
---|
153 | t--
|
---|
154 | l++
|
---|
155 | }
|
---|
156 | if nextEmit < s {
|
---|
157 | if false {
|
---|
158 | emitLiteral(dst, src[nextEmit:s])
|
---|
159 | } else {
|
---|
160 | for _, v := range src[nextEmit:s] {
|
---|
161 | dst.tokens[dst.n] = token(v)
|
---|
162 | dst.litHist[v]++
|
---|
163 | dst.n++
|
---|
164 | }
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | // Save the match found
|
---|
169 | if false {
|
---|
170 | dst.AddMatchLong(l, uint32(s-t-baseMatchOffset))
|
---|
171 | } else {
|
---|
172 | // Inlined...
|
---|
173 | xoffset := uint32(s - t - baseMatchOffset)
|
---|
174 | xlength := l
|
---|
175 | oc := offsetCode(xoffset)
|
---|
176 | xoffset |= oc << 16
|
---|
177 | for xlength > 0 {
|
---|
178 | xl := xlength
|
---|
179 | if xl > 258 {
|
---|
180 | if xl > 258+baseMatchLength {
|
---|
181 | xl = 258
|
---|
182 | } else {
|
---|
183 | xl = 258 - baseMatchLength
|
---|
184 | }
|
---|
185 | }
|
---|
186 | xlength -= xl
|
---|
187 | xl -= baseMatchLength
|
---|
188 | dst.extraHist[lengthCodes1[uint8(xl)]]++
|
---|
189 | dst.offHist[oc]++
|
---|
190 | dst.tokens[dst.n] = token(matchType | uint32(xl)<<lengthShift | xoffset)
|
---|
191 | dst.n++
|
---|
192 | }
|
---|
193 | }
|
---|
194 | s += l
|
---|
195 | nextEmit = s
|
---|
196 | if nextS >= s {
|
---|
197 | s = nextS + 1
|
---|
198 | }
|
---|
199 | if s >= sLimit {
|
---|
200 | // Index first pair after match end.
|
---|
201 | if int(s+l+4) < len(src) {
|
---|
202 | cv := load3232(src, s)
|
---|
203 | e.table[hash(cv)] = tableEntry{offset: s + e.cur}
|
---|
204 | }
|
---|
205 | goto emitRemainder
|
---|
206 | }
|
---|
207 |
|
---|
208 | // We could immediately start working at s now, but to improve
|
---|
209 | // compression we first update the hash table at s-2 and at s. If
|
---|
210 | // another emitCopy is not our next move, also calculate nextHash
|
---|
211 | // at s+1. At least on GOARCH=amd64, these three hash calculations
|
---|
212 | // are faster as one load64 call (with some shifts) instead of
|
---|
213 | // three load32 calls.
|
---|
214 | x := load6432(src, s-2)
|
---|
215 | o := e.cur + s - 2
|
---|
216 | prevHash := hash(uint32(x))
|
---|
217 | e.table[prevHash] = tableEntry{offset: o}
|
---|
218 | x >>= 16
|
---|
219 | currHash := hash(uint32(x))
|
---|
220 | candidate = e.table[currHash]
|
---|
221 | e.table[currHash] = tableEntry{offset: o + 2}
|
---|
222 |
|
---|
223 | offset := s - (candidate.offset - e.cur)
|
---|
224 | if offset > maxMatchOffset || uint32(x) != load3232(src, candidate.offset-e.cur) {
|
---|
225 | cv = uint32(x >> 8)
|
---|
226 | s++
|
---|
227 | break
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | emitRemainder:
|
---|
233 | if int(nextEmit) < len(src) {
|
---|
234 | // If nothing was added, don't encode literals.
|
---|
235 | if dst.n == 0 {
|
---|
236 | return
|
---|
237 | }
|
---|
238 | emitLiteral(dst, src[nextEmit:])
|
---|
239 | }
|
---|
240 | }
|
---|