@@ -31,7 +31,7 @@ type Pathfinder struct {
31
31
ChainLength int
32
32
33
33
table []uint32
34
- chain []uint16
34
+ chain []uint32
35
35
36
36
history []byte
37
37
arrivals []arrival
@@ -57,7 +57,7 @@ type arrival struct {
57
57
}
58
58
59
59
const (
60
- baseMatchCost float32 = 5
60
+ baseMatchCost float32 = 4
61
61
repeatMatchCost float32 = 6
62
62
)
63
63
@@ -106,9 +106,7 @@ func (q *Pathfinder) FindMatches(dst []Match, src []byte) []Match {
106
106
delta := len (q .history ) - q .MaxDistance
107
107
copy (q .history , q .history [delta :])
108
108
q .history = q .history [:q .MaxDistance ]
109
- if q .ChainLength > 0 {
110
- q .chain = q .chain [:q .MaxDistance ]
111
- }
109
+ q .chain = q .chain [:q .MaxDistance ]
112
110
113
111
for i , v := range q .table {
114
112
newV := max (int (v )- delta , 0 )
@@ -119,11 +117,20 @@ func (q *Pathfinder) FindMatches(dst []Match, src []byte) []Match {
119
117
// Append src to the history buffer.
120
118
historyLen := len (q .history )
121
119
q .history = append (q .history , src ... )
122
- if q .ChainLength > 0 {
123
- q .chain = append (q .chain , make ([]uint16 , len (src ))... )
124
- }
120
+ q .chain = append (q .chain , make ([]uint32 , len (src ))... )
125
121
src = q .history
126
122
123
+ // Calculate hashes and build the chain.
124
+ for i := historyLen ; i < len (src )- 7 ; i ++ {
125
+ h := ((binary .LittleEndian .Uint64 (src [i :]) & (1 << (8 * q .HashLen ) - 1 )) * hashMul64 ) >> (64 - q .TableBits )
126
+ candidate := int (q .table [h ])
127
+ q .table [h ] = uint32 (i )
128
+ if candidate != 0 {
129
+ delta := i - candidate
130
+ q .chain [i ] = uint32 (delta )
131
+ }
132
+ }
133
+
127
134
for i := historyLen ; i < len (src ); i ++ {
128
135
var arrivedHere arrival
129
136
if i > historyLen {
@@ -161,17 +168,12 @@ func (q *Pathfinder) FindMatches(dst []Match, src []byte) []Match {
161
168
}
162
169
}
163
170
164
- // Calculate and store the hash.
165
- h := ((binary .LittleEndian .Uint64 (src [i :]) & (1 << (8 * q .HashLen ) - 1 )) * hashMul64 ) >> (64 - q .TableBits )
166
- candidate := int (q .table [h ])
167
- q .table [h ] = uint32 (i )
168
- if q .ChainLength > 0 && candidate != 0 {
169
- delta := i - candidate
170
- if delta < 1 << 16 {
171
- q .chain [i ] = uint16 (delta )
172
- }
171
+ delta := q .chain [i ]
172
+ if delta == 0 {
173
+ continue
173
174
}
174
- if candidate == 0 || i - candidate > q .MaxDistance {
175
+ candidate := i - int (delta )
176
+ if candidate <= 0 || i - candidate > q .MaxDistance {
175
177
continue
176
178
}
177
179
0 commit comments