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:
		
							parent
							
								
									9ca5540b75
								
							
						
					
					
						commit
						340c749d16
					
				| @ -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 { |     fn init_state(world: &mut World, system_meta: &mut SystemMeta) -> Self::State { | ||||||
|         let component_id = world.components.init_resource::<T>(); |         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(); |         let combined_access = system_meta.component_access_set.combined_access(); | ||||||
|         assert!( |         assert!( | ||||||
| @ -490,9 +490,6 @@ unsafe impl<'a, T: Resource> SystemParam for Res<'a, T> { | |||||||
|             .component_access_set |             .component_access_set | ||||||
|             .add_unfiltered_resource_read(component_id); |             .add_unfiltered_resource_read(component_id); | ||||||
| 
 | 
 | ||||||
|         let archetype_component_id = world |  | ||||||
|             .get_resource_archetype_component_id(component_id) |  | ||||||
|             .unwrap(); |  | ||||||
|         system_meta |         system_meta | ||||||
|             .archetype_component_access |             .archetype_component_access | ||||||
|             .add_resource_read(archetype_component_id); |             .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 { |     fn init_state(world: &mut World, system_meta: &mut SystemMeta) -> Self::State { | ||||||
|         let component_id = world.components.init_resource::<T>(); |         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(); |         let combined_access = system_meta.component_access_set.combined_access(); | ||||||
|         if combined_access.has_resource_write(component_id) { |         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 |             .component_access_set | ||||||
|             .add_unfiltered_resource_write(component_id); |             .add_unfiltered_resource_write(component_id); | ||||||
| 
 | 
 | ||||||
|         let archetype_component_id = world |  | ||||||
|             .get_resource_archetype_component_id(component_id) |  | ||||||
|             .unwrap(); |  | ||||||
|         system_meta |         system_meta | ||||||
|             .archetype_component_access |             .archetype_component_access | ||||||
|             .add_resource_write(archetype_component_id); |             .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(); |         system_meta.set_non_send(); | ||||||
| 
 | 
 | ||||||
|         let component_id = world.components.init_non_send::<T>(); |         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(); |         let combined_access = system_meta.component_access_set.combined_access(); | ||||||
|         assert!( |         assert!( | ||||||
| @ -1125,9 +1119,6 @@ unsafe impl<'a, T: 'static> SystemParam for NonSend<'a, T> { | |||||||
|             .component_access_set |             .component_access_set | ||||||
|             .add_unfiltered_resource_read(component_id); |             .add_unfiltered_resource_read(component_id); | ||||||
| 
 | 
 | ||||||
|         let archetype_component_id = world |  | ||||||
|             .get_non_send_archetype_component_id(component_id) |  | ||||||
|             .unwrap(); |  | ||||||
|         system_meta |         system_meta | ||||||
|             .archetype_component_access |             .archetype_component_access | ||||||
|             .add_resource_read(archetype_component_id); |             .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(); |         system_meta.set_non_send(); | ||||||
| 
 | 
 | ||||||
|         let component_id = world.components.init_non_send::<T>(); |         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(); |         let combined_access = system_meta.component_access_set.combined_access(); | ||||||
|         if combined_access.has_component_write(component_id) { |         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 |             .component_access_set | ||||||
|             .add_unfiltered_resource_write(component_id); |             .add_unfiltered_resource_write(component_id); | ||||||
| 
 | 
 | ||||||
|         let archetype_component_id = world |  | ||||||
|             .get_non_send_archetype_component_id(component_id) |  | ||||||
|             .unwrap(); |  | ||||||
|         system_meta |         system_meta | ||||||
|             .archetype_component_access |             .archetype_component_access | ||||||
|             .add_resource_write(archetype_component_id); |             .add_resource_write(archetype_component_id); | ||||||
|  | |||||||
| @ -26,7 +26,7 @@ pub use identifier::WorldId; | |||||||
| pub use spawn_batch::*; | pub use spawn_batch::*; | ||||||
| 
 | 
 | ||||||
| use crate::{ | use crate::{ | ||||||
|     archetype::{ArchetypeComponentId, ArchetypeId, ArchetypeRow, Archetypes}, |     archetype::{ArchetypeId, ArchetypeRow, Archetypes}, | ||||||
|     bundle::{Bundle, BundleInfo, BundleInserter, BundleSpawner, Bundles}, |     bundle::{Bundle, BundleInfo, BundleInserter, BundleSpawner, Bundles}, | ||||||
|     change_detection::{MutUntyped, TicksMut}, |     change_detection::{MutUntyped, TicksMut}, | ||||||
|     component::{ |     component::{ | ||||||
| @ -1783,26 +1783,6 @@ impl World { | |||||||
|         unsafe { self.as_unsafe_world_cell().get_non_send_resource_mut() } |         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
 |     /// 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).
 |     /// 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.
 |     /// This is faster than doing equivalent operations one-by-one.
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Chris Russell
						Chris Russell