Skip to content

Commit

Permalink
Merge pull request ipfs/go-ipfs-chunker#17 from ipfs/feat/plus-9
Browse files Browse the repository at this point in the history
Improve performance of buzhash

This commit was moved from ipfs/go-ipfs-chunker@cc99d74
  • Loading branch information
Stebalien committed Nov 6, 2019
2 parents dfcd2f2 + fb1137a commit 045ce67
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions chunker/buzhash.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,33 @@ func (b *Buzhash) NextBytes() ([]byte, error) {

var state uint32 = 0

if buzMin > len(b.buf) {
panic("this is impossible")
}

for ; i < buzMin; i++ {
state = bits.RotateLeft32(state, 1)
state = state ^ bytehash[b.buf[i]]
}

if b.n+n > len(b.buf) {
panic("this is impossible, but gives +9 to performance")
}
{
max := b.n + n - 32 - 1

for ; state&buzMask != 0 && i < b.n+n; i++ {
state = bits.RotateLeft32(state, 1) ^ bytehash[b.buf[i-32]] ^ bytehash[b.buf[i]]
buf := b.buf
bufshf := b.buf[32:]
i = buzMin - 32
_ = buf[max]
_ = bufshf[max]

for ; i <= max; i++ {
if state&buzMask == 0 {
break
}
state = bits.RotateLeft32(state, 1) ^
bytehash[buf[i]] ^
bytehash[bufshf[i]]
}
i += 32
}

res := make([]byte, i)
Expand Down

0 comments on commit 045ce67

Please sign in to comment.