mirror of
https://github.com/storytold/lightcraft.git
synced 2026-11-01 05:57:09 -06:00
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:
@@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user