Skip to content

Commit 12b2601

Browse files
committed
Make PersistentHugr::children more efficient
1 parent 0e13fd6 commit 12b2601

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

hugr-persistent/src/trait_impls.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use hugr_core::{
1313
render::{self, MermaidFormatter, NodeLabel},
1414
},
1515
},
16-
ops::OpType,
16+
ops::{OpTag, OpTrait, OpType},
1717
};
1818

1919
use crate::CommitId;
@@ -215,14 +215,26 @@ impl HugrView for PersistentHugr {
215215
}
216216

217217
fn children(&self, node: Self::Node) -> impl DoubleEndedIterator<Item = Self::Node> + Clone {
218-
let (hugr, node_map) = self.apply_all();
219-
let children = hugr.children(node_map[&node]).collect_vec();
220-
let inv_node_map: HashMap<_, _> = node_map.into_iter().map(|(k, v)| (v, k)).collect();
221-
children.into_iter().map(move |child| {
222-
*inv_node_map
223-
.get(&child)
224-
.expect("node not found in node map")
225-
})
218+
// Only children of dataflow parents may change
219+
if OpTag::DataflowParent.is_superset(self.get_optype(node).tag()) {
220+
// TODO: make this more efficient by traversing from inputs to outputs
221+
let (hugr, node_map) = self.apply_all();
222+
let children = hugr.children(node_map[&node]).collect_vec();
223+
let inv_node_map: HashMap<_, _> = node_map.into_iter().map(|(k, v)| (v, k)).collect();
224+
Either::Right(children.into_iter().map(move |child| {
225+
*inv_node_map
226+
.get(&child)
227+
.expect("node not found in node map")
228+
}))
229+
} else {
230+
// children are children of the commit hugr
231+
let cm = self.get_commit(node.owner());
232+
Either::Left(
233+
cm.commit_hugr()
234+
.children(node.1)
235+
.map(|n| cm.to_patch_node(n)),
236+
)
237+
}
226238
}
227239

228240
fn descendants(&self, node: Self::Node) -> impl Iterator<Item = Self::Node> + Clone {

0 commit comments

Comments
 (0)