Skip to content

perf(PromQL): skip histogram buckets for timestamp, start_timestamp, absent - #19738

Merged
krajorama merged 1 commit into
prometheus:mainfrom
linasm:skip-hisotgram-buckets-for-more-functions
Oct 20, 2026
Merged

krajorama merged 1 commit into
prometheus:mainfrom
linasm:skip-hisotgram-buckets-for-more-functions

Conversation

@linasm

@linasm linasm commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

Include timestamp, start_timestamp and absent under SkipHistogramBuckets optimization as these functions do not depend on the actual histogram values (and, therefore, can benefit from skipping their buckets).

Benchmark results:

goos: darwin
goarch: arm64
pkg: github.com/prometheus/prometheus/promql
cpu: Apple M4 Pro
                                  │ /tmp/bench_old_ts.txt │        /tmp/bench_new_ts.txt        │
                                  │        sec/op         │   sec/op     vs base                │
NativeHistograms/sum_timestamp-14             269.9m ± 6%   240.5m ± 5%  -10.89% (p=0.000 n=10)

                                  │ /tmp/bench_old_ts.txt │        /tmp/bench_new_ts.txt         │
                                  │         B/op          │     B/op      vs base                │
NativeHistograms/sum_timestamp-14            503.9Mi ± 0%   374.7Mi ± 0%  -25.64% (p=0.000 n=10)

                                  │ /tmp/bench_old_ts.txt │        /tmp/bench_new_ts.txt        │
                                  │       allocs/op       │  allocs/op   vs base                │
NativeHistograms/sum_timestamp-14             7.999M ± 0%   2.314M ± 0%  -71.07% (p=0.000 n=10)

Note that there are two classes of other functions that could be considered for SkipHistogramBuckets optimization as well (but they are not included in this PR on purpose):

  1. Range functions that do not depend on histogram values (such as count_over_time, present_over_time, absent_over_time, and the ts_of_*_over_time family) - it would make sense to include them. But currently, if included naively, they suffer a benchmark regression, so this would require some additional effort.
  2. All the other PromQL functions that only support floats - they would benefit in terms of performance/execution cost, but here we are talking about meaningless queries that try to call float-only functions on native histograms. I am leaving this for consideration for now.

Release notes for end users (ALL commits must be considered).

Reviewers should verify clarity and quality.

[PERF] PromQL: Decode only histogram sum/count (not buckets) for timestamp(), start_timestamp() and absent().

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. One comment for general audience which is unrelated to this change and doesn't block merging.

Comment thread promql/engine_test.go Resolved
Comment thread promql/engine.go Resolved
@linasm
linasm force-pushed the skip-hisotgram-buckets-for-more-functions branch from ff02029 to 04b66b1 Compare October 16, 2026 13:29
@linasm
linasm requested a review from krajorama October 16, 2026 13:31

ghost left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

LLM claims an issue with gauge histograms, but I think it's minor enough to not block this PR:

load 1m
  g{case="gauge"} {{schema:0 count:4 sum:10 buckets:[1 2 1] counter_reset_hint:gauge}} {{schema:0 count:5 sum:10 buckets:[2 1 2] counter_reset_hint:gauge}}

eval instant at 1m timestamp(resets(g[5m]) > 0)
  {case="gauge"} 60

eval instant at 1m absent(resets(g{case="gauge"}[5m]) > 0)

This passes on main and not on this PR.

@linasm

ghost commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

LLM claims an issue with gauge histograms, but I think it's minor enough to not block this PR:

I could exclude queries with resets from "skip buckets" optimization, but the docs say that

resets should only be used with counters (for both floats and histograms)

Besides, the issue with gauge histograms being unable to detect a reset after stripping the buckets already manifests on main, with queries like:

histogram_sum(increase(g[5m]))
histogram_count(rate(g[5m]))   

Anyways, I am open to making further changes, just let me know the intended direction.

@krajorama

ghost commented Oct 20, 2026

Copy link
Copy Markdown
Member

I'll just merge this @linasm , resets in gauges don't mean anything anyway.

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.

4 participants