/
o3.go
209 lines (176 loc) · 4.96 KB
/
o3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package pack
import "encoding/binary"
// O3 is an implementation of the MatchFinder interface
// that is comparable to Brotli level 3, but uses the overlap
// parsing strategy.
type O3 struct {
// MaxDistance is the maximum distance (in bytes) to look back for
// a match. The default is 65535.
MaxDistance int
table [1<<o3Bits + o3Sweep]uint32
history []byte
matchCache []AbsoluteMatch
}
const (
o3Bits = 16
o3Sweep = 2
)
func (q *O3) Reset() {
q.table = [1<<o3Bits + o3Sweep]uint32{}
q.history = q.history[:0]
}
func (q *O3) store(index int) {
hash := q.hash(binary.LittleEndian.Uint64(q.history[index:]))
offset := index >> 3 % o3Sweep
q.table[int(hash)+offset] = uint32(index)
}
// FindMatches looks for matches in src, appends them to dst, and returns dst.
func (q *O3) FindMatches(dst []Match, src []byte) []Match {
if q.MaxDistance == 0 {
q.MaxDistance = 65535
}
var nextEmit int
if len(q.history) > maxHistory {
// Trim down the history buffer.
delta := len(q.history) - minHistory
copy(q.history, q.history[delta:])
q.history = q.history[:minHistory]
for i, v := range q.table {
newV := int(v) - delta
if newV < 0 {
newV = 0
}
q.table[i] = uint32(newV)
}
}
// Append src to the history buffer.
nextEmit = len(q.history)
q.history = append(q.history, src...)
src = q.history
return q.Parse(dst, nextEmit, len(src))
}
func (q *O3) Parse(dst []Match, start, end int) []Match {
s := start
nextEmit := start
matchList := q.matchCache[:0]
for s < end {
matchList = matchList[:0]
m := q.search(s, nextEmit, end)
if m.End-m.Start < 4 {
s++
continue
}
matchList = append(matchList, m)
for {
for i := m.Start; i < m.End-2 && i < len(q.history)-8; i++ {
q.store(i)
}
// Look for a new match overlapping the end of m.
newMatch := q.search(m.End-2, m.Start, end)
if newMatch.End-newMatch.Start <= m.End-m.Start {
// It's no better than the previous match, so ignore it.
// But store the hash for the last byte of m before
// going on.
i := m.End - 1
if i <= len(q.history)-8 {
q.store(i)
}
break
}
m = newMatch
matchList = append(matchList, m)
}
// We now have a series of overlapping matches,
// each one longer than the previous one.
// Now we need to resolve the overlaps.
for i := len(matchList) - 2; i >= 0; i-- {
if matchList[i].End-matchList[i].Start > matchList[i+1].End-matchList[i+1].Start {
// This match is actually longer than the following one, probably because
// the following one has already been trimmed.
// So we'll trim the following one to remove the overlap with this match.
if matchList[i].End > matchList[i+1].Start {
matchList[i+1].Match += matchList[i].End - matchList[i+1].Start
matchList[i+1].Start = matchList[i].End
}
if matchList[i+1].End-matchList[i+1].Start < 4 {
// The following match is too short now, so we'll just drop it.
matchList = append(matchList[:i+1], matchList[i+2:]...)
if i < len(matchList)-1 {
// Run through the loop with the same index again,
// to catch overlaps between this match and its new neighbor.
i++
}
}
} else {
// The following match is longer than this one, so we'll trim this one
// to remove the overlap.
if matchList[i].End > matchList[i+1].Start {
matchList[i].End = matchList[i+1].Start
}
if matchList[i].End-matchList[i].Start < 4 {
// This match is too short now, so we'll just drop it.
matchList = append(matchList[:i], matchList[i+1:]...)
}
}
}
for _, m := range matchList {
dst = append(dst, Match{
Unmatched: m.Start - nextEmit,
Length: m.End - m.Start,
Distance: m.Start - m.Match,
})
nextEmit = m.End
}
s = nextEmit
}
if nextEmit < end {
dst = append(dst, Match{
Unmatched: end - nextEmit,
})
}
q.matchCache = matchList[:0]
return dst
}
func (q *O3) checkMatch(pos, candidate, min, max int) AbsoluteMatch {
src := q.history
if candidate == 0 || pos-candidate > q.MaxDistance {
return AbsoluteMatch{}
}
if binary.LittleEndian.Uint32(src[pos:]) != binary.LittleEndian.Uint32(src[candidate:]) {
return AbsoluteMatch{}
}
// We have a 4-byte match now.
start := pos
match := candidate
end := extendMatch(src[:max], match+4, start+4)
for start > min && match > 0 && src[start-1] == src[match-1] {
start--
match--
}
return AbsoluteMatch{
Start: start,
End: end,
Match: match,
}
}
func (q *O3) search(pos, min, max int) AbsoluteMatch {
if pos+8 > len(q.history) {
return AbsoluteMatch{}
}
src := q.history
h := q.hash(binary.LittleEndian.Uint64(src[pos:]))
m := AbsoluteMatch{}
for _, c := range q.table[h : h+o3Sweep] {
newMatch := q.checkMatch(pos, int(c), min, max)
if newMatch.End-newMatch.Start > m.End-m.Start {
m = newMatch
}
}
offset := pos >> 3 % o3Sweep
q.table[int(h)+offset] = uint32(pos)
return m
}
func (*O3) hash(u uint64) uint32 {
hash := (u << 24) * hashMul64
return uint32(hash >> (64 - o3Bits))
}