Remove redundant ArchetypeComponentId lookup in Res and ResMut (#14691)

# Objective

`Res` and `ResMut` perform redundant lookups of the resource storage,
first to initialize the `ArchetypeComponentId` and then to retrieve it.

## Solution

Use the `archetype_component_id` returned from
`initialize_resource_internal` to avoid an extra lookup and `unwrap()`.
This commit is contained in:
Chris Russell 2024-08-15 12:12:03 -04:00 committed by GitHub
parent 9ca5540b75
commit 340c749d16
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 5 additions and 37 deletions

View file

@ -477,7 +477,7 @@ unsafe impl<'a, T: Resource> SystemParam for Res<'a, T> {
fn init_state(world: &mut World, system_meta: &mut SystemMeta) -> Self::State {
let component_id = world.components.init_resource::<T>();
world.initialize_resource_internal(component_id);
let archetype_component_id = world.initialize_resource_internal(component_id).id();
let combined_access = system_meta.component_access_set.combined_access();
assert!(
@ -490,9 +490,6 @@ unsafe impl<'a, T: Resource> SystemParam for Res<'a, T> {
.component_access_set
.add_unfiltered_resource_read(component_id);
let archetype_component_id = world
.get_resource_archetype_component_id(component_id)
.unwrap();
system_meta
.archetype_component_access
.add_resource_read(archetype_component_id);
@ -574,7 +571,7 @@ unsafe impl<'a, T: Resource> SystemParam for ResMut<'a, T> {
fn init_state(world: &mut World, system_meta: &mut SystemMeta) -> Self::State {
let component_id = world.components.init_resource::<T>();
world.initialize_resource_internal(component_id);
let archetype_component_id = world.initialize_resource_internal(component_id).id();
let combined_access = system_meta.component_access_set.combined_access();
if combined_access.has_resource_write(component_id) {
@ -590,9 +587,6 @@ unsafe impl<'a, T: Resource> SystemParam for ResMut<'a, T> {
.component_access_set
.add_unfiltered_resource_write(component_id);
let archetype_component_id = world
.get_resource_archetype_component_id(component_id)
.unwrap();
system_meta
.archetype_component_access
.add_resource_write(archetype_component_id);
@ -1112,7 +1106,7 @@ unsafe impl<'a, T: 'static> SystemParam for NonSend<'a, T> {
system_meta.set_non_send();
let component_id = world.components.init_non_send::<T>();
world.initialize_non_send_internal(component_id);
let archetype_component_id = world.initialize_non_send_internal(component_id).id();
let combined_access = system_meta.component_access_set.combined_access();
assert!(
@ -1125,9 +1119,6 @@ unsafe impl<'a, T: 'static> SystemParam for NonSend<'a, T> {
.component_access_set
.add_unfiltered_resource_read(component_id);
let archetype_component_id = world
.get_non_send_archetype_component_id(component_id)
.unwrap();
system_meta
.archetype_component_access
.add_resource_read(archetype_component_id);
@ -1206,7 +1197,7 @@ unsafe impl<'a, T: 'static> SystemParam for NonSendMut<'a, T> {
system_meta.set_non_send();
let component_id = world.components.init_non_send::<T>();
world.initialize_non_send_internal(component_id);
let archetype_component_id = world.initialize_non_send_internal(component_id).id();
let combined_access = system_meta.component_access_set.combined_access();
if combined_access.has_component_write(component_id) {
@ -1222,9 +1213,6 @@ unsafe impl<'a, T: 'static> SystemParam for NonSendMut<'a, T> {
.component_access_set
.add_unfiltered_resource_write(component_id);
let archetype_component_id = world
.get_non_send_archetype_component_id(component_id)
.unwrap();
system_meta
.archetype_component_access
.add_resource_write(archetype_component_id);

View file

@ -26,7 +26,7 @@ pub use identifier::WorldId;
pub use spawn_batch::*;
use crate::{
archetype::{ArchetypeComponentId, ArchetypeId, ArchetypeRow, Archetypes},
archetype::{ArchetypeId, ArchetypeRow, Archetypes},
bundle::{Bundle, BundleInfo, BundleInserter, BundleSpawner, Bundles},
change_detection::{MutUntyped, TicksMut},
component::{
@ -1783,26 +1783,6 @@ impl World {
unsafe { self.as_unsafe_world_cell().get_non_send_resource_mut() }
}
// Shorthand helper function for getting the [`ArchetypeComponentId`] for a resource.
#[inline]
pub(crate) fn get_resource_archetype_component_id(
&self,
component_id: ComponentId,
) -> Option<ArchetypeComponentId> {
let resource = self.storages.resources.get(component_id)?;
Some(resource.id())
}
// Shorthand helper function for getting the [`ArchetypeComponentId`] for a resource.
#[inline]
pub(crate) fn get_non_send_archetype_component_id(
&self,
component_id: ComponentId,
) -> Option<ArchetypeComponentId> {
let resource = self.storages.non_send_resources.get(component_id)?;
Some(resource.id())
}
/// For a given batch of ([`Entity`], [`Bundle`]) pairs, either spawns each [`Entity`] with the given
/// bundle (if the entity does not exist), or inserts the [`Bundle`] (if the entity already exists).
/// This is faster than doing equivalent operations one-by-one.