blob: 0cde31dd9b003f38a831a99d85a75eb41ec09a8b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
package cryptocore
import (
"bytes"
"log"
"sync"
)
// Number of bytes to prefetch.
// 512 looks like a good compromise between throughput and latency - see
// randsize_test.go for numbers.
const prefetchN = 512
func init() {
randPrefetcher.refill = make(chan []byte)
go randPrefetcher.refillWorker()
}
type randPrefetcherT struct {
sync.Mutex
buf bytes.Buffer
refill chan []byte
}
func (r *randPrefetcherT) read(want int) (out []byte) {
out = make([]byte, want)
r.Lock()
// Note: don't use defer, it slows us down!
have, err := r.buf.Read(out)
if have == want && err == nil {
r.Unlock()
return out
}
// Buffer was empty -> re-fill
fresh := <-r.refill
if len(fresh) != prefetchN {
log.Panicf("randPrefetcher: refill: got %d bytes instead of %d", len(fresh), prefetchN)
}
r.buf.Reset()
r.buf.Write(fresh)
have, err = r.buf.Read(out)
if have != want || err != nil {
log.Panicf("randPrefetcher could not satisfy read: have=%d want=%d err=%v", have, want, err)
}
r.Unlock()
return out
}
func (r *randPrefetcherT) refillWorker() {
for {
r.refill <- RandBytes(prefetchN)
}
}
var randPrefetcher randPrefetcherT
|