From f93116a7f7b2b027194a02469ee4121ec8ff7a13 Mon Sep 17 00:00:00 2001 From: discord9 <55937128+discord9@users.noreply.github.com> Date: Mon, 19 Oct 2026 14:52:24 +0800 Subject: [PATCH] refactor(promql): drop per-field clearing the evaluator never needs Two review findings, both about code that does nothing. `finish_series` cleared every field after `evaluate(None)` returned. That loop is redundant: `evaluate` ends with either every point evaluated -- so `head` moved to the end and `advance` dropped the samples -- or no field holding a sample, and either way the fields are empty. It also did not clear `histograms`, which made it read as though it should. Removing it keeps the suite green, and `push` already documents that an evaluator is not reused after an error. `FieldSamples::clear` existed only for that loop. `overlapping_window_spans_chunks` used a `sum` kernel and never touched resets. Windows spanning chunks are already covered by `evaluator_matches_brute_force_reference`, which chunks randomly, and no mutation was caught only by this test. Its `sum` helper went with it. Signed-off-by: discord9 Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> --- src/promql/src/range_eval.rs | 17 ++++------------- src/promql/src/range_eval/tests.rs | 24 ------------------------ 2 files changed, 4 insertions(+), 37 deletions(-) diff --git a/src/promql/src/range_eval.rs b/src/promql/src/range_eval.rs index c0a2066adc6..b809fe2284d 100644 --- a/src/promql/src/range_eval.rs +++ b/src/promql/src/range_eval.rs @@ -124,8 +124,10 @@ struct FieldSamples { /// Native histogram samples, kept apart from the floats as Prometheus does. histograms: Vec<(i64, FloatHistogram)>, histogram_head: usize, - /// Sequence number of `values[0]`. Absolute and monotonic, so a reset keeps its position - /// when `head` samples are dropped instead of every index being renumbered. + /// Sequence number of `values[0]`. Absolute and monotonic across the series this evaluator + /// evaluates, so a reset keeps its position when `head` samples are dropped instead of every + /// index being renumbered. An empty field holds the number of samples already dropped, which + /// keeps positions increasing when the next series starts. base: u64, /// Resets between consecutive retained samples, ascending by position: exactly the ones a /// window over these samples can still contain. Maintained only for the counter-correcting @@ -204,14 +206,6 @@ impl FieldSamples { let count = self.resets[first..].partition_point(|reset| reset.position < end); &self.resets[first..first + count] } - - fn clear(&mut self) { - self.timestamps.clear(); - self.values.clear(); - self.head = 0; - self.base = 0; - self.resets.clear(); - } } impl RangeEvaluator { @@ -373,9 +367,6 @@ impl RangeEvaluator { fn finish_series(&mut self) -> Result> { self.evaluate(None)?; let mut state = self.series.take().expect("set by the caller"); - for field in &mut self.fields { - field.clear(); - } if self.drops_metric_name { state.metric.remove_all(&METADATA_LABELS); } diff --git a/src/promql/src/range_eval/tests.rs b/src/promql/src/range_eval/tests.rs index a6d89b33c8f..66f38734f98 100644 --- a/src/promql/src/range_eval/tests.rs +++ b/src/promql/src/range_eval/tests.rs @@ -89,10 +89,6 @@ fn scanned_resets(values: &[f64]) -> Vec { .collect() } -fn sum(window: FloatWindow<'_>) -> Option { - Some(window.values.iter().sum()) -} - fn raw_schema(fields: usize) -> SeriesSchema { SeriesSchema { fields: (0..fields) @@ -510,26 +506,6 @@ fn functions_match_range_udfs() { } } -#[test] -fn overlapping_window_spans_chunks() { - let window = RangeWindow { - range_ms: 15, - offset_ms: 0, - anchor_ms: None, - }; - let kernel: WindowKernel = Box::new(sum); - let mut evaluator = - RangeEvaluator::with_kernel(&raw_schema(1), grid(10, 10, 3), window, kernel, true, true) - .unwrap(); - let first = chunk(&[(5, vec![Some(1.0)]), (12, vec![Some(2.0)])], 1, 0, false); - let second = chunk(&[(18, vec![Some(4.0)]), (25, vec![Some(8.0)])], 1, 0, true); - assert!(evaluator.push(&first).unwrap().is_none()); - let series = evaluator.push(&second).unwrap(); - // Windows (-5, 10], (5, 20] and (15, 30]. - let expected = vec![vec![Some(1.0)], vec![Some(6.0)], vec![Some(12.0)]]; - assert_series_eq(series.as_ref(), &expected, &metric(0)); -} - #[test] fn empty_input_and_series_without_results() { let window = RangeWindow {