perf(promql): keep the grid position in i64 instead of dividing in i128 (#10175)

`grid_position` runs once per reported point and computed the position with
`i128` arithmetic: `div_euclid` and `rem_euclid` on the same operands, which
become two `__divti3` calls for a timestamp and a start that are both `i64`.

Stay in `i64` when the subtraction does not overflow and the offset is not
negative: `div` and `rem` on the same operands become one machine divide.
Off-lattice timestamps, positions past the end, negative offsets and
overflowing subtractions fall through to the `i128` path, which answers the
same as before, error message included.

The fast path is a strict subset of what the `i128` code accepted, so the
answer cannot change; a wrapped subtraction is the one way it could, and a
test pins that an overflowing offset still goes to the wide path. Another
test pins that a lattice whose offsets do not fit in an `i64` at all still
answers, so the fast path cannot quietly narrow what the function accepts,
and a third pins the lattice itself.

Measured on a step=1s range query over 28.8M grid points on the native PromQL
path, retired instructions per query, alternating the two binaries:

  session A (3 rounds)   before 51,233.33 / 50,677.21 / 50,795.70 M
                         after  47,174.07 / 46,378.66 / 46,473.54 M
  session B (2 rounds)   before 51,343.56 / 51,232.13 M
                         after  45,803.16 / 45,942.42 M

That is 8.5% to 10.6% fewer instructions, with every paired round moving in
the same direction and no overlap between the two ranges. The two sessions do
not share a baseline to better than about 1%, so the range is reported rather
than one figure.

The `perf` samples put `grid_position` at 3.99% of the profile and the divide
at 3.6%, which understates this: `__divti3` is dozens of instructions per
call, so a sampling share reads low where an instruction count does not.

`cargo nextest run -p promql` passes 1123/1123.

Signed-off-by: discord9 <discord9@163.com>
This commit is contained in:
discord9
2026-11-06 14:52:41 +08:00
committed by GitHub
parent bf54d15768
commit 595abae174
2 changed files with 61 additions and 1 deletions
+13
View File
@@ -2346,6 +2346,19 @@ fn grid_end_ms(spec: GridSpec) -> i64 {
/// skip, so it is reported instead of dropped.
fn grid_position(grid: &EvalGrid, timestamp: i64) -> Result<usize> {
let spec = grid.spec();
// This runs once per reported point, so the common case stays in `i64`: `div` and `rem` on the
// same operands become one machine divide, where the `i128` path below divides twice. A grid
// whose numbers do not fit, or a timestamp off the lattice, falls through to that path, which
// computes the same answer.
if spec.step_ms > 0
&& let Some(offset) = timestamp.checked_sub(spec.start_ms)
&& offset >= 0
{
let (position, remainder) = (offset / spec.step_ms, offset % spec.step_ms);
if remainder == 0 && (position as u64) < spec.len {
return Ok(position as usize);
}
}
let offset = i128::from(timestamp) - i128::from(spec.start_ms);
let step = i128::from(spec.step_ms);
let position = offset.div_euclid(step);
+48 -1
View File
@@ -43,7 +43,8 @@ fn is_resources_exhausted(error: &DataFusionError) -> bool {
use super::{
BinaryRuntime, Emitted, InputId, InputSlot, NativeChunk, NativeExecutionState, NativeExecutor,
NodeState, PairKernel, RuntimeNode, RuntimeOp, absorb_rebuilds, evaluated_bytes, grid_end_ms,
map_chunk, map_workspace_bytes, operand_chunk_bytes, raw_bytes, vector_workspace_bytes,
grid_position, map_chunk, map_workspace_bytes, operand_chunk_bytes, raw_bytes,
vector_workspace_bytes,
};
use crate::evaluated::EvaluatedChunk;
use crate::grid_series::merge_same_labelset;
@@ -11867,3 +11868,49 @@ fn exporting_a_partial_charges_the_fold_and_the_groups_it_builds() {
charges[0]
);
}
#[test]
fn grid_position_reads_the_lattice_and_reports_what_is_off_it() {
let grid = EvalGrid::try_new(GridSpec::try_new(1_000, 30_000, 3).unwrap()).unwrap();
// Points of the lattice answer their own index, whether the position is the first, the last,
// or between them.
assert_eq!(0, grid_position(&grid, 1_000).unwrap());
assert_eq!(1, grid_position(&grid, 31_000).unwrap());
assert_eq!(2, grid_position(&grid, 61_000).unwrap());
// A timestamp off the lattice, one before the grid, and one past its end are disagreements
// between the selection and its binding, so each is reported rather than rounded into a point.
for off in [1_001, 8_999, 0, 91_000, 91_001] {
assert!(
grid_position(&grid, off).is_err(),
"{off} is not a point of the grid"
);
}
}
#[test]
fn grid_position_still_answers_a_lattice_whose_offset_does_not_fit_in_an_i64() {
// The points are `i64::MIN`, `-1` and `i64::MAX - 1`: the last one is in range, but it sits
// `2 * i64::MAX` past the start, so only the `i128` path can place it. Answering an error here
// would make the fast path narrower than the code it replaces.
let grid = EvalGrid::try_new(GridSpec::try_new(i64::MIN, i64::MAX, 3).unwrap()).unwrap();
assert_eq!(0, grid_position(&grid, i64::MIN).unwrap());
// The offset is `i64::MAX`, the widest the fast path accepts.
assert_eq!(1, grid_position(&grid, -1).unwrap());
assert_eq!(2, grid_position(&grid, i64::MAX - 1).unwrap());
// The third point of this grid is `0`, and its offset is `i64::MAX + 1`, one past what the
// fast path accepts.
let grid = EvalGrid::try_new(GridSpec::try_new(i64::MIN, 1_i64 << 62, 3).unwrap()).unwrap();
assert_eq!(2, grid_position(&grid, 0).unwrap());
}
#[test]
fn grid_position_reports_a_timestamp_whose_offset_overflows_and_misses_the_lattice() {
// The difference does not fit in an `i64`, so the `i128` path answers. Wrapped, it would be
// negative, which is not a position either: the answer is the lattice's own rejection, not a
// panic and not some row read through a wrapped offset.
let grid = EvalGrid::try_new(GridSpec::try_new(i64::MIN + 5, 1, 10).unwrap()).unwrap();
assert_eq!(0, grid_position(&grid, i64::MIN + 5).unwrap());
let error = grid_position(&grid, i64::MAX).unwrap_err().to_string();
assert!(error.contains("is not a point of"), "{error}");
}