Skip to content

Commit 86139ea

Browse files
authoredMar 24, 2022
Memory optimization of pruning pass (#1001)
* first iteration * fmt
1 parent 960030b commit 86139ea

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed
 

‎moose/src/compilation/pruning.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use crate::computation::{Computation, Operator};
2+
use bitvec::prelude::*;
23
use petgraph::visit::{depth_first_search, DfsEvent};
34

45
/// Prunes the computation from anything not relevant for the output
5-
pub fn prune_graph(comp: Computation) -> anyhow::Result<Computation> {
6+
pub fn prune_graph(mut comp: Computation) -> anyhow::Result<Computation> {
67
// Need to reverse the graph, because we will be traversing it from the outputs
78
let mut graph = comp.as_graph();
89
graph.reverse();
910
// Operations to keep
10-
let mut keep = Vec::with_capacity(comp.operations.len());
11+
let mut keep: BitVec<u8, Lsb0> = BitVec::repeat(false, comp.operations.len());
1112
// Identify all the output nodes
1213
let outputs = graph
1314
.node_indices()
@@ -16,12 +17,15 @@ pub fn prune_graph(comp: Computation) -> anyhow::Result<Computation> {
1617
// Perform a DFS
1718
depth_first_search(&graph, outputs, |event| {
1819
if let DfsEvent::Discover(visited, _) = event {
19-
keep.push(comp.operations[graph[visited].index].clone());
20+
keep.set(graph[visited].index, true);
2021
};
2122
});
2223

23-
// Construct a new computation. NB: we did not toposort it.
24-
Ok(Computation { operations: keep })
24+
let mut iter = keep.iter();
25+
// only keep the operations that were visited by DFS
26+
comp.operations.retain(|_| *iter.next().unwrap());
27+
28+
Ok(comp)
2529
}
2630

2731
#[cfg(test)]

0 commit comments

Comments
 (0)
Please sign in to comment.