Skip to content

Commit 045ce67

Browse files
authored
Merge pull request ipfs/go-ipfs-chunker#17 from ipfs/feat/plus-9
Improve performance of buzhash This commit was moved from ipfs/go-ipfs-chunker@cc99d74
2 parents dfcd2f2 + fb1137a commit 045ce67

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

chunker/buzhash.go

+21-5
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,33 @@ func (b *Buzhash) NextBytes() ([]byte, error) {
6161

6262
var state uint32 = 0
6363

64+
if buzMin > len(b.buf) {
65+
panic("this is impossible")
66+
}
67+
6468
for ; i < buzMin; i++ {
6569
state = bits.RotateLeft32(state, 1)
6670
state = state ^ bytehash[b.buf[i]]
6771
}
6872

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

73-
for ; state&buzMask != 0 && i < b.n+n; i++ {
74-
state = bits.RotateLeft32(state, 1) ^ bytehash[b.buf[i-32]] ^ bytehash[b.buf[i]]
76+
buf := b.buf
77+
bufshf := b.buf[32:]
78+
i = buzMin - 32
79+
_ = buf[max]
80+
_ = bufshf[max]
81+
82+
for ; i <= max; i++ {
83+
if state&buzMask == 0 {
84+
break
85+
}
86+
state = bits.RotateLeft32(state, 1) ^
87+
bytehash[buf[i]] ^
88+
bytehash[bufshf[i]]
89+
}
90+
i += 32
7591
}
7692

7793
res := make([]byte, i)

0 commit comments

Comments
 (0)