Skip to content

Commit f1eace6

Browse files
ItsDootchescock
andauthored
Thoroughly document UninitializedId semantics (#20080)
# Objective Clean up documentation around `UninitializedId`, which has slightly confusing semantics. ## Solution Added documentation comments on `UninitializedId`. --------- Co-authored-by: Chris Russell <8494645+chescock@users.noreply.github.com>
1 parent 20dfae9 commit f1eace6

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

crates/bevy_ecs/src/schedule/schedule.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use bevy_utils::{default, prelude::DebugName, TypeIdMap};
1515
use core::{
1616
any::{Any, TypeId},
1717
fmt::{Debug, Write},
18+
ops::Range,
1819
};
1920
use fixedbitset::FixedBitSet;
2021
use log::{error, info, warn};
@@ -752,11 +753,31 @@ new_key_type! {
752753
pub struct SystemSetKey;
753754
}
754755

756+
/// A node in a [`ScheduleGraph`] with a system or conditions that have not been
757+
/// initialized yet.
758+
///
759+
/// We have to defer initialization of nodes in the graph until we have
760+
/// `&mut World` access, so we store these in a list ([`ScheduleGraph::uninit`])
761+
/// until then. In most cases, initialization occurs upon the first run of the
762+
/// schedule.
755763
enum UninitializedId {
764+
/// A system and its conditions that have not been initialized yet.
756765
System(SystemKey),
766+
/// A system set's conditions that have not been initialized yet.
757767
Set {
758768
key: SystemSetKey,
759-
first_uninit_condition: usize,
769+
/// The range of indices in [`SystemSets::conditions`] that correspond
770+
/// to conditions that have not been initialized yet.
771+
///
772+
/// [`SystemSets::conditions`] for a given set may be appended to
773+
/// multiple times (e.g. when `configure_sets` is called multiple with
774+
/// the same set), so we need to track which conditions in that list
775+
/// are newly added and not yet initialized.
776+
///
777+
/// Systems don't need this tracking because each `add_systems` call
778+
/// creates separate nodes in the graph with their own conditions,
779+
/// so all conditions are initialized together.
780+
uninitialized_conditions: Range<usize>,
760781
},
761782
}
762783

@@ -793,8 +814,8 @@ pub struct ScheduleGraph {
793814
pub system_conditions: SecondaryMap<SystemKey, Vec<ConditionWithAccess>>,
794815
/// Data about system sets in the schedule
795816
system_sets: SystemSets,
796-
/// Systems that have not been initialized yet; for system sets, we store the index of the first uninitialized condition
797-
/// (all the conditions after that index still need to be initialized)
817+
/// Systems, their conditions, and system set conditions that need to be
818+
/// initialized before the schedule can be run.
798819
uninit: Vec<UninitializedId>,
799820
/// Directed acyclic graph of the hierarchy (which systems/sets are children of which sets)
800821
hierarchy: Dag,
@@ -807,7 +828,6 @@ pub struct ScheduleGraph {
807828
anonymous_sets: usize,
808829
changed: bool,
809830
settings: ScheduleBuildSettings,
810-
811831
passes: BTreeMap<TypeId, Box<dyn ScheduleBuildPassObj>>,
812832
}
813833

@@ -1101,9 +1121,10 @@ impl ScheduleGraph {
11011121

11021122
// system init has to be deferred (need `&mut World`)
11031123
let system_set_conditions = self.system_sets.conditions.entry(key).unwrap().or_default();
1124+
let start = system_set_conditions.len();
11041125
self.uninit.push(UninitializedId::Set {
11051126
key,
1106-
first_uninit_condition: system_set_conditions.len(),
1127+
uninitialized_conditions: start..(start + conditions.len()),
11071128
});
11081129
system_set_conditions.extend(conditions.into_iter().map(ConditionWithAccess::new));
11091130

@@ -1189,11 +1210,9 @@ impl ScheduleGraph {
11891210
}
11901211
UninitializedId::Set {
11911212
key,
1192-
first_uninit_condition,
1213+
uninitialized_conditions,
11931214
} => {
1194-
for condition in self.system_sets.conditions[key]
1195-
.iter_mut()
1196-
.skip(first_uninit_condition)
1215+
for condition in &mut self.system_sets.conditions[key][uninitialized_conditions]
11971216
{
11981217
condition.access = condition.condition.initialize(world);
11991218
}

0 commit comments

Comments
 (0)