Skip to content

Commit 53c5b98

Browse files
Discuss caching, provide almost-working example
1 parent 2224d60 commit 53c5b98

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

crates/bevy_ecs/src/system/function_system.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,22 @@ impl SystemMeta {
6565
///
6666
/// For an alternative approach to split mutable access to the world, see [`World::resource_scope`].
6767
///
68+
/// # Warning
69+
/// This API has more performance overhead than more direct methods: prefer them when possible.
70+
///
71+
/// [`SystemState`] values created can be cached to improve performance,
72+
/// and *must* be cached and reused in order for system parameters that rely on local state to work correctly.
73+
/// These include:
74+
/// - [`Added`](crate::system::query::Added) and [`Changed`](crate::system::query::Changed) query filters
75+
/// - [`Local`](crate::system::Local) variables that hold state
76+
/// - [`EventReader`](crate::event::EventReader) system parameters, which rely on a [`Local`](crate::system::Local) to track which events have been seen
77+
///
6878
/// # Example
79+
///
80+
/// Basic usage:
6981
/// ```rust
70-
/// use bevy::ecs::system::SystemState;
71-
/// use bevy::ecs::world::World;
82+
/// use bevy_ecs::prelude::*;
83+
/// use bevy_ecs::{system::SystemState};
7284
///
7385
/// struct MyEvent;
7486
/// struct MyResource(u32);
@@ -81,16 +93,36 @@ impl SystemMeta {
8193
///
8294
/// // Construct a `SystemState` struct, passing in a tuple of `SystemParam`
8395
/// // as if you were writing an ordinary system.
84-
/// // This can be cached and reused for improved performance.
8596
/// let mut system_state: SystemState<(
8697
/// EventWriter<MyEvent>,
8798
/// Option<ResMut<MyResource>>,
8899
/// Query<&MyComponent>,
89100
/// )> = SystemState::new(&mut world);
90101
///
91102
/// // Use system_state.get_mut(&mut world) and unpack your system parameters into variables!
92-
/// // You can use system_state.get(&world) instead for read-only versions of your system parameters
93-
/// let (event_writer, maybe_respource, query) = system_state.get_mut(&mut world);
103+
/// // system_state.get(&world) provides read-only versions of your system parameters instead.
104+
/// let (event_writer, maybe_resource, query) = system_state.get_mut(&mut world);
105+
/// ```
106+
/// Caching:
107+
/// ```rust
108+
/// use bevy_ecs::prelude::*;
109+
/// use bevy_ecs::{system::SystemState};
110+
///
111+
/// struct MyEvent;
112+
///
113+
/// // Create and store a system state once
114+
/// let mut world = World::new();
115+
/// let initial_state: SystemState<EventReader<MyEvent>> = SystemState::new(&mut world);
116+
/// // The system state is cached directly as a resource
117+
/// world.insert_resource(initial_state);
118+
///
119+
/// // Later, fetch the cached system state, saving on overhead
120+
/// let cached_state = world.get_resource_mut::<SystemState<EventReader<MyEvent>>>().unwrap();
121+
/// let mut event_reader = cached_state.get_mut(&mut world);
122+
///
123+
/// for events in event_reader.iter(){
124+
/// // Handle events!
125+
/// };
94126
/// ```
95127
pub struct SystemState<Param: SystemParam> {
96128
meta: SystemMeta,

0 commit comments

Comments
 (0)