Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #17 from ipfs/feat/plus-9
Browse files Browse the repository at this point in the history
Improve performance of buzhash
  • Loading branch information
Stebalien committed Nov 6, 2019
2 parents 21b0c06 + 79bdab2 commit cc99d74
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions 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 cc99d74

Please sign in to comment.