perf(core): session ancestry is the branch prefix; fork_at bound 2020 -> 2002 allocations
5af6f910095bcb037c7ef3a22ae6738c762ec102
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
- Parent 1: bca13c20b369
There are many coding harnesses - but this one is fast
5af6f910095bcb037c7ef3a22ae6738c762ec102
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
+19 / −23 text lines
+16 / −21 text lines
fn ancestry(&self, at: EntryId) -> Option<Vec<Entry>> { let known = self.entries_by_id(); let mut chain = Vec::new(); let mut cursor = Some(at); while let Some(id) = cursor { let entry = known.get(&id)?; chain.push((*entry).clone()); cursor = entry.parent; } chain.reverse(); Some(chain) /// /// A branch stores its root-to-leaf chain in order, so the chain is the /// branch prefix ending at `at`; each parent link is verified against the /// preceding entry and no lookup table is built. fn ancestry(&self, at: EntryId) -> Option<&[Entry]> { let (branch, idx) = self.branches.iter().find_map(|branch| { let idx = branch.entries.iter().position(|entry| entry.id == at)?; Some((branch, idx)) })?; let chain = &branch.entries[..=idx]; let linked = chain .iter() .enumerate() .all(|(i, entry)| entry.parent == i.checked_sub(1).map(|p| chain[p].id)); linked.then_some(chain)+3 / −2 text lines