repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by bugabinga

perf(core): session ancestry is the branch prefix; fork_at bound 2020 -> 2002 allocations

5af6f910095bcb037c7ef3a22ae6738c762ec102

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

+19 / −23 text lines

Diff layout

smith-core/src/session.rs

Modified

+16 / −21 text lines

269269 entry
270270 }
272 /// Every entry of this session, regardless of branch.
273 fn entries_by_id(&self) -> HashMap<EntryId, &Entry> {
274 self.branches
275 .iter()
276 .flat_map(|branch| branch.entries.iter())
277 .map(|entry| (entry.id, entry))
278 .collect()
279 }
281272 /// The root-to-`at` entry chain, or `None` when `at` is unknown or its
282273 /// ancestry is broken.
283 fn ancestry(&self, at: EntryId) -> Option<Vec<Entry>> {
284 let known = self.entries_by_id();
285 let mut chain = Vec::new();
286 let mut cursor = Some(at);
287 while let Some(id) = cursor {
288 let entry = known.get(&id)?;
289 chain.push((*entry).clone());
290 cursor = entry.parent;
291 }
292 chain.reverse();
293 Some(chain)
274 ///
275 /// A branch stores its root-to-leaf chain in order, so the chain is the
276 /// branch prefix ending at `at`; each parent link is verified against the
277 /// preceding entry and no lookup table is built.
278 fn ancestry(&self, at: EntryId) -> Option<&[Entry]> {
279 let (branch, idx) = self.branches.iter().find_map(|branch| {
280 let idx = branch.entries.iter().position(|entry| entry.id == at)?;
281 Some((branch, idx))
282 })?;
283 let chain = &branch.entries[..=idx];
284 let linked = chain
285 .iter()
286 .enumerate()
287 .all(|(i, entry)| entry.parent == i.checked_sub(1).map(|p| chain[p].id));
288 linked.then_some(chain)
294289 }
296291 /// Fork the history ending at `at` into a new session.
316311 id: SessionId::new(),
317312 branches: vec![Branch {
318313 branch_id: BranchId::new(),
319 entries,
314 entries: entries.to_vec(),
320315 selected_idx,
321316 }],
322317 active_branch_idx: 0,

smith-core/tests/alloc_bounds.rs

Modified

+3 / −2 text lines

88#[global_allocator]
99static ALLOC: Counting = Counting;
11/// `fork_at` over 1000 entries; clones every entry on the chain today.
12const FORK_AT_1000_ALLOCATIONS: u64 = 2020;
11/// `fork_at` over 1000 entries: the branch list, the chain `Vec`, and two allocations per cloned
12/// text entry (`blocks` and its `String`) until payloads become bytes at rest.
13const FORK_AT_1000_ALLOCATIONS: u64 = 2002;
1415fn linear_session(len: usize) -> Session {
1516 let mut session = Session::new();