@@ -34,7 +34,7 @@ type match struct {
34
34
est int32
35
35
}
36
36
37
- const highScore = 25000
37
+ const highScore = maxMatchLen * 8
38
38
39
39
// estBits will estimate output bits from predefined tables.
40
40
func (m * match ) estBits (bitsPerByte int32 ) {
@@ -201,6 +201,9 @@ encodeLoop:
201
201
return
202
202
}
203
203
if debugAsserts {
204
+ if offset <= 0 {
205
+ panic (offset )
206
+ }
204
207
if ! bytes .Equal (src [s :s + 4 ], src [offset :offset + 4 ]) {
205
208
panic (fmt .Sprintf ("first match mismatch: %v != %v, first: %08x" , src [s :s + 4 ], src [offset :offset + 4 ], first ))
206
209
}
@@ -291,37 +294,41 @@ encodeLoop:
291
294
continue
292
295
}
293
296
294
- s := s + 1
295
297
candidateS = e .table [hashLen (cv >> 8 , bestShortTableBits , bestShortLen )]
296
- cv = load6432 (src , s )
297
- cv2 := load6432 (src , s + 1 )
298
+ cv = load6432 (src , s + 1 )
299
+ cv2 := load6432 (src , s + 2 )
298
300
candidateL = e .longTable [hashLen (cv , bestLongTableBits , bestLongLen )]
299
301
candidateL2 := e .longTable [hashLen (cv2 , bestLongTableBits , bestLongLen )]
300
302
301
303
// Short at s+1
302
- improve (& best , candidateS .offset - e .cur , s , uint32 (cv ), - 1 )
304
+ improve (& best , candidateS .offset - e .cur , s + 1 , uint32 (cv ), - 1 )
303
305
// Long at s+1, s+2
304
- improve (& best , candidateL .offset - e .cur , s , uint32 (cv ), - 1 )
305
- improve (& best , candidateL .prev - e .cur , s , uint32 (cv ), - 1 )
306
- improve (& best , candidateL2 .offset - e .cur , s + 1 , uint32 (cv2 ), - 1 )
307
- improve (& best , candidateL2 .prev - e .cur , s + 1 , uint32 (cv2 ), - 1 )
306
+ improve (& best , candidateL .offset - e .cur , s + 1 , uint32 (cv ), - 1 )
307
+ improve (& best , candidateL .prev - e .cur , s + 1 , uint32 (cv ), - 1 )
308
+ improve (& best , candidateL2 .offset - e .cur , s + 2 , uint32 (cv2 ), - 1 )
309
+ improve (& best , candidateL2 .prev - e .cur , s + 2 , uint32 (cv2 ), - 1 )
308
310
if false {
309
311
// Short at s+3.
310
312
// Too often worse...
311
- improve (& best , e .table [hashLen (cv2 >> 8 , bestShortTableBits , bestShortLen )].offset - e .cur , s + 2 , uint32 (cv2 >> 8 ), - 1 )
313
+ improve (& best , e .table [hashLen (cv2 >> 8 , bestShortTableBits , bestShortLen )].offset - e .cur , s + 3 , uint32 (cv2 >> 8 ), - 1 )
312
314
}
313
- // See if we can find a better match by checking where the current best ends.
314
- // Use that offset to see if we can find a better full match.
315
- if sAt := best .s + best .length ; sAt < sLimit {
316
- nextHashL := hashLen (load6432 (src , sAt ), bestLongTableBits , bestLongLen )
317
- candidateEnd := e .longTable [nextHashL ]
318
- // Start check at a fixed offset to allow for a few mismatches.
319
- // For this compression level 2 yields the best results.
320
- const skipBeginning = 2
321
- if pos := candidateEnd .offset - e .cur - best .length + skipBeginning ; pos >= 0 {
322
- improve (& best , pos , best .s + skipBeginning , load3232 (src , best .s + skipBeginning ), - 1 )
323
- if pos := candidateEnd .prev - e .cur - best .length + skipBeginning ; pos >= 0 {
324
- improve (& best , pos , best .s + skipBeginning , load3232 (src , best .s + skipBeginning ), - 1 )
315
+
316
+ // Start check at a fixed offset to allow for a few mismatches.
317
+ // For this compression level 2 yields the best results.
318
+ // We cannot do this if we have already indexed this position.
319
+ const skipBeginning = 2
320
+ if best .s > s - skipBeginning {
321
+ // See if we can find a better match by checking where the current best ends.
322
+ // Use that offset to see if we can find a better full match.
323
+ if sAt := best .s + best .length ; sAt < sLimit {
324
+ nextHashL := hashLen (load6432 (src , sAt ), bestLongTableBits , bestLongLen )
325
+ candidateEnd := e .longTable [nextHashL ]
326
+
327
+ if off := candidateEnd .offset - e .cur - best .length + skipBeginning ; off >= 0 {
328
+ improve (& best , off , best .s + skipBeginning , load3232 (src , best .s + skipBeginning ), - 1 )
329
+ if off := candidateEnd .prev - e .cur - best .length + skipBeginning ; off >= 0 {
330
+ improve (& best , off , best .s + skipBeginning , load3232 (src , best .s + skipBeginning ), - 1 )
331
+ }
325
332
}
326
333
}
327
334
}
0 commit comments