Skip to content

Improve node encapsulation in ScheduleGraph #20119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions crates/bevy_ecs/src/schedule/auto_insert_apply_deferred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use bevy_platform::collections::HashMap;

use crate::{
schedule::{SystemKey, SystemSetKey},
system::IntoSystem,
system::{IntoSystem, System},
world::World,
};

use super::{
is_apply_deferred, ApplyDeferred, DiGraph, Direction, NodeId, ReportCycles, ScheduleBuildError,
ScheduleBuildPass, ScheduleGraph, SystemNode,
ScheduleBuildPass, ScheduleGraph,
};

/// A [`ScheduleBuildPass`] that inserts [`ApplyDeferred`] systems into the schedule graph
Expand Down Expand Up @@ -49,10 +49,7 @@ impl AutoInsertApplyDeferredPass {
fn add_auto_sync(&mut self, graph: &mut ScheduleGraph) -> SystemKey {
let key = graph
.systems
.insert(SystemNode::new(Box::new(IntoSystem::into_system(
ApplyDeferred,
))));
graph.system_conditions.insert(key, Vec::new());
.insert(Box::new(IntoSystem::into_system(ApplyDeferred)), Vec::new());

// ignore ambiguities with auto sync points
// They aren't under user control, so no one should know or care.
Expand Down Expand Up @@ -81,7 +78,7 @@ impl ScheduleBuildPass for AutoInsertApplyDeferredPass {
let topo = graph.topsort_graph(dependency_flattened, ReportCycles::Dependency)?;

fn set_has_conditions(graph: &ScheduleGraph, set: SystemSetKey) -> bool {
!graph.set_conditions_at(set).is_empty()
graph.system_sets.has_conditions(set)
|| graph
.hierarchy()
.graph()
Expand All @@ -94,7 +91,7 @@ impl ScheduleBuildPass for AutoInsertApplyDeferredPass {
}

fn system_has_conditions(graph: &ScheduleGraph, key: SystemKey) -> bool {
!graph.system_conditions[key].is_empty()
graph.systems.has_conditions(key)
|| graph
.hierarchy()
.graph()
Expand All @@ -108,7 +105,7 @@ impl ScheduleBuildPass for AutoInsertApplyDeferredPass {

let mut system_has_conditions_cache = HashMap::<SystemKey, bool>::default();
let mut is_valid_explicit_sync_point = |key: SystemKey| {
is_apply_deferred(&graph.systems[key].get().unwrap().system)
is_apply_deferred(&graph.systems[key])
&& !*system_has_conditions_cache
.entry(key)
.or_insert_with(|| system_has_conditions(graph, key))
Expand Down Expand Up @@ -148,7 +145,7 @@ impl ScheduleBuildPass for AutoInsertApplyDeferredPass {
} else if !node_needs_sync {
// No previous node has postponed sync points to add so check if the system itself
// has deferred params that require a sync point to apply them.
node_needs_sync = graph.systems[key].get().unwrap().system.has_deferred();
node_needs_sync = graph.systems[key].has_deferred();
}

for target in dependency_flattened.neighbors_directed(*node, Direction::Outgoing) {
Expand All @@ -160,7 +157,7 @@ impl ScheduleBuildPass for AutoInsertApplyDeferredPass {

let mut edge_needs_sync = node_needs_sync;
if node_needs_sync
&& !graph.systems[target].get().unwrap().system.is_exclusive()
&& !graph.systems[target].is_exclusive()
&& self
.no_sync_edges
.contains(&(*node, NodeId::System(target)))
Expand Down Expand Up @@ -210,7 +207,7 @@ impl ScheduleBuildPass for AutoInsertApplyDeferredPass {
continue;
}

if is_apply_deferred(&graph.systems[target].get().unwrap().system) {
if is_apply_deferred(&graph.systems[target]) {
// We don't need to insert a sync point since ApplyDeferred is a sync point
// already!
continue;
Expand Down
7 changes: 2 additions & 5 deletions crates/bevy_ecs/src/schedule/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ use crate::{
ConditionWithAccess, InternedSystemSet, SystemKey, SystemSetKey, SystemTypeSet,
SystemWithAccess,
},
system::{
RunSystemError, ScheduleSystem, System, SystemIn, SystemParamValidationError,
SystemStateFlags,
},
system::{RunSystemError, System, SystemIn, SystemParamValidationError, SystemStateFlags},
world::{unsafe_world_cell::UnsafeWorldCell, DeferredWorld, World},
};

Expand Down Expand Up @@ -157,7 +154,7 @@ impl SystemSchedule {
pub struct ApplyDeferred;

/// Returns `true` if the [`System`] is an instance of [`ApplyDeferred`].
pub(super) fn is_apply_deferred(system: &ScheduleSystem) -> bool {
pub(super) fn is_apply_deferred(system: &dyn System<In = (), Out = ()>) -> bool {
system.type_id() == TypeId::of::<ApplyDeferred>()
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/schedule/executor/multi_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ impl ExecutorState {
// Move the full context object into the new future.
let context = *context;

if is_apply_deferred(system) {
if is_apply_deferred(&**system) {
// TODO: avoid allocation
let unapplied_systems = self.unapplied_systems.clone();
self.unapplied_systems.clear();
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/schedule/executor/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl SystemExecutor for SimpleExecutor {
continue;
}

if is_apply_deferred(system) {
if is_apply_deferred(&**system) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/schedule/executor/single_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl SystemExecutor for SingleThreadedExecutor {
continue;
}

if is_apply_deferred(system) {
if is_apply_deferred(&**system) {
self.apply_deferred(schedule, world);
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ecs/src/schedule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ mod auto_insert_apply_deferred;
mod condition;
mod config;
mod executor;
mod node;
mod pass;
mod schedule;
mod set;
mod stepping;

use self::graph::*;
pub use self::{condition::*, config::*, executor::*, schedule::*, set::*};
pub use self::{condition::*, config::*, executor::*, node::*, schedule::*, set::*};
pub use pass::ScheduleBuildPass;

pub use self::graph::NodeId;
Expand Down
Loading
Loading