Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize memory allocation in histogram iterators #7319

Closed
wants to merge 1 commit into from

Conversation

duricanikolic
Copy link
Contributor

What this PR does

Prometheus introduced a possibility to optionally reuse the memory area of the given histogram or float histogram to reduce allocation (prometheus/prometheus#13340). We highlight the functions from the chunkenc.Iterator interface, that are of particular importance for the above mentioned change:

type Iterator interface {
	...
	// AtHistogram returns the current timestamp/value pair if the value is a
	// histogram with integer counts. Before the iterator has advanced, the behaviour
	// is unspecified.
	// The method accepts an optional Histogram object which will be
	// reused when not nil. Otherwise, a new Histogram object will be allocated.
	AtHistogram(*histogram.Histogram) (int64, *histogram.Histogram)
	// AtFloatHistogram returns the current timestamp/value pair if the
	// value is a histogram with floating-point counts. It also works if the
	// value is a histogram with integer counts, in which case a
	// FloatHistogram copy of the histogram is returned. Before the iterator
	// has advanced, the behaviour is unspecified.
	// The method accepts an optional FloatHistogram object which will be
	// reused when not nil. Otherwise, a new FloatHistogram object will be allocated.
	AtFloatHistogram(*histogram.FloatHistogram) (int64, *histogram.FloatHistogram)
}

In #7274 we adapted Mimir's chunk.Iterator interface in such a way that its AtHistogram() and AtFloatHistogram() methods follow the signature of the corresponding methods of Prometheus' chunkenc.Iterator, i.e., they now also contain optional arguments of type *histogram.Histogram and *histogram.FloatHistogram that allow memory allocation optimization. Mimir's chunk.prometheusChunkIterator, an implementation of chunk.Iterator, was also updated accordingly.

In this PR we additionally optimize histogram and float histogram memory allocations by modifying chunk.prometheusChunkIterator's implementation of Bulk().

A comparison between the old and the new implementation of Bulk() actually showed an improvement in memory allocation:

                                                                       │   old.txt   │              new.txt               │
                                                                       │   sec/op    │   sec/op     vs base               │
PrometheusChunkIterator_Batch/encoding-PrometheusHistogramChunk-8        7.186m ± 1%   7.110m ± 0%  -1.07% (p=0.000 n=10)
PrometheusChunkIterator_Batch/encoding-PrometheusFloatHistogramChunk-8   10.91m ± 0%   10.88m ± 1%       ~ (p=0.089 n=10)

                                                                       │   old.txt    │               new.txt                │
                                                                       │     B/op     │     B/op      vs base                │
PrometheusChunkIterator_Batch/encoding-PrometheusHistogramChunk-8        1.620Mi ± 0%   1.428Mi ± 0%  -11.87% (p=0.000 n=10)
PrometheusChunkIterator_Batch/encoding-PrometheusFloatHistogramChunk-8   1.977Mi ± 0%   1.784Mi ± 0%   -9.73% (p=0.000 n=10)

                                                                       │   old.txt   │              new.txt               │
                                                                       │  allocs/op  │  allocs/op   vs base               │
PrometheusChunkIterator_Batch/encoding-PrometheusHistogramChunk-8        29.27k ± 0%   26.37k ± 0%  -9.90% (p=0.000 n=10)
PrometheusChunkIterator_Batch/encoding-PrometheusFloatHistogramChunk-8   31.45k ± 0%   28.55k ± 0%  -9.21% (p=0.000 n=10)

Which issue(s) this PR fixes or relates to

Part of #7235

Checklist

  • Tests updated.
  • [na] Documentation added.
  • [na] CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX].
  • [na] about-versioning.md updated with experimental features.

Signed-off-by: Yuri Nikolic <durica.nikolic@grafana.com>
@duricanikolic duricanikolic self-assigned this Feb 7, 2024
@duricanikolic duricanikolic deleted the yuri/chunk-iterator-optimization branch February 7, 2024 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant