This repository was archived by the owner on Jun 20, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +17
-11
lines changed Expand file tree Collapse file tree 2 files changed +17
-11
lines changed Original file line number Diff line number Diff line change @@ -40,14 +40,16 @@ func (b *Buzhash) NextBytes() ([]byte, error) {
40
40
buf := b .buf
41
41
n , err := io .ReadFull (b .r , buf [b .n :])
42
42
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
+ }
51
53
} else {
52
54
b .err = err
53
55
pool .Put (buf )
@@ -65,14 +67,18 @@ func (b *Buzhash) NextBytes() ([]byte, error) {
65
67
state = state ^ bytehash [buf [i ]]
66
68
}
67
69
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 ++ {
69
75
state = bits .RotateLeft32 (state , 1 ) ^ bytehash [buf [i - 32 ]] ^ bytehash [buf [i ]]
70
76
}
71
77
72
78
res := make ([]byte , i )
73
79
copy (res , b .buf )
74
80
75
- b .n = copy (b .buf , buf [i :])
81
+ b .n = copy (b .buf , buf [i :b . n + n ])
76
82
77
83
return res , nil
78
84
}
Original file line number Diff line number Diff line change @@ -53,7 +53,7 @@ func BenchmarkBuzhash2(b *testing.B) {
53
53
})
54
54
}
55
55
56
- func TestBuzhashBitsHash (t * testing.T ) {
56
+ func TestBuzhashBitsHashBias (t * testing.T ) {
57
57
counts := make ([]byte , 32 )
58
58
for _ , h := range bytehash {
59
59
for i := 0 ; i < 32 ; i ++ {
You can’t perform that action at this time.
0 commit comments