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
Reuse float histogram objects #13276
Conversation
This commit reduces the memory needed to query native histogram objects by reusing existing HPoint instances. Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com>
1719b3c
to
e2a9f8a
Compare
Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com>
9fb3899
to
48df9fc
Compare
My PR for adding quantile_over_time support increased allocations in the matrix selector due to creating a new []float64 slice for each call to the aligner function. This commit resolves that issue by reusing the same slice, and applies the trick in prometheus/prometheus#13276 to further drive down memory usage. Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com>
My PR for adding quantile_over_time support increased allocations in the matrix selector due to creating a new []float64 slice for each call to the aligner function. This commit resolves that issue by reusing the same slice, and applies the trick in prometheus/prometheus#13276 to further drive down memory usage. Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com>
* Reduce allocations in matrix selector My PR for adding quantile_over_time support increased allocations in the matrix selector due to creating a new []float64 slice for each call to the aligner function. This commit resolves that issue by reusing the same slice, and applies the trick in prometheus/prometheus#13276 to further drive down memory usage. Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com> * Fix histograms Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com> --------- Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good in general. Just two smallish nits.
promql/engine.go
Outdated
if n := len(floats); n < cap(floats) { | ||
floats = floats[:n+1] | ||
floats[n].T, floats[n].F = t, f | ||
} else { | ||
floats = append(floats, FPoint{T: t, F: f}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see the reason for the cap
hack above for histograms (so that we can get a previously allocated histogram for re-use), but with float64, we don't need to re-use anything allocated.
Isn't append
under the hood doing the same thing as you have implemented in the 1st branch of the if
clause, provided the capacity is sufficient? Do you see any difference in the benchmark if you don't do this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we could provide a consistent way to resize the buffer for both floats and histograms, and also save an allocation for FPoint
. However, benchmarks show 0% difference so reverting the change makes sense.
return it.t, it.h | ||
} | ||
|
||
func (it *sampleRingIterator) AtFloatHistogram() (int64, *histogram.FloatHistogram) { | ||
func (it *SampleRingIterator) AtFloatHistogram(fh *histogram.FloatHistogram) (int64, *histogram.FloatHistogram) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that SampleRingIterator
doesn't implement chunkenc.Iterator
anymore, it would be good to have doc comments. At least for this method, that now has an additional parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added comments on all methods. I also removed Seek
and Err
because they were never called. Also Seek
was poorly implemented and always returned chunkenc.ValNone
. Let me know if I should revert this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yead, Seek was only implemented to implement the chunkenc.Iterator
interface. But as proven by your change, we don't need that.
Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com>
Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much.
In prometheus#13276 we started reusing float histogram objects to reduce allocations in PromQL. That PR introduces a bug where histogram pointers gets copied to the beginning of the histograms slice, but are still kept in the end of the slice. When a new histogram is read at the last element, it can overwrite a previous element because the pointer is the same. This commit fixes the issue by moving outdated points to the end of the slice so that we don't end up with duplicate pointers in the same buffer. In other words, the slice gets rotated so that old objects can get reused. Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com>
In prometheus#13276 we started reusing float histogram objects to reduce allocations in PromQL. That PR introduces a bug where histogram pointers gets copied to the beginning of the histograms slice, but are still kept in the end of the slice. When a new histogram is read at the last element, it can overwrite a previous element because the pointer is the same. This commit fixes the issue by moving outdated points to the end of the slice so that we don't end up with duplicate pointers in the same buffer. In other words, the slice gets rotated so that old objects can get reused. Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com>
In prometheus#13276 we started reusing float histogram objects to reduce allocations in PromQL. That PR introduces a bug where histogram pointers gets copied to the beginning of the histograms slice, but are still kept in the end of the slice. When a new histogram is read into the last element, it can overwrite a previous element because the pointer is the same. This commit fixes the issue by moving outdated points to the end of the slice so that we don't end up with duplicate pointers in the same buffer. In other words, the slice gets rotated so that old objects can get reused. Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com>
This commit updates Prometheus to commit d0c2d9c which contains the loser-tree based postings merge. Note that we cannot update to latest main since there is currently a bug in PromQL when querying native histograms introduced by prometheus/prometheus#13276. This issue should be fixed by prometheus/prometheus#13289. This commit also updates the Thanos PromQL engine to latest main. Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com>
This commit updates Prometheus to commit d0c2d9c which contains the loser-tree based postings merge. Note that we cannot update to latest main since there is currently a bug in PromQL when querying native histograms introduced by prometheus/prometheus#13276. This issue should be fixed by prometheus/prometheus#13289. This commit also updates the Thanos PromQL engine to latest main. Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com>
This commit updates Prometheus to commit d0c2d9c which contains the loser-tree based postings merge. Note that we cannot update to latest main since there is currently a bug in PromQL when querying native histograms introduced by prometheus/prometheus#13276. This issue should be fixed by prometheus/prometheus#13289. This commit also updates the Thanos PromQL engine to latest main. Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com>
* Update prometheus to d0c2d9c This commit updates Prometheus to commit d0c2d9c which contains the loser-tree based postings merge. Note that we cannot update to latest main since there is currently a bug in PromQL when querying native histograms introduced by prometheus/prometheus#13276. This issue should be fixed by prometheus/prometheus#13289. This commit also updates the Thanos PromQL engine to latest main. Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com> * Fix handler_test.go Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com> * Fix manager_test.go Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com> * Use empty registry for file discovery Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com> --------- Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com>
This commit optimizes queries against native histograms by reusing histogram objects for windowing functions. The same optimization was applied in Prometheus in prometheus/prometheus#13276 and prometheus/prometheus#13289.
This commit optimizes queries against native histograms by reusing histogram objects for windowing functions. The same optimization was applied in Prometheus in prometheus/prometheus#13276 and prometheus/prometheus#13289. Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com>
This commit optimizes queries against native histograms by reusing histogram objects for windowing functions. The same optimization was applied in Prometheus in prometheus/prometheus#13276 and prometheus/prometheus#13289. Signed-off-by: Filip Petkovski <filip.petkovsky@gmail.com>
This commit builds upon #13215, and reduces memory for querying native histogram by reusing existing HPoint instances.
Benchmarks show ~23% improvement in performance and ~45% reduction in memory usage. Queries without
rate
are not affected yet. To make improvements there we will need to change the iterator interface and I will do that in a follow up PR.