Build the used mask ids once in next_mask_id's fallback

Scanning every mask for every candidate id made a dense run of ids
quadratic. Collect them into a set first.
This commit is contained in:
Bruno-Alumn
2026-10-30 15:21:57 +01:00
parent bc12ec8e40
commit 5bcdc439f6
+7 -2
View File
@@ -164,11 +164,16 @@ impl DevelopSettings {
/// Next free mask id: one past the highest, or, when that is `u32::MAX` (ids come from files,
/// pastes and agents), the lowest one no mask uses.
pub fn next_mask_id(&self) -> u32 {
let used = |id: u32| self.masks.iter().any(|m| m.id == id);
match self.masks.iter().map(|m| m.id).max() {
None => 1,
// fewer masks than ids: one of the first `masks.len() + 1` is free
Some(max) => max.checked_add(1).or_else(|| (1..=u32::MAX).find(|&id| !used(id))).unwrap_or(0),
Some(max) => max
.checked_add(1)
.or_else(|| {
let used: std::collections::HashSet<u32> = self.masks.iter().map(|m| m.id).collect();
(1..=u32::MAX).find(|id| !used.contains(id))
})
.unwrap_or(0),
}
}
}