From 86e56d20530658706eae8d1415707ed181c6bc52 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Wed, 28 Dec 2022 18:38:01 +0100 Subject: [PATCH] cob: Move graph sorting to `evaluate` function This avoids passing the sorted items as an iterator. Signed-off-by: Alexis Sellier --- radicle-cob/src/change_graph.rs | 15 +------------ radicle-cob/src/change_graph/evaluation.rs | 25 +++++++++++----------- radicle-cob/src/history.rs | 12 +++++------ 3 files changed, 20 insertions(+), 32 deletions(-) diff --git a/radicle-cob/src/change_graph.rs b/radicle-cob/src/change_graph.rs index 2b883787..23833bb3 100644 --- a/radicle-cob/src/change_graph.rs +++ b/radicle-cob/src/change_graph.rs @@ -93,20 +93,7 @@ impl ChangeGraph { let (root, root_node) = roots.first().unwrap(); let manifest = root_node.manifest.clone(); let rng = fastrand::Rng::new(); - let items = self.graph.sorted(rng).into_iter().map(|oid| { - let node = &self.graph[&oid]; - let child_commits = node - .dependents - .iter() - .map(|e| *self.graph[e].id()) - .collect::>(); - - (&node.value, oid, child_commits) - }); - let history = { - let root_change = &self.graph[*root]; - evaluate(*root_change.id(), &self.graph, items) - }; + let history = evaluate(*self.graph[*root].id(), &self.graph, rng); CollaborativeObject { manifest, diff --git a/radicle-cob/src/change_graph/evaluation.rs b/radicle-cob/src/change_graph/evaluation.rs index 9de460c2..4b9ed7c8 100644 --- a/radicle-cob/src/change_graph/evaluation.rs +++ b/radicle-cob/src/change_graph/evaluation.rs @@ -15,17 +15,18 @@ use crate::{change::Change, history, pruning_fold}; /// # Panics /// /// If the change corresponding to the root OID is not in `items` -pub fn evaluate<'b>( - root: Oid, - graph: &Dag, - items: impl Iterator)>, -) -> history::History { +pub fn evaluate(root: Oid, graph: &Dag, rng: fastrand::Rng) -> history::History { let entries = pruning_fold::pruning_fold( HashMap::::new(), - items.map(|(change, idx, children)| ChangeWithChildren { - idx, - change, - child_commits: children, + graph.sorted(rng).into_iter().map(|oid| { + let node = &graph[&oid]; + let child_commits = node.dependents.iter().copied().collect(); + + ChangeWithChildren { + oid, + change: &node.value, + child_commits, + } }), |mut entries, c| match evaluate_change(c.change, &c.child_commits) { Err(RejectionReason::InvalidSignatures) => { @@ -37,11 +38,11 @@ pub fn evaluate<'b>( } Ok(entry) => { // Get parent commits and calculate this node's clock based on theirs. - let clock = graph[&c.idx] + let clock = graph[&c.oid] .dependencies .iter() .map(|e| { - let entry = &entries[&graph[e].id.into()]; + let entry = &entries[&EntryId::from(*e)]; let clock = entry.clock(); clock + entry.contents().len() as Clock - 1 @@ -81,7 +82,7 @@ fn evaluate_change( } struct ChangeWithChildren<'a> { - idx: Oid, + oid: Oid, change: &'a Change, child_commits: Vec, } diff --git a/radicle-cob/src/history.rs b/radicle-cob/src/history.rs index adf66ac0..35252fa1 100644 --- a/radicle-cob/src/history.rs +++ b/radicle-cob/src/history.rs @@ -105,12 +105,12 @@ impl History { where F: for<'r> FnMut(A, &'r EntryWithClock) -> ControlFlow, { - let sorted = self.graph.sorted(fastrand::Rng::new()); - #[allow(clippy::let_and_return)] - let items = sorted.iter().map(|idx| { - let entry = &self.graph[idx]; - entry - }); + let items = self + .graph + .sorted(fastrand::Rng::new()) + .into_iter() + .map(|idx| &self.graph[&idx]); + pruning_fold::pruning_fold(init, items, f) }