Skip to content

Commit

Permalink
FEAT: Speed up Graph, StableGraph::map
Browse files Browse the repository at this point in the history
Use Vec::extend instead of a push loop -- this improves performance.

Microbenchmark results:

```
 name              old ns/iter  new ns/iter  diff ns/iter   diff %
 graph_map         569          323                  -246  -43.23%
 stable_graph_map  594          405                  -189  -31.82%
```
  • Loading branch information
bluss committed Sep 23, 2017
1 parent 29efa15 commit 92a9d4e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
14 changes: 14 additions & 0 deletions benches/stable_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,17 @@ fn parse_graph<Ty: EdgeType>(s: &str) -> Graph<(), (), Ty>
gr
}


#[bench]
fn stable_graph_map(bench: &mut Bencher)
{
let a = parse_stable_graph::<Directed>(BIGGER);
bench.iter(|| a.map(|i, _| i, |i, _| i));
}

#[bench]
fn graph_map(bench: &mut Bencher)
{
let a = parse_graph::<Directed>(BIGGER);
bench.iter(|| a.map(|i, _| i, |i, _| i));
}
14 changes: 6 additions & 8 deletions src/graph_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1264,19 +1264,17 @@ impl<N, E, Ty, Ix> Graph<N, E, Ty, Ix>
G: FnMut(EdgeIndex<Ix>, &'a E) -> E2,
{
let mut g = Graph::with_capacity(self.node_count(), self.edge_count());
for (i, node) in enumerate(&self.nodes) {
g.nodes.push(Node {
g.nodes.extend(enumerate(&self.nodes).map(|(i, node)|
Node {
weight: node_map(NodeIndex::new(i), &node.weight),
next: node.next,
});
}
for (i, edge) in enumerate(&self.edges) {
g.edges.push(Edge {
}));
g.edges.extend(enumerate(&self.edges).map(|(i, edge)|
Edge {
weight: edge_map(EdgeIndex::new(i), &edge.weight),
next: edge.next,
node: edge.node,
});
}
}));
g
}

Expand Down

0 comments on commit 92a9d4e

Please sign in to comment.