Skip to content

Commit fb1137a

Browse files
committed
Improve performance of buzhash
``` name old time/op new time/op delta Buzhash2/1K-4 610ns ± 4% 643ns ±16% ~ (p=0.421 n=8+10) Buzhash2/1M-4 1.25ms ± 5% 1.16ms ± 4% -7.31% (p=0.000 n=10+10) Buzhash2/16M-4 19.2ms ± 2% 17.5ms ± 2% -8.73% (p=0.000 n=9+9) Buzhash2/100M-4 117ms ± 1% 107ms ± 3% -8.26% (p=0.000 n=10+10) name old speed new speed delta Buzhash2/1K-4 1.68GB/s ± 4% 1.60GB/s ±14% ~ (p=0.408 n=8+10) Buzhash2/1M-4 842MB/s ± 5% 908MB/s ± 3% +7.86% (p=0.000 n=10+10) Buzhash2/16M-4 875MB/s ± 2% 959MB/s ± 2% +9.57% (p=0.000 n=9+9) Buzhash2/100M-4 897MB/s ± 1% 977MB/s ± 3% +9.02% (p=0.000 n=10+10) name old alloc/op new alloc/op delta Buzhash2/1K-4 1.17kB ± 1% 1.17kB ± 0% -0.50% (p=0.006 n=10+10) Buzhash2/1M-4 1.08MB ± 1% 1.07MB ± 0% ~ (p=0.739 n=10+10) Buzhash2/16M-4 17.1MB ± 0% 17.1MB ± 0% ~ (p=0.579 n=10+10) Buzhash2/100M-4 106MB ± 0% 106MB ± 0% -0.01% (p=0.000 n=9+7) name old allocs/op new allocs/op delta Buzhash2/1K-4 3.00 ± 0% 3.00 ± 0% ~ (all equal) Buzhash2/1M-4 8.00 ± 0% 8.00 ± 0% ~ (all equal) Buzhash2/16M-4 72.0 ± 0% 72.0 ± 0% ~ (all equal) Buzhash2/100M-4 406 ± 0% 406 ± 0% ~ (all equal) ``` License: MIT Signed-off-by: Jakub Sztandera <[email protected]> This commit was moved from ipfs/go-ipfs-chunker@79bdab2
1 parent dfcd2f2 commit fb1137a

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)