Skip to content

Commit 013c33e

Browse files
committed
Shrink DrawFunctionId (#6944)
# Objective This includes one part of #4899. The aim is to improve CPU-side rendering performance by reducing the memory footprint and bandwidth required. ## Solution Shrink `DrawFunctionId` to `u32`. Enforce that `u32 as usize` conversions are always safe by forbidding compilation on 16-bit platforms. This shouldn't be a breaking change since #4736 disabled compilation of `bevy_ecs` on those platforms. Shrinking `DrawFunctionId` shrinks all of the `PhaseItem` types, which is integral to sort and render phase performance. Testing against `many_cubes`, the sort phase improved by 22% (174.21us -> 141.76us per frame). ![image](https://user-images.githubusercontent.com/3137680/207345422-a512b4cf-1680-46e0-9973-ea72494ebdfe.png) The main opaque pass also imrproved by 9% (5.49ms -> 5.03ms) ![image](https://user-images.githubusercontent.com/3137680/207346436-cbee7209-6450-4964-b566-0b64cfa4b4ea.png) Overall frame time improved by 5% (14.85ms -> 14.09ms) ![image](https://user-images.githubusercontent.com/3137680/207346895-9de8676b-ef37-4cb9-8445-8493f5f90003.png) There will be a followup PR that likewise shrinks `CachedRenderPipelineId` which should yield similar results on top of these improvements.
1 parent 2b50bc8 commit 013c33e

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

crates/bevy_render/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[cfg(target_pointer_width = "16")]
2+
compile_error!("bevy_render cannot compile for a 16-bit platform.");
3+
14
extern crate core;
25

36
pub mod camera;

crates/bevy_render/src/render_phase/draw.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub trait PhaseItem: Sized + Send + Sync + 'static {
6565
// TODO: make this generic?
6666
/// An identifier for a [`Draw`] function stored in [`DrawFunctions`].
6767
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
68-
pub struct DrawFunctionId(usize);
68+
pub struct DrawFunctionId(u32);
6969

7070
/// Stores all draw functions for the [`PhaseItem`] type.
7171
/// For retrieval they are associated with their [`TypeId`].
@@ -82,15 +82,15 @@ impl<P: PhaseItem> DrawFunctionsInternal<P> {
8282

8383
/// Adds the [`Draw`] function and associates it to the type `T`
8484
pub fn add_with<T: 'static, D: Draw<P>>(&mut self, draw_function: D) -> DrawFunctionId {
85+
let id = DrawFunctionId(self.draw_functions.len().try_into().unwrap());
8586
self.draw_functions.push(Box::new(draw_function));
86-
let id = DrawFunctionId(self.draw_functions.len() - 1);
8787
self.indices.insert(TypeId::of::<T>(), id);
8888
id
8989
}
9090

9191
/// Retrieves the [`Draw`] function corresponding to the `id` mutably.
9292
pub fn get_mut(&mut self, id: DrawFunctionId) -> Option<&mut dyn Draw<P>> {
93-
self.draw_functions.get_mut(id.0).map(|f| &mut **f)
93+
self.draw_functions.get_mut(id.0 as usize).map(|f| &mut **f)
9494
}
9595

9696
/// Retrieves the id of the [`Draw`] function corresponding to their associated type `T`.

0 commit comments

Comments
 (0)