bevy/crates/bevy_ecs/src
James Liu ae9775c83b
Optimize Event Updates (#12936)
# Objective
Improve performance scalability when adding new event types to a Bevy
app. Currently, just using Bevy in the default configuration, all apps
spend upwards of 100+us in the `First` schedule, every app tick,
evaluating if it should update events or not, even if events are not
being used for that particular frame, and this scales with the number of
Events registered in the app.

## Solution
As `Events::update` is guaranteed `O(1)` by just checking if a
resource's value, swapping two Vecs, and then clearing one of them, the
actual cost of running `event_update_system` is *very* cheap. The
overhead of doing system dependency injection, task scheduling ,and the
multithreaded executor outweighs the cost of running the system by a
large margin.

Create an `EventRegistry` resource that keeps a number of function
pointers that update each event. Replace the per-event type
`event_update_system` with a singular exclusive system uses the
`EventRegistry` to update all events instead. Update `SubApp::add_event`
to use `EventRegistry` instead.

## Performance
This speeds reduces the cost of the `First` schedule in both many_foxes
and many_cubes by over 80%. Note this is with system spans on. The
majority of this is now context-switching costs from launching
`time_system`, which should be mostly eliminated with #12869.

![image](https://github.com/bevyengine/bevy/assets/3137680/037624be-21a2-4dc2-a42f-9d0bfa3e9b4a)

The actual `event_update_system` is usually *very* short, using only a
few microseconds on average.

![image](https://github.com/bevyengine/bevy/assets/3137680/01ff1689-3595-49b6-8f09-5c44bcf903e8)

---

## Changelog
TODO

## Migration Guide
TODO

---------

Co-authored-by: Josh Matthews <josh@joshmatthews.net>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
2024-04-13 14:11:28 +00:00
..
entity Clean up some low level dependencies (#12858) 2024-04-08 19:45:42 +00:00
identifier fix some typos (#12038) 2024-02-22 18:55:22 +00:00
query Add archetypal to Access Debug impl (#12947) 2024-04-13 06:06:48 +00:00
reflect Make some ReflectComponent/ReflectBundle methods work with EntityMut too (#12895) 2024-04-08 01:46:07 +00:00
schedule Optimize Event Updates (#12936) 2024-04-13 14:11:28 +00:00
storage Document the lifetime requirement of byte_offset and byte_add (#12893) 2024-04-08 17:13:35 +00:00
system Add EntityWorldMut::remove_by_id (#12842) 2024-04-03 09:50:32 +00:00
world Implement From<&'w mut EntityMut> for EntityMut<'w> (#12896) 2024-04-07 13:24:10 +00:00
archetype.rs Adding some docs for archetype internals (#12578) 2024-03-23 01:48:31 +00:00
bundle.rs Update safety comment for bundle removal (#12657) 2024-03-23 22:07:08 +00:00
change_detection.rs Optimize Event Updates (#12936) 2024-04-13 14:11:28 +00:00
component.rs Optimize Event Updates (#12936) 2024-04-13 14:11:28 +00:00
event.rs Optimize Event Updates (#12936) 2024-04-13 14:11:28 +00:00
intern.rs Moves intern and label modules into bevy_ecs (#12772) 2024-04-08 15:34:11 +00:00
label.rs Moves intern and label modules into bevy_ecs (#12772) 2024-04-08 15:34:11 +00:00
lib.rs Moves intern and label modules into bevy_ecs (#12772) 2024-04-08 15:34:11 +00:00
removal_detection.rs Add RemovedComponentEvents::iter (#12815) 2024-04-01 20:20:30 +00:00