From 9e5595ca3e564ff7f5e776397fccc0bdddfc9d40 Mon Sep 17 00:00:00 2001 From: discord9 <55937128+discord9@users.noreply.github.com> Date: Tue, 20 Oct 2026 00:54:39 +0800 Subject: [PATCH] perf(promql): find the run a chunk would overlap without scanning history The stored runs are sorted and disjoint, so at most one of them can share a position with an incoming run. Searching the whole array made a sparse series split across chunks cost a pass over its history per chunk. Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> --- src/promql/src/native/execute.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/promql/src/native/execute.rs b/src/promql/src/native/execute.rs index 8d87732830b..a23d103402e 100644 --- a/src/promql/src/native/execute.rs +++ b/src/promql/src/native/execute.rs @@ -335,7 +335,13 @@ impl Emitted { for (index, runs) in occupied.iter().enumerate() { let stored = history.fields.get(index).map_or(&[][..], Vec::as_slice); for run in runs { - if let Some(previous) = stored.iter().find(|stored| stored.overlaps(*run)) { + // The stored runs are sorted and disjoint, so only the one holding `run.start` + // or the one just before it can share a position. Scanning them all would make + // a sparse series split across chunks cost a pass over its whole history per + // chunk. + let at = stored.partition_point(|stored| stored.end <= run.start); + let previous = stored.get(at).filter(|stored| stored.overlaps(*run)); + if let Some(previous) = previous { return Err(occupied_position(path, &labels, index, run, previous)); } }