Skip to content

Commit 571ace5

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

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

extension/src/state_aggregate.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -757,14 +757,16 @@ 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 {
764-
let amount = tis_end_time - tis_start_time;
765-
assert!(amount >= 0, "incorrectly ordered times");
766-
total += amount;
767-
}
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 && tis.state.materialize(agg.states_as_str()) == state {
767+
let amount = tis_end_time - tis_start_time;
768+
assert!(amount >= 0, "incorrectly ordered times");
769+
total += amount;
768770
}
769771
}
770772
total

0 commit comments

Comments
 (0)