Skip to content

Commit 2b3ee67

Browse files
authored
Clean up NodeId kind fetching (#20196)
# Objective - Part of #20115 We don't actually need the `ScheduleGraph` in order to get the kind of a node, so lets move it to our newly created trait. ## Solution - Removed `ScheduleGraph::get_node_kind`, added `GraphNodeId::kind` instead. - Co-located `GraphNodeId` in the same file as `DiGraph`/`UnGraph`/`Graph`. - Implemented `GraphNodeId` for `SystemSetKey` for symmetry. No migration guide needed as `get_node_kind` is a private function. ## Testing Re-using current tests.
1 parent 9d4bfdb commit 2b3ee67

File tree

6 files changed

+52
-43
lines changed

6 files changed

+52
-43
lines changed

crates/bevy_ecs/src/schedule/graph/graph_map.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,36 @@
55
//! [`petgraph`]: https://docs.rs/petgraph/0.6.5/petgraph/
66
77
use alloc::vec::Vec;
8-
use bevy_platform::{collections::HashSet, hash::FixedHasher};
98
use core::{
10-
fmt,
9+
fmt::{self, Debug},
1110
hash::{BuildHasher, Hash},
1211
};
12+
13+
use bevy_platform::{collections::HashSet, hash::FixedHasher};
1314
use indexmap::IndexMap;
1415
use smallvec::SmallVec;
1516

16-
use crate::schedule::graph::node::GraphNodeId;
17-
1817
use Direction::{Incoming, Outgoing};
1918

19+
/// Types that can be used as node identifiers in a [`DiGraph`]/[`UnGraph`].
20+
///
21+
/// [`DiGraph`]: crate::schedule::graph::DiGraph
22+
/// [`UnGraph`]: crate::schedule::graph::UnGraph
23+
pub trait GraphNodeId: Copy + Eq + Hash + Ord + Debug {
24+
/// The type that packs and unpacks this [`GraphNodeId`] with a [`Direction`].
25+
/// This is used to save space in the graph's adjacency list.
26+
type Adjacent: Copy + Debug + From<(Self, Direction)> + Into<(Self, Direction)>;
27+
/// The type that packs and unpacks this [`GraphNodeId`] with another
28+
/// [`GraphNodeId`]. This is used to save space in the graph's edge list.
29+
type Edge: Copy + Eq + Hash + Debug + From<(Self, Self)> + Into<(Self, Self)>;
30+
31+
/// Name of the kind of this node id.
32+
///
33+
/// For structs, this should return a human-readable name of the struct.
34+
/// For enums, this should return a human-readable name of the enum variant.
35+
fn kind(&self) -> &'static str;
36+
}
37+
2038
/// A `Graph` with undirected edges of some [`GraphNodeId`] `N`.
2139
///
2240
/// For example, an edge between *1* and *2* is equivalent to an edge between
@@ -55,7 +73,7 @@ where
5573
edges: HashSet<N::Edge, S>,
5674
}
5775

58-
impl<const DIRECTED: bool, N: GraphNodeId, S: BuildHasher> fmt::Debug for Graph<DIRECTED, N, S> {
76+
impl<const DIRECTED: bool, N: GraphNodeId, S: BuildHasher> Debug for Graph<DIRECTED, N, S> {
5977
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6078
self.nodes.fmt(f)
6179
}

crates/bevy_ecs/src/schedule/graph/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ use fixedbitset::FixedBitSet;
1313
use crate::schedule::set::*;
1414

1515
mod graph_map;
16-
mod node;
1716
mod tarjan_scc;
1817

19-
pub use graph_map::{DiGraph, Direction, UnGraph};
20-
pub use node::GraphNodeId;
18+
pub use graph_map::{DiGraph, Direction, GraphNodeId, UnGraph};
2119

2220
/// Specifies what kind of edge should be added to the dependency graph.
2321
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash)]

crates/bevy_ecs/src/schedule/graph/node.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

crates/bevy_ecs/src/schedule/graph/tarjan_scc.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use crate::schedule::graph::node::GraphNodeId;
2-
3-
use super::DiGraph;
41
use alloc::vec::Vec;
5-
use core::hash::BuildHasher;
6-
use core::num::NonZeroUsize;
2+
use core::{hash::BuildHasher, num::NonZeroUsize};
3+
74
use smallvec::SmallVec;
85

6+
use crate::schedule::graph::{DiGraph, GraphNodeId};
7+
98
/// Create an iterator over *strongly connected components* using Algorithm 3 in
109
/// [A Space-Efficient Algorithm for Finding Strongly Connected Components][1] by David J. Pierce,
1110
/// which is a memory-efficient variation of [Tarjan's algorithm][2].

crates/bevy_ecs/src/schedule/node.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,19 @@ new_key_type! {
258258
impl GraphNodeId for SystemKey {
259259
type Adjacent = (SystemKey, Direction);
260260
type Edge = (SystemKey, SystemKey);
261+
262+
fn kind(&self) -> &'static str {
263+
"system"
264+
}
265+
}
266+
267+
impl GraphNodeId for SystemSetKey {
268+
type Adjacent = (SystemSetKey, Direction);
269+
type Edge = (SystemSetKey, SystemSetKey);
270+
271+
fn kind(&self) -> &'static str {
272+
"system set"
273+
}
261274
}
262275

263276
impl TryFrom<NodeId> for SystemKey {
@@ -324,6 +337,13 @@ impl NodeId {
324337
impl GraphNodeId for NodeId {
325338
type Adjacent = CompactNodeIdAndDirection;
326339
type Edge = CompactNodeIdPair;
340+
341+
fn kind(&self) -> &'static str {
342+
match self {
343+
NodeId::System(n) => n.kind(),
344+
NodeId::Set(n) => n.kind(),
345+
}
346+
}
327347
}
328348

329349
impl From<SystemKey> for NodeId {

crates/bevy_ecs/src/schedule/schedule.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,13 +1443,6 @@ impl ScheduleGraph {
14431443
)
14441444
}
14451445

1446-
fn get_node_kind(&self, id: &NodeId) -> &'static str {
1447-
match id {
1448-
NodeId::System(_) => "system",
1449-
NodeId::Set(_) => "system set",
1450-
}
1451-
}
1452-
14531446
/// If [`ScheduleBuildSettings::hierarchy_detection`] is [`LogLevel::Ignore`] this check
14541447
/// is skipped.
14551448
fn optionally_check_hierarchy_conflicts(
@@ -1481,7 +1474,7 @@ impl ScheduleGraph {
14811474
writeln!(
14821475
message,
14831476
" -- {} `{}` cannot be child of set `{}`, longer path exists",
1484-
self.get_node_kind(child),
1477+
child.kind(),
14851478
self.get_node_name(child),
14861479
self.get_node_name(parent),
14871480
)
@@ -1585,12 +1578,9 @@ impl ScheduleGraph {
15851578
) -> String {
15861579
let mut message = format!("schedule has {} before/after cycle(s):\n", cycles.len());
15871580
for (i, cycle) in cycles.iter().enumerate() {
1588-
let mut names = cycle.iter().map(|&id| {
1589-
(
1590-
self.get_node_kind(&id.into()),
1591-
self.get_node_name(&id.into()),
1592-
)
1593-
});
1581+
let mut names = cycle
1582+
.iter()
1583+
.map(|&id| (id.into().kind(), self.get_node_name(&id.into())));
15941584
let (first_kind, first_name) = names.next().unwrap();
15951585
writeln!(
15961586
message,

0 commit comments

Comments
 (0)