Skip to content

perf: improve histogram fold plan#7937

Open
waynexia wants to merge 8 commits intoGreptimeTeam:mainfrom
waynexia:prom-plan
Open

perf: improve histogram fold plan#7937
waynexia wants to merge 8 commits intoGreptimeTeam:mainfrom
waynexia:prom-plan

Conversation

@waynexia
Copy link
Copy Markdown
Member

@waynexia waynexia commented Apr 8, 2026

Signed-off-by: Ruihang Xia waynestxia@gmail.comI hereby agree to the terms of the GreptimeDB CLA.

Refer to a related PR or issue link (optional)

What's changed and what's your intention?

reduce overheads by optimizations like reusing parsed content and buffer, removing redundant checks on the happy path, storing bucket position and building output in batch etc.

Details
histogram_fold/small/10s_10t_12b
                        time:   [144.45 µs 144.63 µs 144.80 µs]
                        change: [−15.462% −15.303% −15.152%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild
histogram_fold/medium/50s_100t_12b
                        time:   [279.76 µs 281.33 µs 283.55 µs]
                        change: [−82.834% −82.724% −82.608%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  5 (5.00%) high severe
Benchmarking histogram_fold/large/100s_500t_12b: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 8.3s, enable flat sampling, or reduce sample count to 50.
histogram_fold/large/100s_500t_12b
                        time:   [1.6163 ms 1.6231 ms 1.6312 ms]
                        change: [−88.962% −88.924% −88.886%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  2 (2.00%) high mild
  3 (3.00%) high severe
histogram_fold/many_buckets/10s_100t_50b
                        time:   [228.52 µs 228.80 µs 229.07 µs]
                        change: [−81.279% −81.245% −81.210%] (p = 0.00 < 0.05)
                        Performance has improved.
histogram_fold/quantile/q0.10
                        time:   [299.48 µs 299.95 µs 300.64 µs]
                        change: [−80.582% −80.539% −80.496%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
  1 (1.00%) high mild
  1 (1.00%) high severe
histogram_fold/quantile/q0.50
                        time:   [302.57 µs 302.84 µs 303.12 µs]
                        change: [−80.620% −80.591% −80.560%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
  4 (4.00%) high mild
histogram_fold/quantile/q0.90
                        time:   [280.91 µs 281.14 µs 281.36 µs]
                        change: [−82.073% −82.040% −82.004%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
  4 (4.00%) high mild
histogram_fold/quantile/q0.99
                        time:   [281.09 µs 281.64 µs 282.20 µs]
                        change: [−82.109% −82.066% −82.023%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild

PR Checklist

Please convert it to a draft if some of the following conditions are not met.

  • I have written the necessary rustdoc comments.
  • I have added the necessary unit tests and integration tests.
  • This PR requires documentation updates.
  • API changes are backward compatible.
  • Schema or data changes are backward compatible.

waynexia added 7 commits April 2, 2026 06:44
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
@github-actions github-actions Bot added size/M docs-not-required This change does not impact docs. labels Apr 8, 2026
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 22c656efff

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/promql/src/extension_plan/histogram_fold.rs
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces an optimistic execution path for the HistogramFold plan to improve performance, including new benchmarks and refactored logic for histogram evaluation. I have identified a critical issue in the new evaluate_row_fast_validated function where it fails to validate the minimum number of buckets, which could lead to an out-of-bounds panic. Please incorporate the suggested check for bucket.len() < 2 to ensure robustness.

Comment thread src/promql/src/extension_plan/histogram_fold.rs Outdated
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Copy link
Copy Markdown
Collaborator

@fengjiachun fengjiachun left a comment

Choose a reason for hiding this comment

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

LGTM

let remaining_input_batch = batch.slice(cursor, remaining_rows);
self.switch_to_safe_mode(remaining_input_batch)?;
return Ok(());
if !Self::is_positive_infinity(le_array, cursor + bucket_num - 1) {
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.

should the optimis path still use validate_optimistic_group to valid just in case input is malformed?

Copy link
Copy Markdown
Contributor

@evenyag evenyag left a comment

Choose a reason for hiding this comment

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

Adding some comments may help me understand the changes. 😵‍💫

Comment on lines +1115 to +1135
let expected_pos = total * quantile;
let fit_bucket_pos = counter.partition_point(|&c| c < expected_pos);
if fit_bucket_pos >= bucket.len() - 1 {
bucket[bucket.len() - 2]
} else {
let upper_bound = bucket[fit_bucket_pos];
let upper_count = counter[fit_bucket_pos];
let mut lower_bound = bucket[0].min(0.0);
let mut lower_count = 0.0;
if fit_bucket_pos > 0 {
lower_bound = bucket[fit_bucket_pos - 1];
lower_count = counter[fit_bucket_pos - 1];
}
if (upper_count - lower_count).abs() < 1e-10 {
f64::NAN
} else {
lower_bound
+ (upper_bound - lower_bound) / (upper_count - lower_count)
* (expected_pos - lower_count)
}
}
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.

Is it almost the same as the part in evaluate_row()? Can we reuse it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs-not-required This change does not impact docs. size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants