diff --git a/s2/encode_all.go b/s2/encode_all.go index f1d27b6a8d..d5878d1821 100644 --- a/s2/encode_all.go +++ b/s2/encode_all.go @@ -12,16 +12,11 @@ import ( ) func load32(b []byte, i int) uint32 { - b = b[i:] - b = b[:4] - return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24 + return binary.LittleEndian.Uint32(b[i:]) } func load64(b []byte, i int) uint64 { - b = b[i:] - b = b[:8] - return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | - uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56 + return binary.LittleEndian.Uint64(b[i:]) } // hash6 returns the hash of the lowest 6 bytes of u to fit in a hash table with h bits. diff --git a/s2/encode_best.go b/s2/encode_best.go index 4b571bf316..9a9587036c 100644 --- a/s2/encode_best.go +++ b/s2/encode_best.go @@ -90,16 +90,17 @@ func encodeBlockBest(dst, src []byte) (d int) { if load32(src, offset) != first { return match{offset: offset, s: s} } - m := match{offset: offset, s: s, length: 4, rep: rep} + m := match{offset: offset, s: s, length: 4 + offset, rep: rep} s += 4 for s <= sLimit { - if diff := load64(src, s) ^ load64(src, offset+m.length); diff != 0 { + if diff := load64(src, s) ^ load64(src, m.length); diff != 0 { m.length += bits.TrailingZeros64(diff) >> 3 break } s += 8 m.length += 8 } + m.length -= offset return m } @@ -150,7 +151,7 @@ func encodeBlockBest(dst, src []byte) (d int) { best = bestOf(best, matchAt(s-repeat+1, s+1, uint32(cv>>8), true)) // s+2 - if true { + if best.length < 100 { nextShort = sTable[hash4(cv>>8, sTableBits)] s++ cv = load64(src, s)