Skip to content

Commit 88b353c

Browse files
committed
Reduce the use of atomics in the render phase (#7084)
# Objective Speed up the render phase of rendering. An extension of #6885. `SystemState::get` increments the `World`'s change tick atomically every time it's called. This is notably more expensive than a unsynchronized increment, even without contention. It also updates the archetypes, even when there has been nothing to update when it's called repeatedly. ## Solution Piggyback off of #6885. Split `SystemState::validate_world_and_update_archetypes` into `SystemState::validate_world` and `SystemState::update_archetypes`, and make the later `pub`. Then create safe variants of `SystemState::get_unchecked_manual` that still validate the `World` but do not update archetypes and do not increment the change tick using `World::read_change_tick` and `World::change_tick`. Update `RenderCommandState` to call `SystemState::update_archetypes` in `Draw::prepare` and `SystemState::get_manual` in `Draw::draw`. ## Performance There's a slight perf benefit (~2%) for `main_opaque_pass_3d` on `many_foxes` (340.39 us -> 333.32 us) ![image](https://user-images.githubusercontent.com/3137680/210643746-25320b98-3e2b-4a95-8084-892c23bb8b4e.png) ## Alternatives We can change `SystemState::get` to not increment the `World`'s change tick. Though this would still put updating the archetypes and an atomic read on the hot-path. --- ## Changelog Added: `SystemState::get_manual` Added: `SystemState::get_manual_mut` Added: `SystemState::update_archetypes`
1 parent 9eefd7c commit 88b353c

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

crates/bevy_ecs/src/system/function_system.rs

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,17 @@ impl<Param: SystemParam> SystemState<Param> {
170170
where
171171
Param: ReadOnlySystemParam,
172172
{
173-
self.validate_world_and_update_archetypes(world);
173+
self.validate_world(world);
174+
self.update_archetypes(world);
174175
// SAFETY: Param is read-only and doesn't allow mutable access to World. It also matches the World this SystemState was created with.
175176
unsafe { self.get_unchecked_manual(world) }
176177
}
177178

178179
/// Retrieve the mutable [`SystemParam`] values.
179180
#[inline]
180181
pub fn get_mut<'w, 's>(&'s mut self, world: &'w mut World) -> SystemParamItem<'w, 's, Param> {
181-
self.validate_world_and_update_archetypes(world);
182+
self.validate_world(world);
183+
self.update_archetypes(world);
182184
// SAFETY: World is uniquely borrowed and matches the World this SystemState was created with.
183185
unsafe { self.get_unchecked_manual(world) }
184186
}
@@ -196,8 +198,20 @@ impl<Param: SystemParam> SystemState<Param> {
196198
self.world_id == world.id()
197199
}
198200

199-
fn validate_world_and_update_archetypes(&mut self, world: &World) {
201+
/// Asserts that the [`SystemState`] matches the provided [`World`].
202+
#[inline]
203+
fn validate_world(&self, world: &World) {
200204
assert!(self.matches_world(world), "Encountered a mismatched World. A SystemState cannot be used with Worlds other than the one it was created with.");
205+
}
206+
207+
/// Updates the state's internal view of the `world`'s archetypes. If this is not called before fetching the parameters,
208+
/// the results may not accurately reflect what is in the `world`.
209+
///
210+
/// This is only required if [`SystemState::get_manual`] or [`SystemState::get_manual_mut`] is being called, and it only needs to
211+
/// be called if the `world` has been structurally mutated (i.e. added/removed a component or resource). Users using
212+
/// [`SystemState::get`] or [`SystemState::get_mut`] do not need to call this as it will be automatically called for them.
213+
#[inline]
214+
pub fn update_archetypes(&mut self, world: &World) {
201215
let archetypes = world.archetypes();
202216
let new_generation = archetypes.generation();
203217
let old_generation = std::mem::replace(&mut self.archetype_generation, new_generation);
@@ -212,6 +226,43 @@ impl<Param: SystemParam> SystemState<Param> {
212226
}
213227
}
214228

229+
/// Retrieve the [`SystemParam`] values. This can only be called when all parameters are read-only.
230+
/// This will not update the state's view of the world's archetypes automatically nor increment the
231+
/// world's change tick.
232+
///
233+
/// For this to return accurate results, ensure [`SystemState::update_archetypes`] is called before this
234+
/// function.
235+
///
236+
/// Users should strongly prefer to use [`SystemState::get`] over this function.
237+
#[inline]
238+
pub fn get_manual<'w, 's>(&'s mut self, world: &'w World) -> SystemParamItem<'w, 's, Param>
239+
where
240+
Param: ReadOnlySystemParam,
241+
{
242+
self.validate_world(world);
243+
let change_tick = world.read_change_tick();
244+
// SAFETY: Param is read-only and doesn't allow mutable access to World. It also matches the World this SystemState was created with.
245+
unsafe { self.fetch(world, change_tick) }
246+
}
247+
248+
/// Retrieve the mutable [`SystemParam`] values. This will not update the state's view of the world's archetypes
249+
/// automatically nor increment the world's change tick.
250+
///
251+
/// For this to return accurate results, ensure [`SystemState::update_archetypes`] is called before this
252+
/// function.
253+
///
254+
/// Users should strongly prefer to use [`SystemState::get_mut`] over this function.
255+
#[inline]
256+
pub fn get_manual_mut<'w, 's>(
257+
&'s mut self,
258+
world: &'w mut World,
259+
) -> SystemParamItem<'w, 's, Param> {
260+
self.validate_world(world);
261+
let change_tick = world.change_tick();
262+
// SAFETY: World is uniquely borrowed and matches the World this SystemState was created with.
263+
unsafe { self.fetch(world, change_tick) }
264+
}
265+
215266
/// Retrieve the [`SystemParam`] values. This will not update archetypes automatically.
216267
///
217268
/// # Safety
@@ -224,6 +275,19 @@ impl<Param: SystemParam> SystemState<Param> {
224275
world: &'w World,
225276
) -> SystemParamItem<'w, 's, Param> {
226277
let change_tick = world.increment_change_tick();
278+
self.fetch(world, change_tick)
279+
}
280+
281+
/// # Safety
282+
/// This call might access any of the input parameters in a way that violates Rust's mutability rules. Make sure the data
283+
/// access is safe in the context of global [`World`] access. The passed-in [`World`] _must_ be the [`World`] the [`SystemState`] was
284+
/// created with.
285+
#[inline]
286+
unsafe fn fetch<'w, 's>(
287+
&'s mut self,
288+
world: &'w World,
289+
change_tick: u32,
290+
) -> SystemParamItem<'w, 's, Param> {
227291
let param = Param::get_param(&mut self.param_state, &self.meta, world, change_tick);
228292
self.meta.last_change_tick = change_tick;
229293
param

crates/bevy_render/src/render_phase/draw.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ where
244244
/// Prepares the render command to be used. This is called once and only once before the phase
245245
/// begins. There may be zero or more `draw` calls following a call to this function.
246246
fn prepare(&mut self, world: &'_ World) {
247+
self.state.update_archetypes(world);
247248
self.view.update_archetypes(world);
248249
self.entity.update_archetypes(world);
249250
}
@@ -256,7 +257,7 @@ where
256257
view: Entity,
257258
item: &P,
258259
) {
259-
let param = self.state.get(world);
260+
let param = self.state.get_manual(world);
260261
let view = self.view.get_manual(world, view).unwrap();
261262
let entity = self.entity.get_manual(world, item.entity()).unwrap();
262263
// TODO: handle/log `RenderCommand` failure

0 commit comments

Comments
 (0)