Skip to content

Commit f29979a

Browse files
Rollup merge of rust-lang#136082 - Zalathar:iter-nodes, r=oli-obk
Incorporate `iter_nodes` into `graph::DirectedGraph` This helper method iterates over all node IDs in the dense range `0..num_nodes`. In practice, we have a lot of graph-algorithm code that already assumes that nodes are densely numbered, by using `num_nodes` to allocate per-node indexed data structures. So I don't think this is actually a substantial change to the de-facto semantics of `graph::DirectedGraph`. --- Resolves a FIXME from rust-lang#135481.
2 parents 06df5cd + d36e2b8 commit f29979a

File tree

6 files changed

+18
-23
lines changed

6 files changed

+18
-23
lines changed

compiler/rustc_data_structures/src/graph/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,23 @@ mod tests;
1414
pub trait DirectedGraph {
1515
type Node: Idx;
1616

17+
/// Returns the total number of nodes in this graph.
18+
///
19+
/// Several graph algorithm implementations assume that every node ID is
20+
/// strictly less than the number of nodes, i.e. nodes are densely numbered.
21+
/// That assumption allows them to use `num_nodes` to allocate per-node
22+
/// data structures, indexed by node.
1723
fn num_nodes(&self) -> usize;
24+
25+
/// Iterates over all nodes of a graph in ascending numeric order.
26+
///
27+
/// Assumes that nodes are densely numbered, i.e. every index in
28+
/// `0..num_nodes` is a valid node.
29+
fn iter_nodes(
30+
&self,
31+
) -> impl Iterator<Item = Self::Node> + DoubleEndedIterator + ExactSizeIterator {
32+
(0..self.num_nodes()).map(<Self::Node as Idx>::new)
33+
}
1834
}
1935

2036
pub trait NumEdges: DirectedGraph {

compiler/rustc_data_structures/src/graph/scc/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,8 @@ where
333333
to_annotation,
334334
};
335335

336-
let scc_indices = (0..num_nodes)
337-
.map(G::Node::new)
336+
let scc_indices = graph
337+
.iter_nodes()
338338
.map(|node| match this.start_walk_from(node) {
339339
WalkReturn::Complete { scc_index, .. } => scc_index,
340340
WalkReturn::Cycle { min_depth, .. } => {

compiler/rustc_mir_transform/src/coverage/counters.rs

-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ use rustc_index::bit_set::DenseBitSet;
1010
use rustc_middle::mir::coverage::{CounterId, CovTerm, Expression, ExpressionId, Op};
1111

1212
use crate::coverage::counters::balanced_flow::BalancedFlowGraph;
13-
use crate::coverage::counters::iter_nodes::IterNodes;
1413
use crate::coverage::counters::node_flow::{
1514
CounterTerm, NodeCounters, make_node_counters, node_flow_data_for_balanced_graph,
1615
};
1716
use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph};
1817

1918
mod balanced_flow;
20-
mod iter_nodes;
2119
mod node_flow;
2220
mod union_find;
2321

compiler/rustc_mir_transform/src/coverage/counters/balanced_flow.rs

-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ use rustc_data_structures::graph::reversed::ReversedGraph;
2020
use rustc_index::Idx;
2121
use rustc_index::bit_set::DenseBitSet;
2222

23-
use crate::coverage::counters::iter_nodes::IterNodes;
24-
2523
/// A view of an underlying graph that has been augmented to have “balanced flow”.
2624
/// This means that the flow (execution count) of each node is equal to the
2725
/// sum of its in-edge flows, and also equal to the sum of its out-edge flows.

compiler/rustc_mir_transform/src/coverage/counters/iter_nodes.rs

-16
This file was deleted.

compiler/rustc_mir_transform/src/coverage/counters/node_flow.rs

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_index::bit_set::DenseBitSet;
1111
use rustc_index::{Idx, IndexSlice, IndexVec};
1212
use rustc_middle::mir::coverage::Op;
1313

14-
use crate::coverage::counters::iter_nodes::IterNodes;
1514
use crate::coverage::counters::union_find::UnionFind;
1615

1716
#[cfg(test)]

0 commit comments

Comments
 (0)