Merge pull request #13276 from fpetkovski/reuse-float-histograms

Reuse float histogram objects
This commit is contained in:
Björn Rabenstein 2023-12-13 13:43:01 +01:00 committed by GitHub
commit 775de1a3bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 30 deletions

View file

@ -2078,21 +2078,27 @@ loop:
case chunkenc.ValNone: case chunkenc.ValNone:
break loop break loop
case chunkenc.ValFloatHistogram, chunkenc.ValHistogram: case chunkenc.ValFloatHistogram, chunkenc.ValHistogram:
t, h := buf.AtFloatHistogram() t := buf.AtT()
if value.IsStaleNaN(h.Sum) {
continue loop
}
// Values in the buffer are guaranteed to be smaller than maxt. // Values in the buffer are guaranteed to be smaller than maxt.
if t >= mintHistograms { if t >= mintHistograms {
if ev.currentSamples >= ev.maxSamples {
ev.error(ErrTooManySamples(env))
}
point := HPoint{T: t, H: h}
if histograms == nil { if histograms == nil {
histograms = getHPointSlice(16) histograms = getHPointSlice(16)
} }
histograms = append(histograms, point) n := len(histograms)
ev.currentSamples += point.size() if n < cap(histograms) {
histograms = histograms[:n+1]
} else {
histograms = append(histograms, HPoint{})
}
histograms[n].T, histograms[n].H = buf.AtFloatHistogram(histograms[n].H)
if value.IsStaleNaN(histograms[n].H.Sum) {
histograms = histograms[:n]
continue loop
}
if ev.currentSamples >= ev.maxSamples {
ev.error(ErrTooManySamples(env))
}
ev.currentSamples += histograms[n].size()
} }
case chunkenc.ValFloat: case chunkenc.ValFloat:
t, f := buf.At() t, f := buf.At()

View file

@ -74,7 +74,7 @@ func (b *BufferedSeriesIterator) PeekBack(n int) (sample chunks.Sample, ok bool)
// Buffer returns an iterator over the buffered data. Invalidates previously // Buffer returns an iterator over the buffered data. Invalidates previously
// returned iterators. // returned iterators.
func (b *BufferedSeriesIterator) Buffer() chunkenc.Iterator { func (b *BufferedSeriesIterator) Buffer() *SampleRingIterator {
return b.buf.iterator() return b.buf.iterator()
} }
@ -252,7 +252,7 @@ type sampleRing struct {
f int // Position of first element in ring buffer. f int // Position of first element in ring buffer.
l int // Number of elements in buffer. l int // Number of elements in buffer.
it sampleRingIterator it SampleRingIterator
} }
type bufType int type bufType int
@ -304,13 +304,15 @@ func (r *sampleRing) reset() {
} }
// Returns the current iterator. Invalidates previously returned iterators. // Returns the current iterator. Invalidates previously returned iterators.
func (r *sampleRing) iterator() chunkenc.Iterator { func (r *sampleRing) iterator() *SampleRingIterator {
r.it.r = r r.it.r = r
r.it.i = -1 r.it.i = -1
return &r.it return &r.it
} }
type sampleRingIterator struct { // SampleRingIterator is returned by BufferedSeriesIterator.Buffer() and can be
// used to iterate samples buffered in the lookback window.
type SampleRingIterator struct {
r *sampleRing r *sampleRing
i int i int
t int64 t int64
@ -319,7 +321,7 @@ type sampleRingIterator struct {
fh *histogram.FloatHistogram fh *histogram.FloatHistogram
} }
func (it *sampleRingIterator) Next() chunkenc.ValueType { func (it *SampleRingIterator) Next() chunkenc.ValueType {
it.i++ it.i++
if it.i >= it.r.l { if it.i >= it.r.l {
return chunkenc.ValNone return chunkenc.ValNone
@ -358,30 +360,28 @@ func (it *sampleRingIterator) Next() chunkenc.ValueType {
} }
} }
func (it *sampleRingIterator) Seek(int64) chunkenc.ValueType { // At returns the current float element of the iterator.
return chunkenc.ValNone func (it *SampleRingIterator) At() (int64, float64) {
}
func (it *sampleRingIterator) Err() error {
return nil
}
func (it *sampleRingIterator) At() (int64, float64) {
return it.t, it.f return it.t, it.f
} }
func (it *sampleRingIterator) AtHistogram() (int64, *histogram.Histogram) { // AtHistogram returns the current histogram element of the iterator.
func (it *SampleRingIterator) AtHistogram() (int64, *histogram.Histogram) {
return it.t, it.h return it.t, it.h
} }
func (it *sampleRingIterator) AtFloatHistogram() (int64, *histogram.FloatHistogram) { // AtFloatHistogram returns the current histogram element of the iterator. If the
// current sample is an integer histogram, it will be converted to a float histogram.
// An optional histogram.FloatHistogram can be provided to avoid allocating a new
// object for the conversion.
func (it *SampleRingIterator) AtFloatHistogram(fh *histogram.FloatHistogram) (int64, *histogram.FloatHistogram) {
if it.fh == nil { if it.fh == nil {
return it.t, it.h.ToFloat(nil) return it.t, it.h.ToFloat(fh)
} }
return it.t, it.fh return it.t, it.fh
} }
func (it *sampleRingIterator) AtT() int64 { func (it *SampleRingIterator) AtT() int64 {
return it.t return it.t
} }

View file

@ -243,11 +243,11 @@ func TestBufferedSeriesIteratorMixedHistograms(t *testing.T) {
buf := it.Buffer() buf := it.Buffer()
require.Equal(t, chunkenc.ValFloatHistogram, buf.Next()) require.Equal(t, chunkenc.ValFloatHistogram, buf.Next())
_, fh := buf.AtFloatHistogram() _, fh := buf.AtFloatHistogram(nil)
require.Equal(t, histograms[0].ToFloat(nil), fh) require.Equal(t, histograms[0].ToFloat(nil), fh)
require.Equal(t, chunkenc.ValHistogram, buf.Next()) require.Equal(t, chunkenc.ValHistogram, buf.Next())
_, fh = buf.AtFloatHistogram() _, fh = buf.AtFloatHistogram(nil)
require.Equal(t, histograms[1].ToFloat(nil), fh) require.Equal(t, histograms[1].ToFloat(nil), fh)
} }