- Notifications
You must be signed in to change notification settings - Fork10k
promql: avoid unnecessaryMetric.Get() calls infunctions.go#17676
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
promql: avoid unnecessaryMetric.Get() calls infunctions.go#17676
Conversation
Signed-off-by: Vilius Pranckaitis <vpranckaitis@gmail.com>
promql/functions.go Outdated
| // In case of a counter reset, we leave resultSample at | ||
| // its current value, which is already ss[1]. | ||
| casess[1].H!=nil&&ss[0].H!=nil: | ||
| metricName:=samples.Metric.Get(model.MetricNameLabel) |
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 considered pushing this further down into the 3 branches branches below, though wasn't sure if this wouldn't be too excessive.
bboreham commentedDec 12, 2025
Please show benchmark results, as requested in the PR template. |
bboreham left a comment
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.
Thanks for this; I think it's a good idea.
I would rather defer evaluation as far as possible, e.g. intohistogramRate, and add a small helper function to shorten the code where used. Then don't create a new variablemetricName anywhere it would only be used once.
vpranckaitis commentedDec 12, 2025
Did not write a separate benchmark, but used the exisitng The benchmark results show 1–6% reduction in runtime, and practically no change in allocations. |
vpranckaitis commentedDec 15, 2025
@bboreham I've refactored the code to use Also, pushed the metric name extraction further down into |
Moved some
Metric.Get()calls in PromQL functions to avoid unnecessary label extraction. Withstringlabelsthat is a non-trivial amount of work, as shown by CPU profiles. In many cases, this work was done to extract metric name, and was only used if annotations were emitted.In the same go I also replaced
labels.MetricNamewithmodel.MetricNameLabel, since the former was deprecated.Which issue(s) does the PR fix:
Does this PR introduce a user-facing change?