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

Code before:
    ///
    /// let mut world = World::default();
    /// let counter_one = world.register_system(increment);
    /// let counter_two = world.register_system(increment);
    /// assert_eq!(world.run_system_with(counter_one, 1).unwrap(), 1);
    /// assert_eq!(world.run_system_with(counter_one, 20).unwrap(), 21);
    /// assert_eq!(world.run_system_with(counter_two, 30).unwrap(), 30);
    /// ```
    ///
    /// See [`World::run_system`] for more examples.
    pub fn run_system_with<I, O>(
        &mut self,
        id: SystemId<I, O>,
        input: I::Inner<'_>,
    ) -> Result<O, RegisteredSystemError<I, O>>
    where
        I: SystemInput + 'static,
        O: 'static,
    {
        // Lookup
        let mut entity = self
            .get_entity_mut(id.entity)
            .map_err(|_| RegisteredSystemError::SystemIdNotRegistered(id))?;

        let Some(system_id_marker) = entity.get::<SystemIdMarker>() else {
            return Err(RegisteredSystemError::SystemNotCached);
        };

        if system_id_marker.input_type_id.type_id != TypeId::of::<I>()
            || system_id_marker.output_type_id.type_id != TypeId::of::<O>()
        {
            return Err(RegisteredSystemError::IncorrectType(
                id,
                system_id_marker.clone(),
            ));
        }

        // Take ownership of system trait object
        let RegisteredSystem {
            mut initialized,
            mut system,
        } = entity
            .take::<RegisteredSystem<I, O>>()
            .ok_or(RegisteredSystemError::Recursive(id))?;

        // Initialize the system
        if !initialized {
            system.initialize(self);
            initialized = true;
        }


Review comment (performance):
The docs for `SystemNotCached` say "A cached system was removed by value, but no system with its type was found".  I think we either want to return `SystemIdNotRegistered` here, or update the docs.  

Code after:
    ///
    /// let mut world = World::default();
    /// let counter_one = world.register_system(increment);
    /// let counter_two = world.register_system(increment);
    /// assert_eq!(world.run_system_with(counter_one, 1).unwrap(), 1);
    /// assert_eq!(world.run_system_with(counter_one, 20).unwrap(), 21);
    /// assert_eq!(world.run_system_with(counter_two, 30).unwrap(), 30);
    /// ```
    ///
    /// See [`World::run_system`] for more examples.
    pub fn run_system_with<I, O>(
        &mut self,
        id: SystemId<I, O>,
        input: I::Inner<'_>,
    ) -> Result<O, RegisteredSystemError<I, O>>
    where
        I: SystemInput + 'static,
        O: 'static,
    {
        // Lookup
        let mut entity = self
            .get_entity_mut(id.entity)
            .map_err(|_| RegisteredSystemError::SystemIdNotRegistered(id))?;

        // Take ownership of system trait object
        let Some(RegisteredSystem {
            mut initialized,
            mut system,
        }) = entity.take::<RegisteredSystem<I, O>>()
        else {
            let Some(system_id_marker) = entity.get::<SystemIdMarker>() else {
                return Err(RegisteredSystemError::SystemIdNotRegistered(id));
            };
            if system_id_marker.input_type_id.type_id != TypeId::of::<I>()
                || system_id_marker.output_type_id.type_id != TypeId::of::<O>()
            {
                return Err(RegisteredSystemError::IncorrectType(
                    id,
                    system_id_marker.clone(),
                ));
            }
            return Err(RegisteredSystemError::Recursive(id));
        };

        // Initialize the system
        if !initialized {
            system.initialize(self);
            initialized = true;
        }

        // refresh hotpatches for stored systems