Don't unconditionally create temporary render entities for visible objects. (#16723)

PR #15756 made us create temporary render entities for all visible
objects, even if they had no render world counterpart. This regressed
our `many_cubes` time from about 3.59 ms/frame to 4.66 ms/frame.

This commit changes that behavior to use `Entity::PLACEHOLDER` instead
of creating a temporary render entity. This improves our `many_cubes`
time from 5.66 ms/frame to 3.96 ms/frame, a 43% speedup.

I tested 3D, 2D gizmos, and UI and they seem to work.

See the following graph of `many_cubes` frame time (lower is better). PR
#15756 is the one in October.

![Time (ms_frame) vs
Date(3)](https://github.com/user-attachments/assets/2c31a893-97bd-40f6-9e89-d2195a44cf40)
This commit is contained in:
Patrick Walton 2024-12-09 19:31:17 -08:00 committed by GitHub
parent 1cc4d1e8ac
commit a81c8f9744
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 12 deletions

View file

@ -31,7 +31,7 @@ use bevy_render::{
}; };
use bevy_render::{ use bevy_render::{
mesh::allocator::SlabId, mesh::allocator::SlabId,
sync_world::{MainEntity, RenderEntity, TemporaryRenderEntity}, sync_world::{MainEntity, RenderEntity},
}; };
use bevy_transform::{components::GlobalTransform, prelude::Transform}; use bevy_transform::{components::GlobalTransform, prelude::Transform};
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
@ -286,7 +286,7 @@ pub fn extract_lights(
let render_cubemap_visible_entities = RenderCubemapVisibleEntities { let render_cubemap_visible_entities = RenderCubemapVisibleEntities {
data: cubemap_visible_entities data: cubemap_visible_entities
.iter() .iter()
.map(|v| create_render_visible_mesh_entities(&mut commands, &mapper, v)) .map(|v| create_render_visible_mesh_entities(&mapper, v))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.try_into() .try_into()
.unwrap(), .unwrap(),
@ -343,7 +343,7 @@ pub fn extract_lights(
continue; continue;
} }
let render_visible_entities = let render_visible_entities =
create_render_visible_mesh_entities(&mut commands, &mapper, visible_entities); create_render_visible_mesh_entities(&mapper, visible_entities);
let texel_size = let texel_size =
2.0 * ops::tan(spot_light.outer_angle) / directional_light_shadow_map.size as f32; 2.0 * ops::tan(spot_light.outer_angle) / directional_light_shadow_map.size as f32;
@ -430,7 +430,7 @@ pub fn extract_lights(
cascade_visible_entities.insert( cascade_visible_entities.insert(
entity, entity,
v.iter() v.iter()
.map(|v| create_render_visible_mesh_entities(&mut commands, &mapper, v)) .map(|v| create_render_visible_mesh_entities(&mapper, v))
.collect(), .collect(),
); );
} else { } else {
@ -469,7 +469,6 @@ pub fn extract_lights(
} }
fn create_render_visible_mesh_entities( fn create_render_visible_mesh_entities(
commands: &mut Commands,
mapper: &Extract<Query<RenderEntity>>, mapper: &Extract<Query<RenderEntity>>,
visible_entities: &VisibleMeshEntities, visible_entities: &VisibleMeshEntities,
) -> RenderVisibleMeshEntities { ) -> RenderVisibleMeshEntities {
@ -477,9 +476,7 @@ fn create_render_visible_mesh_entities(
entities: visible_entities entities: visible_entities
.iter() .iter()
.map(|e| { .map(|e| {
let render_entity = mapper let render_entity = mapper.get(*e).unwrap_or(Entity::PLACEHOLDER);
.get(*e)
.unwrap_or_else(|_| commands.spawn(TemporaryRenderEntity).id());
(render_entity, MainEntity::from(*e)) (render_entity, MainEntity::from(*e))
}) })
.collect(), .collect(),

View file

@ -6,7 +6,7 @@ use crate::{
render_asset::RenderAssets, render_asset::RenderAssets,
render_graph::{InternedRenderSubGraph, RenderSubGraph}, render_graph::{InternedRenderSubGraph, RenderSubGraph},
render_resource::TextureView, render_resource::TextureView,
sync_world::{RenderEntity, SyncToRenderWorld, TemporaryRenderEntity}, sync_world::{RenderEntity, SyncToRenderWorld},
texture::GpuImage, texture::GpuImage,
view::{ view::{
ColorGrading, ExtractedView, ExtractedWindows, GpuCulling, Msaa, RenderLayers, ColorGrading, ExtractedView, ExtractedWindows, GpuCulling, Msaa, RenderLayers,
@ -1098,9 +1098,7 @@ pub fn extract_cameras(
.get(*entity) .get(*entity)
.cloned() .cloned()
.map(|entity| entity.id()) .map(|entity| entity.id())
.unwrap_or_else(|_e| { .unwrap_or(Entity::PLACEHOLDER);
commands.spawn(TemporaryRenderEntity).id()
});
(render_entity, (*entity).into()) (render_entity, (*entity).into())
}) })
.collect(); .collect();