Do not exit if buffer is not full.

License: MIT
Signed-off-by: default avatarJakub Sztandera <kubuxu@protocol.ai>

License: MIT
Signed-off-by: default avatarJakub Sztandera <kubuxu@protocol.ai>
parent 25cb45d1
...@@ -40,14 +40,16 @@ func (b *Buzhash) NextBytes() ([]byte, error) { ...@@ -40,14 +40,16 @@ func (b *Buzhash) NextBytes() ([]byte, error) {
buf := b.buf buf := b.buf
n, err := io.ReadFull(b.r, buf[b.n:]) n, err := io.ReadFull(b.r, buf[b.n:])
if err != nil { if err != nil {
if err == io.ErrUnexpectedEOF { if err == io.ErrUnexpectedEOF || err == io.EOF {
b.err = io.EOF if b.n+n < buzMin {
res := make([]byte, n+b.n) b.err = io.EOF
copy(res, buf) res := make([]byte, b.n+n)
copy(res, buf)
pool.Put(b.buf)
b.buf = nil pool.Put(b.buf)
return res, nil b.buf = nil
return res, nil
}
} else { } else {
b.err = err b.err = err
pool.Put(buf) pool.Put(buf)
...@@ -65,14 +67,18 @@ func (b *Buzhash) NextBytes() ([]byte, error) { ...@@ -65,14 +67,18 @@ func (b *Buzhash) NextBytes() ([]byte, error) {
state = state ^ bytehash[buf[i]] state = state ^ bytehash[buf[i]]
} }
for ; state&buzMask != 0 && i < buzMax; i++ { if b.n+n > len(buf) {
panic("this is impossible, but gives +9 to performance")
}
for ; state&buzMask != 0 && i < b.n+n; i++ {
state = bits.RotateLeft32(state, 1) ^ bytehash[buf[i-32]] ^ bytehash[buf[i]] state = bits.RotateLeft32(state, 1) ^ bytehash[buf[i-32]] ^ bytehash[buf[i]]
} }
res := make([]byte, i) res := make([]byte, i)
copy(res, b.buf) copy(res, b.buf)
b.n = copy(b.buf, buf[i:]) b.n = copy(b.buf, buf[i:b.n+n])
return res, nil return res, nil
} }
......
...@@ -53,7 +53,7 @@ func BenchmarkBuzhash2(b *testing.B) { ...@@ -53,7 +53,7 @@ func BenchmarkBuzhash2(b *testing.B) {
}) })
} }
func TestBuzhashBitsHash(t *testing.T) { func TestBuzhashBitsHashBias(t *testing.T) {
counts := make([]byte, 32) counts := make([]byte, 32)
for _, h := range bytehash { for _, h := range bytehash {
for i := 0; i < 32; i++ { for i := 0; i < 32; i++ {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment