Skip to content

Commit

Permalink
s2: Clean up and minor performance improvement in best (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost authored Mar 22, 2021
1 parent a0dc84a commit 1454288
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
9 changes: 2 additions & 7 deletions s2/encode_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions s2/encode_best.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1454288

Please sign in to comment.