Skip to content

Commit

Permalink
refactor(tree): Make debugging node ids easier
Browse files Browse the repository at this point in the history
I found when debugging some issues with edges,
having to track indices was making things much more difficult.
My hope is this will help with little negative impact.

We could put this behind a `#[cfg(debug_asserts)]` but I'm assuming it
doesn't make enough of a performance difference to matter.
  • Loading branch information
epage committed Feb 26, 2025
1 parent 2447fab commit 02acff6
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/cargo/ops/tree/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ use std::collections::{HashMap, HashSet};
#[derive(Debug, Copy, Clone)]
pub struct NodeId {
index: usize,
#[allow(dead_code)] // intended for `derive(Debug)`
debug: InternedString,
}

impl NodeId {
fn new(index: usize) -> Self {
Self { index }
fn new(index: usize, debug: InternedString) -> Self {
Self { index, debug }
}
}

Expand Down Expand Up @@ -63,6 +65,15 @@ pub enum Node {
},
}

impl Node {
fn name(&self) -> InternedString {
match self {
Self::Package { package_id, .. } => package_id.name(),
Self::Feature { name, .. } => *name,
}
}
}

#[derive(Debug, Copy, Hash, Eq, Clone, PartialEq)]
pub struct Edge {
kind: EdgeKind,
Expand Down Expand Up @@ -160,7 +171,7 @@ impl<'a> Graph<'a> {

/// Adds a new node to the graph, returning its new index.
fn add_node(&mut self, node: Node) -> NodeId {
let from_index = NodeId::new(self.nodes.len());
let from_index = NodeId::new(self.nodes.len(), node.name());
self.nodes.push(node);
self.edges.push(Edges::new());
self.index.insert(self.node(from_index).clone(), from_index);
Expand Down Expand Up @@ -204,7 +215,7 @@ impl<'a> Graph<'a> {
Node::Package { package_id, .. } => package_ids.contains(package_id),
_ => false,
})
.map(|(i, node)| (node, NodeId::new(i)))
.map(|(i, node)| (node, NodeId::new(i, node.name())))
.collect();
// Sort for consistent output (the same command should always return
// the same output). "unstable" since nodes should always be unique.
Expand Down Expand Up @@ -278,7 +289,7 @@ impl<'a> Graph<'a> {
for edge in node_edges.all() {
let new_edge = Edge {
kind: edge.kind(),
node: NodeId::new(from_idx),
node: NodeId::new(from_idx, self.nodes[from_idx].name()),
};
new_edges[edge.node().index].add_edge(new_edge);
}
Expand All @@ -299,7 +310,7 @@ impl<'a> Graph<'a> {
packages
.entry(package_id.name())
.or_insert_with(Vec::new)
.push((node, NodeId::new(i)));
.push((node, NodeId::new(i, node.name())));
}
}

Expand Down Expand Up @@ -671,7 +682,7 @@ fn add_internal_features(graph: &mut Graph<'_>, resolve: &Resolve) {
Node::Package { .. } => None,
Node::Feature { node_index, name } => {
let package_id = graph.package_id_for_index(*node_index);
Some((package_id, *node_index, NodeId::new(i), *name))
Some((package_id, *node_index, NodeId::new(i, *name), *name))
}
})
.collect();
Expand Down

0 comments on commit 02acff6

Please sign in to comment.