bevy/crates/bevy_ecs/src
Ben Frankel f78856b3bd
Add cached run_system API (#14920)
# Objective

Working with `World` is painful due to lifetime issues and a lack of
ergonomics, so you may want to delegate to the system API. Your current
options are:

- `world.run_system_once`, which initializes the system each time it's
called (performance cost) and doesn't support `Local`. The docs
recommend users not use this method outside of diagnostic use cases like
unit tests.
- `world.run_system`, which requires you to register the system and
store the `SystemId` somewhere (made easier by implementing `FromWorld`
for a newtyped `Local`, unless you're in e.g. a custom `Command` impl).

These options work, but you're choosing between a performance cost and
an ergonomic challenge.

## Solution

Provide a cached `run_system` API that accepts an `S: IntoSystem` and
checks for a `CachedSystemId<S::System>(SystemId)` resource. If it
doesn't exist, it will register the system and save its `SystemId` in
that resource.

In other words, it hides the "save the `SystemId` in a `Local` or
`Resource`" pattern as an implementation detail.

Prior work: https://github.com/bevyengine/bevy/pull/10469.

## Testing

This approach worked in a proof-of-concept:
b34ee29531/src/util/patch/run_system_cached.rs (L35).

A new unit test was added and it passes in CI.
2024-09-23 16:35:29 +00:00
..
entity Choose more descriptive field names for ReserveEntitiesIterator (#15168) 2024-09-20 19:13:35 +00:00
event Rename Add to Queue for methods with deferred semantics (#15234) 2024-09-17 00:17:49 +00:00
identifier Migrated NonZero* to NonZero<*> (#14978) 2024-08-30 02:37:47 +00:00
observer Add World::trigger_ref and World::trigger_targets_ref (#14894) 2024-09-23 16:31:44 +00:00
query Enable clippy::check-private-items so that missing_safety_doc will apply to private functions as well (#15161) 2024-09-18 15:28:41 +00:00
reflect Rename Add to Queue for methods with deferred semantics (#15234) 2024-09-17 00:17:49 +00:00
schedule move ShortName to bevy_reflect (#15340) 2024-09-21 20:52:46 +00:00
storage simplify std::mem references (#15315) 2024-09-19 21:28:16 +00:00
system Add cached run_system API (#14920) 2024-09-23 16:35:29 +00:00
world change return type of World::resource_ref to Ref (#15263) 2024-09-21 19:11:13 +00:00
archetype.rs Add EntityRefExcept and EntityMutExcept world queries, in preparation for generalized animation. (#15207) 2024-09-17 14:53:39 +00:00
batching.rs Parallel event reader (#12554) 2024-04-22 16:37:42 +00:00
bundle.rs Enable clippy::check-private-items so that missing_safety_doc will apply to private functions as well (#15161) 2024-09-18 15:28:41 +00:00
change_detection.rs change return type of World::resource_ref to Ref (#15263) 2024-09-21 19:11:13 +00:00
component.rs Enable clippy::check-private-items so that missing_safety_doc will apply to private functions as well (#15161) 2024-09-18 15:28:41 +00:00
intern.rs Moves intern and label modules into bevy_ecs (#12772) 2024-04-08 15:34:11 +00:00
label.rs Add mappings to EntityMapper (#13727) 2024-06-08 12:52:23 +00:00
lib.rs Remove redundent information and optimize dynamic allocations in Table (#12929) 2024-09-16 22:52:05 +00:00
removal_detection.rs Created an EventMutator for when you want to mutate an event before reading (#13818) 2024-07-08 14:53:06 +00:00
traversal.rs Minimal Bubbling Observers (#13991) 2024-07-15 13:39:41 +00:00