Repository: bevyengine/bevy
File: crates/bevy_ecs/src/schedule/schedule.rs (Rust)

Code before:
    pub struct SystemSetKey;
}

/// Systems and system sets in a [`ScheduleGraph`] which have not been
/// initialized yet.
///
/// We have to defer initialization of nodes in the graph until we have
/// `&mut World` access, so we store these in a list ([`ScheduleGraph::uninit`])
/// until then. In most cases, initialization occurs upon the first run of the
/// schedule.
enum UninitializedId {
    /// A system and its conditions that have not been initialized yet.
    System(SystemKey),
    /// A system set's conditions that have not been initialized yet.
    Set {
        key: SystemSetKey,
        /// The index in [`SystemSets::conditions`] where the first
        /// uninitialized condition is. All conditions after this index also
        /// still need to be initialized.
        ///
        /// Conditions might be added multiple times to a system set (e.g. when
        /// `configure_sets` is called multiple times with the same set), So we
        /// need to track where the newly added conditions start in the
        /// `conditions` list.
        ///
        /// Note: We don't need to do this for systems because calling
        /// `add_systems` multiple times with the same system results in
        /// multiple duplicates of that system in the graph, each with their own
        /// separate list of conditions. That means all of a system's conditions
        /// are always initialized together.
        first_uninit_condition: usize,
    },
}

/// Metadata for system sets in a schedule.
#[derive(Default)]
struct SystemSets {
    /// List of system sets in the schedule
    sets: SlotMap<SystemSetKey, SystemSetNode>,
    /// List of conditions for each system set, in the same order as `system_sets`
    conditions: SecondaryMap<SystemSetKey, Vec<ConditionWithAccess>>,
    /// Map from system set to node id
    ids: HashMap<InternedSystemSet, SystemSetKey>,
}

impl SystemSets {
    fn get_or_add_set(&mut self, set: InternedSystemSet) -> SystemSetKey {
        *self.ids.entry(set).or_insert_with(|| {
            let key = self.sets.insert(SystemSetNode::new(set));
            self.conditions.insert(key, Vec::new());
            key

Review comment (suggestion):
Thank you for writing this down!  I remember that it took me a while to figure out how this index worked and why it wasn't relevant for systems.  

... And a belated thank you for giving it a name and making it not exist for systems, since the magic `0` index made it even harder to figure out.  

Code after:
    /// A unique identifier for a system set in a [`ScheduleGraph`].
    pub struct SystemSetKey;
}

/// A node in a [`ScheduleGraph`] with a system or conditions that have not been
/// initialized yet.
///
/// We have to defer initialization of nodes in the graph until we have
/// `&mut World` access, so we store these in a list ([`ScheduleGraph::uninit`])
/// until then. In most cases, initialization occurs upon the first run of the
/// schedule.
enum UninitializedId {
    /// A system and its conditions that have not been initialized yet.
    System(SystemKey),
    /// A system set's conditions that have not been initialized yet.
    Set {
        key: SystemSetKey,
        /// The range of indices in [`SystemSets::conditions`] that correspond
        /// to conditions that have not been initialized yet.
        ///
        /// [`SystemSets::conditions`] for a given set may be appended to
        /// multiple times (e.g. when `configure_sets` is called multiple with
        /// the same set), so we need to track which conditions in that list
        /// are newly added and not yet initialized.
        ///
        /// Systems don't need this tracking because each `add_systems` call
        /// creates separate nodes in the graph with their own conditions,
        /// so all conditions are initialized together.
        uninitialized_conditions: Range<usize>,
    },
}

/// Metadata for system sets in a schedule.
#[derive(Default)]
struct SystemSets {
    /// List of system sets in the schedule
    sets: SlotMap<SystemSetKey, SystemSetNode>,
    /// List of conditions for each system set, in the same order as `system_sets`
    conditions: SecondaryMap<SystemSetKey, Vec<ConditionWithAccess>>,
    /// Map from system set to node id
    ids: HashMap<InternedSystemSet, SystemSetKey>,
}

impl SystemSets {
    fn get_or_add_set(&mut self, set: InternedSystemSet) -> SystemSetKey {
        *self.ids.entry(set).or_insert_with(|| {
            let key = self.sets.insert(SystemSetNode::new(set));
            self.conditions.insert(key, Vec::new());
            key
        })
    }