-
Notifications
You must be signed in to change notification settings - Fork 7
/
reservoir_buffer.go
142 lines (117 loc) · 3.09 KB
/
reservoir_buffer.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package deepcfr
import (
"bytes"
"encoding/gob"
"sync"
"sync/atomic"
)
// ReservoirBuffer is a collection of samples held in memory.
// One the buffer's max size is reached, additional
// samples are added via reservoir sampling, maintaining
// a uniform distribution over all previous values.
//
// It is safe to call AddSample concurrently from multiple goroutines.
// GetSamples does not copy the underlying slice of samples, and therefore
// is not safe to call concurrently with AddSample.
type ReservoirBuffer struct {
mx sync.Mutex
maxSize int
maxParallel int
samples []Sample
n int64
rngPool randPool
}
// NewBuffer returns an empty Buffer with the given max size.
func NewReservoirBuffer(maxSize, maxParallel int) *ReservoirBuffer {
return &ReservoirBuffer{
maxSize: maxSize,
maxParallel: maxParallel,
samples: make([]Sample, maxSize),
rngPool: newRandPool(2 * maxParallel),
}
}
// AddSample implements Buffer.
func (b *ReservoirBuffer) AddSample(sample Sample) {
// We a are a little bit sloppy here for improved performance:
// Because we do not hold a lock for the duration of the call, it is possible
// for an earlier call to AddSample to collide and overwrite a later call
// if both are simultaneously assigned to the same random bucket.
n := int(atomic.AddInt64(&b.n, 1))
if n <= b.maxSize {
b.mx.Lock()
b.samples[n-1] = sample
b.mx.Unlock()
} else if m := b.rngPool.Intn(n); m < b.maxSize {
b.mx.Lock()
b.samples[m] = sample
b.mx.Unlock()
}
}
// GetSample implements Buffer.
func (b *ReservoirBuffer) GetSample(idx int) Sample {
b.mx.Lock()
defer b.mx.Unlock()
return b.samples[idx]
}
func (b *ReservoirBuffer) Len() int {
return int(atomic.LoadInt64(&b.n))
}
// GetSamples implements Buffer.
func (b *ReservoirBuffer) GetSamples() []Sample {
n := int(atomic.LoadInt64(&b.n))
nSamples := min(n, b.maxSize)
result := make([]Sample, nSamples)
b.mx.Lock()
defer b.mx.Unlock()
copy(result, b.samples)
return result
}
func min(i, j int) int {
if i < j {
return i
}
return j
}
func (b *ReservoirBuffer) Close() error {
return nil
}
// MarshalBinary implements encoding.BinaryMarshaler.
func (b *ReservoirBuffer) MarshalBinary() ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
if err := enc.Encode(b.maxSize); err != nil {
return nil, err
}
if err := enc.Encode(b.maxParallel); err != nil {
return nil, err
}
if err := enc.Encode(b.n); err != nil {
return nil, err
}
if err := enc.Encode(b.samples); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
func (b *ReservoirBuffer) UnmarshalBinary(buf []byte) error {
r := bytes.NewReader(buf)
dec := gob.NewDecoder(r)
if err := dec.Decode(&b.maxSize); err != nil {
return err
}
if err := dec.Decode(&b.maxParallel); err != nil {
return err
}
if err := dec.Decode(&b.n); err != nil {
return err
}
if err := dec.Decode(&b.samples); err != nil {
return err
}
b.rngPool = newRandPool(b.maxParallel)
return nil
}
func init() {
gob.Register(&ReservoirBuffer{})
}