Skip to content

Commit d0cd0c3

Browse files
committed
Optimize duration calculation loop
1 parent 2083c87 commit d0cd0c3

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

extension/src/state_aggregate.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -757,10 +757,14 @@ fn duration_in_inner<'a>(
757757
let state = state.materialize(agg.states_as_str());
758758
let mut total = 0;
759759
for tis in agg.combined_durations.iter() {
760-
if tis.state.materialize(agg.states_as_str()) == state {
761-
let tis_start_time = i64::max(tis.start_time, start);
762-
let tis_end_time = i64::min(tis.end_time, end);
763-
if tis_end_time >= start && tis_start_time <= end {
760+
let tis_start_time = i64::max(tis.start_time, start);
761+
let tis_end_time = i64::min(tis.end_time, end);
762+
if tis_start_time > end {
763+
// combined_durations is sorted, so after this point there can't be any more
764+
break;
765+
};
766+
if tis_end_time >= start {
767+
if tis.state.materialize(agg.states_as_str()) == state {
764768
let amount = tis_end_time - tis_start_time;
765769
assert!(amount >= 0, "incorrectly ordered times");
766770
total += amount;

0 commit comments

Comments
 (0)