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

Improve performance of buzhash #17

Merged
merged 1 commit into from
Nov 6, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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