Codebase list golang-github-beorn7-perks / d9a9656
quantile: remove allocations Blake Mizerany 9 years ago
2 changed file(s) with 11 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
44 )
55
66 func BenchmarkInsertTargeted(b *testing.B) {
7 b.ReportAllocs()
8
79 s := NewTargeted(0.01, 0.5, 0.9, 0.99)
810 b.ResetTimer()
911 for i := float64(0); i < float64(b.N); i++ {
168168 type stream struct {
169169 epsilon float64
170170 n float64
171 l []*Sample
171 l []Sample
172172 ƒ invariant
173173 }
174174
197197 c := s.l[i]
198198 if c.Value > sample.Value {
199199 // Insert at position i.
200 s.l = append(s.l, nil)
200 s.l = append(s.l, Sample{})
201201 copy(s.l[i+1:], s.l[i:])
202 s.l[i] = &Sample{sample.Value, sample.Width, math.Floor(s.ƒ(s, r)) - 1}
202 s.l[i] = Sample{sample.Value, sample.Width, math.Floor(s.ƒ(s, r)) - 1}
203203 i++
204204 goto inserted
205205 }
206206 r += c.Width
207207 }
208 s.l = append(s.l, &Sample{sample.Value, sample.Width, 0})
208 s.l = append(s.l, Sample{sample.Value, sample.Width, 0})
209209 i++
210210 inserted:
211211 s.n += sample.Width
236236 return
237237 }
238238 x := s.l[len(s.l)-1]
239 xi := len(s.l) - 1
239240 r := s.n - 1 - x.Width
240241
241242 for i := len(s.l) - 2; i >= 0; i-- {
242243 c := s.l[i]
243244 if c.Width+x.Width+x.Delta <= s.ƒ(s, r) {
244245 x.Width += c.Width
246 s.l[xi] = x
245247 // Remove element at i.
246248 copy(s.l[i:], s.l[i+1:])
247 s.l[len(s.l)-1] = nil
248249 s.l = s.l[:len(s.l)-1]
250 xi -= 1
249251 } else {
250252 x = c
253 xi = i
251254 }
252255 r -= c.Width
253256 }
255258
256259 func (s *stream) samples() Samples {
257260 samples := make(Samples, len(s.l))
258 for i, c := range s.l {
259 samples[i] = *c
260 }
261 copy(samples, s.l)
261262 return samples
262263 }