Skip to content

Commit 21871ae

Browse files
committed
perf(analyze): fail_groups is now faster
1 parent c6450e2 commit 21871ae

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@ required-features = ["executable"]
4848

4949
[package.metadata."docs.rs"]
5050
all-features = true
51+
52+
[profile.profiling]
53+
inherits = "release"
54+
debug = true

src/analyze.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -343,24 +343,39 @@ fn group_by_time<'check>(checks: &[&'check Check]) -> HashMap<i64, CheckGroup<'c
343343

344344
fn fail_groups<'check>(checks: &[&'check Check]) -> Vec<CheckGroup<'check>> {
345345
let by_time = group_by_time(checks);
346-
let mut groups: HashSet<CheckGroup> = HashSet::new();
347-
let mut group;
348-
for key in by_time.keys() {
349-
group = Vec::new();
346+
let mut groups = Vec::new();
347+
let mut processed_times = HashSet::new();
348+
let window = DEFAULT_PERIOD * OUTAGE_TIME_SPAN;
350349

351-
for in_range_time in
352-
key - (DEFAULT_PERIOD * OUTAGE_TIME_SPAN)..key + (DEFAULT_PERIOD * OUTAGE_TIME_SPAN)
353-
{
354-
if let Some(a) = by_time.get(&in_range_time).cloned() {
355-
group.extend(a);
350+
for &time in by_time.keys() {
351+
// Skip if we've already processed this time as part of another group
352+
if processed_times.contains(&time) {
353+
continue;
354+
}
355+
356+
let mut current_group = Vec::new();
357+
358+
// Find all checks within the time window
359+
let window_start = time.saturating_sub(window);
360+
let window_end = time.saturating_add(window);
361+
362+
// Mark all times in this window as processed
363+
for t in by_time.keys() {
364+
if *t >= window_start && *t <= window_end && processed_times.insert(*t) {
365+
// Only process if not already processed
366+
if let Some(checks) = by_time.get(t) {
367+
current_group.extend(checks.iter().cloned());
368+
}
356369
}
357370
}
358371

359-
group.sort();
360-
groups.insert(group);
372+
if !current_group.is_empty() {
373+
current_group.sort();
374+
groups.push(current_group);
375+
}
361376
}
362377

363-
groups.into_iter().collect()
378+
groups
364379
}
365380

366381
/// Analyze metrics for a specific check type.

0 commit comments

Comments
 (0)