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

Code before:
    /// Creates a new [`UnsafeWorldCell`] view with only read access to everything.
    #[inline]
    pub fn as_unsafe_world_cell_readonly(&self) -> UnsafeWorldCell<'_> {
        UnsafeWorldCell::new_readonly(self)
    }

    /// Retrieves this world's [`Entities`] collection.
    #[inline]
    pub fn entities(&self) -> &Entities {
        &self.entities
    }

    /// Retrieves this world's [`Entities`] collection mutably.
    ///
    /// # Safety
    /// Mutable reference must not be used to put the [`Entities`] data
    /// in an invalid state for this [`World`]
    #[inline]
    pub unsafe fn entities_mut(&mut self) -> &mut Entities {
        &mut self.entities
    }

    /// Retrieves the number of [`Entities`] in the world.
    ///
    /// This is helpful as a diagnostic, but it can also be used effectively in tests.
    /// This function excludes internal entities. For more information look at the [`Internal`] documentation, or use [`World::internal_entity_count`].
    #[inline]
    pub fn entity_count(&self) -> u32 {
        self.entities.len() - self.internal_entity_count()
    }

    /// Counts the number of internal entities, entities marked by the [`Internal`] marker component.
    pub fn internal_entity_count(&self) -> u32 {
        if let Some(internal_component_id) = self.components().component_id::<Internal>()
            && let Some(archetype_map) = self
                .archetypes()
                .component_index()
                .get(&internal_component_id)
        {
            let mut total = 0;
            for archetype_id in archetype_map.keys() {
                if let Some(archetype) = self.archetypes().get(*archetype_id) {
                    total += archetype.len();
                }
            }
            return total;
        }
        0
    }

    /// Retrieves this world's [`Archetypes`] collection.

Review comment (suggestion):
I would vote to remove the changes to this method from this PR, but maybe also remove the line in the doc comment that "it can also be used effectively in tests".  

The only real advantage of `entity_count()` over a `Query<()>` is that it can be O(1) since it just looks at the length of the `Vec<EntityMeta>`.  If we're going to do a bunch of per-archetype work anyway, then users might as well use a `Query`.  

It also feels odd to special-case `Internal` but not any other custom default query filters.  

Code after:
    #[inline]
    pub fn as_unsafe_world_cell_readonly(&self) -> UnsafeWorldCell<'_> {
        UnsafeWorldCell::new_readonly(self)
    }

    /// Retrieves this world's [`Entities`] collection.
    #[inline]
    pub fn entities(&self) -> &Entities {
        &self.entities
    }

    /// Retrieves this world's [`Entities`] collection mutably.
    ///
    /// # Safety
    /// Mutable reference must not be used to put the [`Entities`] data
    /// in an invalid state for this [`World`]
    #[inline]
    pub unsafe fn entities_mut(&mut self) -> &mut Entities {
        &mut self.entities
    }

    /// Retrieves the number of [`Entities`] in the world.
    ///
    /// This is helpful as a diagnostic, but it can also be used effectively in tests.
    #[inline]
    pub fn entity_count(&self) -> u32 {
        self.entities.len()
    }

    /// Retrieves this world's [`Archetypes`] collection.
    #[inline]
    pub fn archetypes(&self) -> &Archetypes {
        &self.archetypes
    }

    /// Retrieves this world's [`Components`] collection.
    #[inline]
    pub fn components(&self) -> &Components {
        &self.components
    }

    /// Prepares a [`ComponentsQueuedRegistrator`] for the world.
    /// **NOTE:** [`ComponentsQueuedRegistrator`] is easily misused.
    /// See its docs for important notes on when and how it should be used.
    #[inline]
    pub fn components_queue(&self) -> ComponentsQueuedRegistrator {
        // SAFETY: These are from the same world.
        unsafe { ComponentsQueuedRegistrator::new(&self.components, &self.component_ids) }
    }

    /// Prepares a [`ComponentsRegistrator`] for the world.