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

Commit 8cdd6f4

Browse files
committed
License: MIT
Signed-off-by: Jakub Sztandera <[email protected]>
1 parent 25cb45d commit 8cdd6f4

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

buzhash.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ func (b *Buzhash) NextBytes() ([]byte, error) {
4040
buf := b.buf
4141
n, err := io.ReadFull(b.r, buf[b.n:])
4242
if err != nil {
43-
if err == io.ErrUnexpectedEOF {
44-
b.err = io.EOF
45-
res := make([]byte, n+b.n)
46-
copy(res, buf)
47-
48-
pool.Put(b.buf)
49-
b.buf = nil
50-
return res, nil
43+
if err == io.ErrUnexpectedEOF || err == io.EOF {
44+
if b.n+n < buzMin {
45+
b.err = io.EOF
46+
res := make([]byte, b.n+n)
47+
copy(res, buf)
48+
49+
pool.Put(b.buf)
50+
b.buf = nil
51+
return res, nil
52+
}
5153
} else {
5254
b.err = err
5355
pool.Put(buf)
@@ -65,14 +67,18 @@ func (b *Buzhash) NextBytes() ([]byte, error) {
6567
state = state ^ bytehash[buf[i]]
6668
}
6769

68-
for ; state&buzMask != 0 && i < buzMax; i++ {
70+
if b.n+n > len(buf) {
71+
panic("this is impossible, but gives +9 to performance")
72+
}
73+
74+
for ; state&buzMask != 0 && i < b.n+n; i++ {
6975
state = bits.RotateLeft32(state, 1) ^ bytehash[buf[i-32]] ^ bytehash[buf[i]]
7076
}
7177

7278
res := make([]byte, i)
7379
copy(res, b.buf)
7480

75-
b.n = copy(b.buf, buf[i:])
81+
b.n = copy(b.buf, buf[i:b.n+n])
7682

7783
return res, nil
7884
}

buzhash_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func BenchmarkBuzhash2(b *testing.B) {
5353
})
5454
}
5555

56-
func TestBuzhashBitsHash(t *testing.T) {
56+
func TestBuzhashBitsHashBias(t *testing.T) {
5757
counts := make([]byte, 32)
5858
for _, h := range bytehash {
5959
for i := 0; i < 32; i++ {

0 commit comments

Comments
 (0)