Skip to content

Commit

Permalink
Don't stop the timer. Makes benchmarks take forever. Simplify Endless…
Browse files Browse the repository at this point in the history
…Reader
  • Loading branch information
klauspost committed Sep 16, 2024
1 parent 0ed83b8 commit f9ced1d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
14 changes: 8 additions & 6 deletions msgp/circular.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ type EndlessReader struct {

// NewEndlessReader returns a new endless reader
func NewEndlessReader(b []byte, tb timer) *EndlessReader {
// Double until we reach 4K.
for len(b) < 4<<10 {
b = append(b, b...)
}
return &EndlessReader{tb: tb, data: b, offset: 0}
}

Expand All @@ -24,16 +28,14 @@ func NewEndlessReader(b []byte, tb timer) *EndlessReader {
// fills the supplied slice while the benchmark
// timer is stopped.
func (c *EndlessReader) Read(p []byte) (int, error) {
c.tb.StopTimer()
var n int
l := len(p)
m := len(c.data)
nn := copy(p[n:], c.data[c.offset:])
n += nn
for n < l {
nn := copy(p[n:], c.data[c.offset:])
n += nn
c.offset += nn
c.offset %= m
n += copy(p[n:], c.data[:])
}
c.tb.StartTimer()
c.offset = (c.offset + l) % m
return n, nil
}
3 changes: 2 additions & 1 deletion msgp/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ var (

func RandBytes(sz int) []byte {
out := make([]byte, sz)
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := range out {
out[i] = byte(rand.Int63n(math.MaxInt64) % 256)
out[i] = byte(rng.Uint32())
}
return out
}
Expand Down

0 comments on commit f9ced1d

Please sign in to comment.