Skip to content

Commit 0dbb7e5

Browse files
committed
bug fix: Dfs -> Bfs
1 parent 9cff499 commit 0dbb7e5

File tree

2 files changed

+83
-51
lines changed

2 files changed

+83
-51
lines changed

src/dfx/src/lib/graph/traverse_filtered.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,51 @@
1-
// TODO: Somebody, adopt this code to `pethgraph`.
2-
use petgraph::{data::DataMap, visit::{Dfs, IntoNeighbors, VisitMap}};
1+
// TODO: Somebody, adopt this code to `petgraph`.
2+
use petgraph::{data::DataMap, visit::{Bfs, Dfs, IntoNeighbors, VisitMap}};
33

4-
pub struct DfsFiltered<NodeId, VM>
5-
// where P: FnMut(&N) -> bool
6-
{
4+
#[allow(unused)]
5+
pub struct DfsFiltered<NodeId, VM> {
76
base: Dfs<NodeId, VM>,
87
// node_filter: P,
98
}
109

1110
impl<NodeId, VM> DfsFiltered<NodeId, VM> {
11+
#[allow(unused)]
1212
pub fn new(base: Dfs<NodeId, VM>) -> Self {
1313
Self {
1414
base
1515
}
1616
}
1717

18+
#[allow(unused)]
19+
pub fn traverse<G, P, C, NodeWeight>(&mut self, graph: G, mut predicate: P, mut call: C)
20+
where C: FnMut(&NodeId, &NodeId) -> (),
21+
G: IntoNeighbors<NodeId = NodeId> + DataMap<NodeWeight = NodeWeight>,
22+
P: FnMut(&NodeId) -> bool,
23+
NodeId: Copy + Eq,
24+
VM: VisitMap<NodeId>,
25+
{
26+
while let Some(source_item_id) = &self.base.next(graph) {
27+
if (&mut predicate)(source_item_id) {
28+
let source_parent_id = self.base.stack.iter().map(|e| *e).rev().find(&mut predicate);
29+
if let Some(source_parent_id) = &source_parent_id {
30+
(&mut call)(source_parent_id, &source_item_id);
31+
}
32+
}
33+
}
34+
}
35+
}
36+
37+
pub struct BfsFiltered<NodeId, VM> {
38+
base: Bfs<NodeId, VM>,
39+
// node_filter: P,
40+
}
41+
42+
impl<NodeId, VM> BfsFiltered<NodeId, VM> {
43+
pub fn new(base: Bfs<NodeId, VM>) -> Self {
44+
Self {
45+
base
46+
}
47+
}
48+
1849
pub fn traverse<G, P, C, NodeWeight>(&mut self, graph: G, mut predicate: P, mut call: C)
1950
where C: FnMut(&NodeId, &NodeId) -> (),
2051
G: IntoNeighbors<NodeId = NodeId> + DataMap<NodeWeight = NodeWeight>,

src/dfx/src/lib/models/canister.rs

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use crate::lib::builders::{
55
use crate::lib::canister_info::CanisterInfo;
66
use crate::lib::environment::Environment;
77
use crate::lib::error::{BuildError, DfxError, DfxResult};
8+
use crate::lib::graph::traverse_filtered::BfsFiltered;
89
use crate::lib::metadata::dfx::DfxMetadata;
910
use crate::lib::metadata::names::{CANDID_ARGS, CANDID_SERVICE, DFX};
10-
use crate::lib::graph::traverse_filtered::DfsFiltered;
1111
use crate::lib::wasm::file::{compress_bytes, read_wasm_module};
1212
use crate::util::assets;
1313
use anyhow::{anyhow, bail, Context};
@@ -22,7 +22,7 @@ use ic_wasm::metadata::{add_metadata, remove_metadata, Kind};
2222
use ic_wasm::optimize::OptLevel;
2323
use itertools::Itertools;
2424
use petgraph::graph::{DiGraph, NodeIndex};
25-
use petgraph::visit::Dfs;
25+
use petgraph::visit::Bfs;
2626
use rand::{thread_rng, RngCore};
2727
use slog::{error, info, trace, warn, Logger};
2828
use std::cell::RefCell;
@@ -583,55 +583,56 @@ impl CanisterPool {
583583
let source_ids = &self.imports.borrow().nodes;
584584
let start: Vec<_> =
585585
real_canisters_to_build.iter().map(|name| MotokoImport::Canister(name.clone())).collect(); // `clone` is inefficient.
586-
let start = start.into_iter().map(|node| *source_ids.get(&node).unwrap()).collect();
586+
let start = start.into_iter().map(|node| *source_ids.get(&node).unwrap());
587587
// // Transform the graph of file dependencies to graph of canister dependencies.
588588
// // For this do DFS for each of `real_canisters_to_build`.
589589
let mut dest_graph: DiGraph<CanisterId, ()> = DiGraph::new();
590590
let mut dest_id_set = HashMap::new();
591-
let dfs = Dfs::from_parts(start, HashSet::new()); // TODO: Use `FixedBitSet` instead of `HashMap`?
592-
let mut filtered_dfs = DfsFiltered::new(dfs);
593-
// let dest_id_set = &mut dest_id_set;
594-
let mut nodes_map = HashMap::new(); // from source graph to dest graph
595-
filtered_dfs.traverse(
596-
source_graph,
597-
|&s| {
598-
let source_id = source_graph.node_weight(s);
599-
if let Some(MotokoImport::Canister(_parent_name)) = source_id {
600-
true
601-
} else {
602-
false
603-
}
604-
},
605-
|&source_parent_id, &source_child_id| {
606-
// FIXME: Is the chain of `unwrap`s and `panic`s correct?
607-
let parent = source_graph.node_weight(source_parent_id).unwrap();
608-
let parent_name = match parent {
609-
MotokoImport::Canister(name) => name,
610-
_ => {
611-
panic!("programming error");
612-
}
613-
};
614-
let parent_canister = self.get_first_canister_with_name(&parent_name).unwrap().canister_id();
615-
616-
let child = source_graph.node_weight(source_child_id).unwrap();
617-
let child_name = match child {
618-
MotokoImport::Canister(name) => name,
619-
_ => {
620-
panic!("programming error");
591+
for start_node in start {
592+
let bfs = Bfs::new(&source_graph, start_node);
593+
let mut filtered_bfs = BfsFiltered::new(bfs);
594+
let mut nodes_map = HashMap::new(); // from source graph to dest graph
595+
filtered_bfs.traverse(
596+
source_graph,
597+
|&s| {
598+
let source_id = source_graph.node_weight(s);
599+
if let Some(MotokoImport::Canister(_parent_name)) = source_id {
600+
true
601+
} else {
602+
false
621603
}
622-
};
623-
let child_canister = self.get_first_canister_with_name(&child_name).unwrap().canister_id();
624-
625-
let dest_parent_id = *dest_id_set.entry(source_parent_id).or_insert_with(|| dest_graph.add_node(parent_canister));
626-
nodes_map.insert(source_parent_id, dest_parent_id);
627-
let dest_child_id = *dest_id_set.entry(source_child_id).or_insert_with(|| dest_graph.add_node(child_canister));
628-
nodes_map.insert(source_child_id, dest_child_id);
629-
nodes_map.entry(source_parent_id).or_insert_with(
630-
|| dest_graph.add_node(*dest_graph.node_weight(source_parent_id).unwrap()) // FIXME: `unwrap()`?
631-
);
632-
dest_graph.add_edge(dest_parent_id, dest_child_id, ());
633-
}
634-
);
604+
},
605+
|&source_parent_id, &source_child_id| {
606+
// FIXME: Is the chain of `unwrap`s and `panic`s correct?
607+
let parent = source_graph.node_weight(source_parent_id).unwrap();
608+
let parent_name = match parent {
609+
MotokoImport::Canister(name) => name,
610+
_ => {
611+
panic!("programming error");
612+
}
613+
};
614+
let parent_canister = self.get_first_canister_with_name(&parent_name).unwrap().canister_id();
615+
616+
let child = source_graph.node_weight(source_child_id).unwrap();
617+
let child_name = match child {
618+
MotokoImport::Canister(name) => name,
619+
_ => {
620+
panic!("programming error");
621+
}
622+
};
623+
let child_canister = self.get_first_canister_with_name(&child_name).unwrap().canister_id();
624+
625+
let dest_parent_id = *dest_id_set.entry(source_parent_id).or_insert_with(|| dest_graph.add_node(parent_canister));
626+
nodes_map.insert(source_parent_id, dest_parent_id);
627+
let dest_child_id = *dest_id_set.entry(source_child_id).or_insert_with(|| dest_graph.add_node(child_canister));
628+
nodes_map.insert(source_child_id, dest_child_id);
629+
nodes_map.entry(source_parent_id).or_insert_with(
630+
|| dest_graph.add_node(*dest_graph.node_weight(source_parent_id).unwrap()) // FIXME: `unwrap()`?
631+
);
632+
dest_graph.add_edge(dest_parent_id, dest_child_id, ());
633+
}
634+
);
635+
}
635636
// let source_graph = &self.imports.borrow().graph;
636637
// let mut dest_graph: DiGraph<CanisterId, ()> = DiGraph::new();
637638
// let mut dest_id_set = HashMap::new();

0 commit comments

Comments
 (0)