bevy/crates/bevy_ecs/src/system/system_param.rs
Alice Cecile 6121e5f933 Reliable change detection (#1471)
# Problem Definition

The current change tracking (via flags for both components and resources) fails to detect changes made by systems that are scheduled to run earlier in the frame than they are.

This issue is discussed at length in [#68](https://github.com/bevyengine/bevy/issues/68) and [#54](https://github.com/bevyengine/bevy/issues/54).

This is very much a draft PR, and contributions are welcome and needed.

# Criteria
1. Each change is detected at least once, no matter the ordering.
2. Each change is detected at most once, no matter the ordering.
3. Changes should be detected the same frame that they are made.
4. Competitive ergonomics. Ideally does not require opting-in.
5. Low CPU overhead of computation.
6. Memory efficient. This must not increase over time, except where the number of entities / resources does.
7. Changes should not be lost for systems that don't run.
8. A frame needs to act as a pure function. Given the same set of entities / components it needs to produce the same end state without side-effects.

**Exact** change-tracking proposals satisfy criteria 1 and 2.
**Conservative** change-tracking proposals satisfy criteria 1 but not 2.
**Flaky** change tracking proposals satisfy criteria 2 but not 1.

# Code Base Navigation

There are three types of flags: 
- `Added`: A piece of data was added to an entity / `Resources`.
- `Mutated`: A piece of data was able to be modified, because its `DerefMut` was accessed
- `Changed`: The bitwise OR of `Added` and `Changed`

The special behavior of `ChangedRes`, with respect to the scheduler is being removed in [#1313](https://github.com/bevyengine/bevy/pull/1313) and does not need to be reproduced.

`ChangedRes` and friends can be found in "bevy_ecs/core/resources/resource_query.rs".

The `Flags` trait for Components can be found in "bevy_ecs/core/query.rs".

`ComponentFlags` are stored in "bevy_ecs/core/archetypes.rs", defined on line 446.

# Proposals

**Proposal 5 was selected for implementation.**

## Proposal 0: No Change Detection

The baseline, where computations are performed on everything regardless of whether it changed.

**Type:** Conservative

**Pros:**
- already implemented
- will never miss events
- no overhead

**Cons:**
- tons of repeated work
- doesn't allow users to avoid repeating work (or monitoring for other changes)

## Proposal 1: Earlier-This-Tick Change Detection

The current approach as of Bevy 0.4. Flags are set, and then flushed at the end of each frame.

**Type:** Flaky

**Pros:**
- already implemented
- simple to understand
- low memory overhead (2 bits per component)
- low time overhead (clear every flag once per frame)

**Cons:**
- misses systems based on ordering
- systems that don't run every frame miss changes
- duplicates detection when looping
- can lead to unresolvable circular dependencies

## Proposal 2: Two-Tick Change Detection

Flags persist for two frames, using a double-buffer system identical to that used in events.

A change is observed if it is found in either the current frame's list of changes or the previous frame's.

**Type:** Conservative

**Pros:**
- easy to understand
- easy to implement
- low memory overhead (4 bits per component)
- low time overhead (bit mask and shift every flag once per frame)

**Cons:**
- can result in a great deal of duplicated work
- systems that don't run every frame miss changes
- duplicates detection when looping

## Proposal 3: Last-Tick Change Detection

Flags persist for two frames, using a double-buffer system identical to that used in events.

A change is observed if it is found in the previous frame's list of changes.

**Type:** Exact

**Pros:**
- exact
- easy to understand
- easy to implement
- low memory overhead (4 bits per component)
- low time overhead (bit mask and shift every flag once per frame)

**Cons:**
- change detection is always delayed, possibly causing painful chained delays
- systems that don't run every frame miss changes
- duplicates detection when looping

## Proposal 4: Flag-Doubling Change Detection

Combine Proposal 2 and Proposal 3. Differentiate between `JustChanged` (current behavior) and `Changed` (Proposal 3).

Pack this data into the flags according to [this implementation proposal](https://github.com/bevyengine/bevy/issues/68#issuecomment-769174804).

**Type:** Flaky + Exact

**Pros:**
- allows users to acc
- easy to implement
- low memory overhead (4 bits per component)
- low time overhead (bit mask and shift every flag once per frame)

**Cons:**
- users must specify the type of change detection required
- still quite fragile to system ordering effects when using the flaky `JustChanged` form
- cannot get immediate + exact results
- systems that don't run every frame miss changes
- duplicates detection when looping

## [SELECTED] Proposal 5: Generation-Counter Change Detection

A global counter is increased after each system is run. Each component saves the time of last mutation, and each system saves the time of last execution. Mutation is detected when the component's counter is greater than the system's counter. Discussed [here](https://github.com/bevyengine/bevy/issues/68#issuecomment-769174804). How to handle addition detection is unsolved; the current proposal is to use the highest bit of the counter as in proposal 1.

**Type:** Exact (for mutations), flaky (for additions)

**Pros:**
- low time overhead (set component counter on access, set system counter after execution)
- robust to systems that don't run every frame
- robust to systems that loop

**Cons:**
- moderately complex implementation
- must be modified as systems are inserted dynamically
- medium memory overhead (4 bytes per component + system)
- unsolved addition detection

## Proposal 6: System-Data Change Detection

For each system, track which system's changes it has seen. This approach is only worth fully designing and implementing if Proposal 5 fails in some way.  

**Type:** Exact

**Pros:**
- exact
- conceptually simple

**Cons:**
- requires storing data on each system
- implementation is complex
- must be modified as systems are inserted dynamically

## Proposal 7: Total-Order Change Detection

Discussed [here](https://github.com/bevyengine/bevy/issues/68#issuecomment-754326523). This proposal is somewhat complicated by the new scheduler, but I believe it should still be conceptually feasible. This approach is only worth fully designing and implementing if Proposal 5 fails in some way.  

**Type:** Exact

**Pros:**
- exact
- efficient data storage relative to other exact proposals

**Cons:**
- requires access to the scheduler
- complex implementation and difficulty grokking
- must be modified as systems are inserted dynamically

# Tests

- We will need to verify properties 1, 2, 3, 7 and 8. Priority: 1 > 2 = 3 > 8 > 7
- Ideally we can use identical user-facing syntax for all proposals, allowing us to re-use the same syntax for each.
- When writing tests, we need to carefully specify order using explicit dependencies.
- These tests will need to be duplicated for both components and resources.
- We need to be sure to handle cases where ambiguous system orders exist.

`changing_system` is always the system that makes the changes, and `detecting_system` always detects the changes.

The component / resource changed will be simple boolean wrapper structs.

## Basic Added / Mutated / Changed

2 x 3 design:
- Resources vs. Components
- Added vs. Changed vs. Mutated
- `changing_system` runs before `detecting_system`
- verify at the end of tick 2

## At Least Once

2 x 3 design:
- Resources vs. Components
- Added vs. Changed vs. Mutated
- `changing_system` runs after `detecting_system`
- verify at the end of tick 2

## At Most Once

2 x 3 design:
- Resources vs. Components
- Added vs. Changed vs. Mutated
- `changing_system` runs once before `detecting_system`
- increment a counter based on the number of changes detected
- verify at the end of tick 2

## Fast Detection
2 x 3 design:
- Resources vs. Components
- Added vs. Changed vs. Mutated
- `changing_system` runs before `detecting_system`
- verify at the end of tick 1

## Ambiguous System Ordering Robustness
2 x 3 x 2 design:
- Resources vs. Components
- Added vs. Changed vs. Mutated
- `changing_system` runs [before/after] `detecting_system` in tick 1
- `changing_system` runs [after/before] `detecting_system` in tick 2

## System Pausing
2 x 3 design:
- Resources vs. Components
- Added vs. Changed vs. Mutated
- `changing_system` runs in tick 1, then is disabled by run criteria
- `detecting_system` is disabled by run criteria until it is run once during tick 3
- verify at the end of tick 3

## Addition Causes Mutation

2 design:
- Resources vs. Components
- `adding_system_1` adds a component / resource
- `adding system_2` adds the same component / resource
- verify the `Mutated` flag at the end of the tick
- verify the `Added` flag at the end of the tick

First check tests for: https://github.com/bevyengine/bevy/issues/333
Second check tests for: https://github.com/bevyengine/bevy/issues/1443

## Changes Made By Commands

- `adding_system` runs in Update in tick 1, and sends a command to add a component 
- `detecting_system` runs in Update in tick 1 and 2, after `adding_system`
- We can't detect the changes in tick 1, since they haven't been processed yet
- If we were to track these changes as being emitted by `adding_system`, we can't detect the changes in tick 2 either, since `detecting_system` has already run once after `adding_system` :( 

# Benchmarks

See: [general advice](https://github.com/bevyengine/bevy/blob/master/docs/profiling.md), [Criterion crate](https://github.com/bheisler/criterion.rs)

There are several critical parameters to vary: 
1. entity count (1 to 10^9)
2. fraction of entities that are changed (0% to 100%)
3. cost to perform work on changed entities, i.e. workload (1 ns to 1s)

1 and 2 should be varied between benchmark runs. 3 can be added on computationally.

We want to measure:
- memory cost
- run time

We should collect these measurements across several frames (100?) to reduce bootup effects and accurately measure the mean, variance and drift.

Entity-component change detection is much more important to benchmark than resource change detection, due to the orders of magnitude higher number of pieces of data.

No change detection at all should be included in benchmarks as a second control for cases where missing changes is unacceptable.

## Graphs
1. y: performance, x: log_10(entity count), color: proposal, facet: performance metric. Set cost to perform work to 0. 
2. y: run time, x: cost to perform work, color: proposal, facet: fraction changed. Set number of entities to 10^6
3. y: memory, x: frames, color: proposal

# Conclusions
1. Is the theoretical categorization of the proposals correct according to our tests?
2. How does the performance of the proposals compare without any load?
3. How does the performance of the proposals compare with realistic loads?
4. At what workload does more exact change tracking become worth the (presumably) higher overhead?
5. When does adding change-detection to save on work become worthwhile?
6. Is there enough divergence in performance between the best solutions in each class to ship more than one change-tracking solution?

# Implementation Plan

1. Write a test suite.
2. Verify that tests fail for existing approach.
3. Write a benchmark suite.
4. Get performance numbers for existing approach.
5. Implement, test and benchmark various solutions using a Git branch per proposal.
6. Create a draft PR with all solutions and present results to team.
7. Select a solution and replace existing change detection.

Co-authored-by: Brice DAVIER <bricedavier@gmail.com>
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2021-03-19 17:53:26 +00:00

985 lines
29 KiB
Rust

use crate::{
archetype::{Archetype, Archetypes},
bundle::Bundles,
component::{Component, ComponentId, ComponentTicks, Components},
entity::{Entities, Entity},
query::{FilterFetch, FilteredAccess, FilteredAccessSet, QueryState, WorldQuery},
system::{CommandQueue, Commands, Query, SystemState},
world::{FromWorld, World},
};
pub use bevy_ecs_macros::SystemParam;
use bevy_ecs_macros::{all_tuples, impl_query_set};
use std::{
marker::PhantomData,
ops::{Deref, DerefMut},
};
/// A parameter that can be used in a system function
///
/// # Derive
/// This trait can be derived.
///
/// ```
/// # use bevy_ecs::prelude::*;
/// use bevy_ecs::system::SystemParam;
///
/// #[derive(SystemParam)]
/// pub struct MyParam<'a> {
/// foo: Res<'a, usize>,
/// }
///
/// fn my_system(param: MyParam) {
/// // Access the resource through `param.foo`
/// }
/// ```
pub trait SystemParam: Sized {
type Fetch: for<'a> SystemParamFetch<'a>;
}
/// # Safety
/// It is the implementor's responsibility to ensure `system_state` is populated with the _exact_
/// [World] access used by the SystemParamState (and associated FetchSystemParam).
/// Additionally, it is the implementor's responsibility to ensure there is no
/// conflicting access across all SystemParams.
pub unsafe trait SystemParamState: Send + Sync + 'static {
type Config: Default + Send + Sync;
fn init(world: &mut World, system_state: &mut SystemState, config: Self::Config) -> Self;
#[inline]
fn new_archetype(&mut self, _archetype: &Archetype, _system_state: &mut SystemState) {}
#[inline]
fn apply(&mut self, _world: &mut World) {}
}
pub trait SystemParamFetch<'a>: SystemParamState {
type Item;
/// # Safety
/// This call might access any of the input parameters in an unsafe way. Make sure the data
/// access is safe in the context of the system scheduler
unsafe fn get_param(
state: &'a mut Self,
system_state: &'a SystemState,
world: &'a World,
change_tick: u32,
) -> Self::Item;
}
pub struct QueryFetch<Q, F>(PhantomData<(Q, F)>);
impl<'a, Q: WorldQuery + 'static, F: WorldQuery + 'static> SystemParam for Query<'a, Q, F>
where
F::Fetch: FilterFetch,
{
type Fetch = QueryState<Q, F>;
}
// SAFE: Relevant query ComponentId and ArchetypeComponentId access is applied to SystemState. If
// this QueryState conflicts with any prior access, a panic will occur.
unsafe impl<Q: WorldQuery + 'static, F: WorldQuery + 'static> SystemParamState for QueryState<Q, F>
where
F::Fetch: FilterFetch,
{
type Config = ();
fn init(world: &mut World, system_state: &mut SystemState, _config: Self::Config) -> Self {
let state = QueryState::new(world);
assert_component_access_compatibility(
&system_state.name,
std::any::type_name::<Q>(),
std::any::type_name::<F>(),
&system_state.component_access_set,
&state.component_access,
world,
);
system_state
.component_access_set
.add(state.component_access.clone());
system_state
.archetype_component_access
.extend(&state.archetype_component_access);
state
}
fn new_archetype(&mut self, archetype: &Archetype, system_state: &mut SystemState) {
self.new_archetype(archetype);
system_state
.archetype_component_access
.extend(&self.archetype_component_access);
}
}
impl<'a, Q: WorldQuery + 'static, F: WorldQuery + 'static> SystemParamFetch<'a> for QueryState<Q, F>
where
F::Fetch: FilterFetch,
{
type Item = Query<'a, Q, F>;
#[inline]
unsafe fn get_param(
state: &'a mut Self,
system_state: &'a SystemState,
world: &'a World,
change_tick: u32,
) -> Self::Item {
Query::new(world, state, system_state.last_change_tick, change_tick)
}
}
fn assert_component_access_compatibility(
system_name: &str,
query_type: &'static str,
filter_type: &'static str,
system_access: &FilteredAccessSet<ComponentId>,
current: &FilteredAccess<ComponentId>,
world: &World,
) {
let mut conflicts = system_access.get_conflicts(current);
if conflicts.is_empty() {
return;
}
let conflicting_components = conflicts
.drain(..)
.map(|component_id| world.components.get_info(component_id).unwrap().name())
.collect::<Vec<&str>>();
let accesses = conflicting_components.join(", ");
panic!("Query<{}, {}> in system {} accesses component(s) {} in a way that conflicts with a previous system parameter. Allowing this would break Rust's mutability rules. Consider merging conflicting Queries into a QuerySet.",
query_type, filter_type, system_name, accesses);
}
pub struct QuerySet<T>(T);
pub struct QuerySetState<T>(T);
impl_query_set!();
/// Shared borrow of a Resource
///
/// When used as a system parameter, panics if resource does not exist.
///
/// Use `Option<Res<T>>` if the resource might not always exist.
pub struct Res<'w, T> {
value: &'w T,
ticks: &'w ComponentTicks,
last_change_tick: u32,
change_tick: u32,
}
impl<'w, T: Component> Res<'w, T> {
/// Returns true if (and only if) this resource been added since the last execution of this
/// system.
pub fn is_added(&self) -> bool {
self.ticks.is_added(self.last_change_tick, self.change_tick)
}
/// Returns true if (and only if) this resource been changed since the last execution of this
/// system.
pub fn is_changed(&self) -> bool {
self.ticks
.is_changed(self.last_change_tick, self.change_tick)
}
}
impl<'w, T: Component> Deref for Res<'w, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.value
}
}
pub struct ResState<T> {
component_id: ComponentId,
marker: PhantomData<T>,
}
impl<'a, T: Component> SystemParam for Res<'a, T> {
type Fetch = ResState<T>;
}
// SAFE: Res ComponentId and ArchetypeComponentId access is applied to SystemState. If this Res
// conflicts with any prior access, a panic will occur.
unsafe impl<T: Component> SystemParamState for ResState<T> {
type Config = ();
fn init(world: &mut World, system_state: &mut SystemState, _config: Self::Config) -> Self {
let component_id = world.initialize_resource::<T>();
let combined_access = system_state.component_access_set.combined_access_mut();
if combined_access.has_write(component_id) {
panic!(
"Res<{}> in system {} conflicts with a previous ResMut<{0}> access. Allowing this would break Rust's mutability rules. Consider removing the duplicate access.",
std::any::type_name::<T>(), system_state.name);
}
combined_access.add_read(component_id);
let resource_archetype = world.archetypes.resource();
let archetype_component_id = resource_archetype
.get_archetype_component_id(component_id)
.unwrap();
system_state
.archetype_component_access
.add_read(archetype_component_id);
Self {
component_id,
marker: PhantomData,
}
}
}
impl<'a, T: Component> SystemParamFetch<'a> for ResState<T> {
type Item = Res<'a, T>;
#[inline]
unsafe fn get_param(
state: &'a mut Self,
system_state: &'a SystemState,
world: &'a World,
change_tick: u32,
) -> Self::Item {
let column = world
.get_populated_resource_column(state.component_id)
.unwrap_or_else(|| {
panic!(
"Requested resource does not exist: {}",
std::any::type_name::<T>()
)
});
Res {
value: &*column.get_ptr().as_ptr().cast::<T>(),
ticks: &*column.get_ticks_mut_ptr(),
last_change_tick: system_state.last_change_tick,
change_tick,
}
}
}
pub struct OptionResState<T>(ResState<T>);
impl<'a, T: Component> SystemParam for Option<Res<'a, T>> {
type Fetch = OptionResState<T>;
}
unsafe impl<T: Component> SystemParamState for OptionResState<T> {
type Config = ();
fn init(world: &mut World, system_state: &mut SystemState, _config: Self::Config) -> Self {
Self(ResState::init(world, system_state, ()))
}
}
impl<'a, T: Component> SystemParamFetch<'a> for OptionResState<T> {
type Item = Option<Res<'a, T>>;
#[inline]
unsafe fn get_param(
state: &'a mut Self,
system_state: &'a SystemState,
world: &'a World,
change_tick: u32,
) -> Self::Item {
world
.get_populated_resource_column(state.0.component_id)
.map(|column| Res {
value: &*column.get_ptr().as_ptr().cast::<T>(),
ticks: &*column.get_ticks_mut_ptr(),
last_change_tick: system_state.last_change_tick,
change_tick,
})
}
}
/// Unique borrow of a Resource
///
/// When used as a system parameter, panics if resource does not exist.
///
/// Use `Option<ResMut<T>>` if the resource might not always exist.
pub struct ResMut<'w, T> {
value: &'w mut T,
ticks: &'w mut ComponentTicks,
last_change_tick: u32,
change_tick: u32,
}
impl<'w, T: Component> ResMut<'w, T> {
/// Returns true if (and only if) this resource been added since the last execution of this
/// system.
pub fn is_added(&self) -> bool {
self.ticks.is_added(self.last_change_tick, self.change_tick)
}
/// Returns true if (and only if) this resource been changed since the last execution of this
/// system.
pub fn is_changed(&self) -> bool {
self.ticks
.is_changed(self.last_change_tick, self.change_tick)
}
}
impl<'w, T: Component> Deref for ResMut<'w, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.value
}
}
impl<'w, T: Component> DerefMut for ResMut<'w, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.ticks.set_changed(self.change_tick);
self.value
}
}
pub struct ResMutState<T> {
component_id: ComponentId,
marker: PhantomData<T>,
}
impl<'a, T: Component> SystemParam for ResMut<'a, T> {
type Fetch = ResMutState<T>;
}
// SAFE: Res ComponentId and ArchetypeComponentId access is applied to SystemState. If this Res
// conflicts with any prior access, a panic will occur.
unsafe impl<T: Component> SystemParamState for ResMutState<T> {
type Config = ();
fn init(world: &mut World, system_state: &mut SystemState, _config: Self::Config) -> Self {
let component_id = world.initialize_resource::<T>();
let combined_access = system_state.component_access_set.combined_access_mut();
if combined_access.has_write(component_id) {
panic!(
"ResMut<{}> in system {} conflicts with a previous ResMut<{0}> access. Allowing this would break Rust's mutability rules. Consider removing the duplicate access.",
std::any::type_name::<T>(), system_state.name);
} else if combined_access.has_read(component_id) {
panic!(
"ResMut<{}> in system {} conflicts with a previous Res<{0}> access. Allowing this would break Rust's mutability rules. Consider removing the duplicate access.",
std::any::type_name::<T>(), system_state.name);
}
combined_access.add_write(component_id);
let resource_archetype = world.archetypes.resource();
let archetype_component_id = resource_archetype
.get_archetype_component_id(component_id)
.unwrap();
system_state
.archetype_component_access
.add_write(archetype_component_id);
Self {
component_id,
marker: PhantomData,
}
}
}
impl<'a, T: Component> SystemParamFetch<'a> for ResMutState<T> {
type Item = ResMut<'a, T>;
#[inline]
unsafe fn get_param(
state: &'a mut Self,
system_state: &'a SystemState,
world: &'a World,
change_tick: u32,
) -> Self::Item {
let value = world
.get_resource_unchecked_mut_with_id(state.component_id)
.unwrap_or_else(|| {
panic!(
"Requested resource does not exist: {}",
std::any::type_name::<T>()
)
});
ResMut {
value: value.value,
ticks: value.component_ticks,
last_change_tick: system_state.last_change_tick,
change_tick,
}
}
}
pub struct OptionResMutState<T>(ResMutState<T>);
impl<'a, T: Component> SystemParam for Option<ResMut<'a, T>> {
type Fetch = OptionResMutState<T>;
}
unsafe impl<T: Component> SystemParamState for OptionResMutState<T> {
type Config = ();
fn init(world: &mut World, system_state: &mut SystemState, _config: Self::Config) -> Self {
Self(ResMutState::init(world, system_state, ()))
}
}
impl<'a, T: Component> SystemParamFetch<'a> for OptionResMutState<T> {
type Item = Option<ResMut<'a, T>>;
#[inline]
unsafe fn get_param(
state: &'a mut Self,
system_state: &'a SystemState,
world: &'a World,
change_tick: u32,
) -> Self::Item {
world
.get_resource_unchecked_mut_with_id(state.0.component_id)
.map(|value| ResMut {
value: value.value,
ticks: value.component_ticks,
last_change_tick: system_state.last_change_tick,
change_tick,
})
}
}
impl<'a> SystemParam for Commands<'a> {
type Fetch = CommandQueue;
}
// SAFE: only local state is accessed
unsafe impl SystemParamState for CommandQueue {
type Config = ();
fn init(_world: &mut World, _system_state: &mut SystemState, _config: Self::Config) -> Self {
Default::default()
}
fn apply(&mut self, world: &mut World) {
self.apply(world);
}
}
impl<'a> SystemParamFetch<'a> for CommandQueue {
type Item = Commands<'a>;
#[inline]
unsafe fn get_param(
state: &'a mut Self,
_system_state: &'a SystemState,
world: &'a World,
_change_tick: u32,
) -> Self::Item {
Commands::new(state, world)
}
}
pub struct Local<'a, T: Component>(&'a mut T);
impl<'a, T: Component> Deref for Local<'a, T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
self.0
}
}
impl<'a, T: Component> DerefMut for Local<'a, T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
self.0
}
}
pub struct LocalState<T: Component>(T);
impl<'a, T: Component + FromWorld> SystemParam for Local<'a, T> {
type Fetch = LocalState<T>;
}
// SAFE: only local state is accessed
unsafe impl<T: Component + FromWorld> SystemParamState for LocalState<T> {
type Config = Option<T>;
fn init(world: &mut World, _system_state: &mut SystemState, config: Self::Config) -> Self {
Self(config.unwrap_or_else(|| T::from_world(world)))
}
}
impl<'a, T: Component + FromWorld> SystemParamFetch<'a> for LocalState<T> {
type Item = Local<'a, T>;
#[inline]
unsafe fn get_param(
state: &'a mut Self,
_system_state: &'a SystemState,
_world: &'a World,
_change_tick: u32,
) -> Self::Item {
Local(&mut state.0)
}
}
pub struct RemovedComponents<'a, T> {
world: &'a World,
component_id: ComponentId,
marker: PhantomData<T>,
}
impl<'a, T> RemovedComponents<'a, T> {
pub fn iter(&self) -> std::iter::Cloned<std::slice::Iter<'_, Entity>> {
self.world.removed_with_id(self.component_id)
}
}
pub struct RemovedComponentsState<T> {
component_id: ComponentId,
marker: PhantomData<T>,
}
impl<'a, T: Component> SystemParam for RemovedComponents<'a, T> {
type Fetch = RemovedComponentsState<T>;
}
// SAFE: no component access. removed component entity collections can be read in parallel and are
// never mutably borrowed during system execution
unsafe impl<T: Component> SystemParamState for RemovedComponentsState<T> {
type Config = ();
fn init(world: &mut World, _system_state: &mut SystemState, _config: Self::Config) -> Self {
Self {
component_id: world.components.get_or_insert_id::<T>(),
marker: PhantomData,
}
}
}
impl<'a, T: Component> SystemParamFetch<'a> for RemovedComponentsState<T> {
type Item = RemovedComponents<'a, T>;
#[inline]
unsafe fn get_param(
state: &'a mut Self,
_system_state: &'a SystemState,
world: &'a World,
_change_tick: u32,
) -> Self::Item {
RemovedComponents {
world,
component_id: state.component_id,
marker: PhantomData,
}
}
}
/// Shared borrow of a NonSend resource
pub struct NonSend<'w, T> {
pub(crate) value: &'w T,
ticks: ComponentTicks,
last_change_tick: u32,
change_tick: u32,
}
impl<'w, T: Component> NonSend<'w, T> {
/// Returns true if (and only if) this resource been added since the last execution of this
/// system.
pub fn is_added(&self) -> bool {
self.ticks.is_added(self.last_change_tick, self.change_tick)
}
/// Returns true if (and only if) this resource been changed since the last execution of this
/// system.
pub fn is_changed(&self) -> bool {
self.ticks
.is_changed(self.last_change_tick, self.change_tick)
}
}
impl<'w, T: 'static> Deref for NonSend<'w, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.value
}
}
pub struct NonSendState<T> {
component_id: ComponentId,
marker: PhantomData<fn() -> T>,
}
impl<'a, T: 'static> SystemParam for NonSend<'a, T> {
type Fetch = NonSendState<T>;
}
// SAFE: NonSendComponentId and ArchetypeComponentId access is applied to SystemState. If this
// NonSend conflicts with any prior access, a panic will occur.
unsafe impl<T: 'static> SystemParamState for NonSendState<T> {
type Config = ();
fn init(world: &mut World, system_state: &mut SystemState, _config: Self::Config) -> Self {
system_state.set_non_send();
let component_id = world.initialize_non_send_resource::<T>();
let combined_access = system_state.component_access_set.combined_access_mut();
if combined_access.has_write(component_id) {
panic!(
"NonSend<{}> in system {} conflicts with a previous mutable resource access ({0}). Allowing this would break Rust's mutability rules. Consider removing the duplicate access.",
std::any::type_name::<T>(), system_state.name);
}
combined_access.add_read(component_id);
let resource_archetype = world.archetypes.resource();
let archetype_component_id = resource_archetype
.get_archetype_component_id(component_id)
.unwrap();
system_state
.archetype_component_access
.add_read(archetype_component_id);
Self {
component_id,
marker: PhantomData,
}
}
}
impl<'a, T: 'static> SystemParamFetch<'a> for NonSendState<T> {
type Item = NonSend<'a, T>;
#[inline]
unsafe fn get_param(
state: &'a mut Self,
system_state: &'a SystemState,
world: &'a World,
change_tick: u32,
) -> Self::Item {
world.validate_non_send_access::<T>();
let column = world
.get_populated_resource_column(state.component_id)
.unwrap_or_else(|| {
panic!(
"Requested non-send resource does not exist: {}",
std::any::type_name::<T>()
)
});
NonSend {
value: &*column.get_ptr().as_ptr().cast::<T>(),
ticks: *column.get_ticks_mut_ptr(),
last_change_tick: system_state.last_change_tick,
change_tick,
}
}
}
/// Unique borrow of a NonSend resource
pub struct NonSendMut<'a, T: 'static> {
pub(crate) value: &'a mut T,
ticks: &'a mut ComponentTicks,
last_change_tick: u32,
change_tick: u32,
}
impl<'w, T: Component> NonSendMut<'w, T> {
/// Returns true if (and only if) this resource been added since the last execution of this
/// system.
pub fn is_added(&self) -> bool {
self.ticks.is_added(self.last_change_tick, self.change_tick)
}
/// Returns true if (and only if) this resource been changed since the last execution of this
/// system.
pub fn is_changed(&self) -> bool {
self.ticks
.is_changed(self.last_change_tick, self.change_tick)
}
}
impl<'a, T: 'static> Deref for NonSendMut<'a, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
self.value
}
}
impl<'a, T: 'static> DerefMut for NonSendMut<'a, T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
self.ticks.set_changed(self.change_tick);
self.value
}
}
impl<'a, T: 'static + core::fmt::Debug> core::fmt::Debug for NonSendMut<'a, T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.value.fmt(f)
}
}
pub struct NonSendMutState<T> {
component_id: ComponentId,
marker: PhantomData<fn() -> T>,
}
impl<'a, T: 'static> SystemParam for NonSendMut<'a, T> {
type Fetch = NonSendMutState<T>;
}
// SAFE: NonSendMut ComponentId and ArchetypeComponentId access is applied to SystemState. If this
// NonSendMut conflicts with any prior access, a panic will occur.
unsafe impl<T: 'static> SystemParamState for NonSendMutState<T> {
type Config = ();
fn init(world: &mut World, system_state: &mut SystemState, _config: Self::Config) -> Self {
system_state.set_non_send();
let component_id = world.components.get_or_insert_non_send_resource_id::<T>();
let combined_access = system_state.component_access_set.combined_access_mut();
if combined_access.has_write(component_id) {
panic!(
"NonSendMut<{}> in system {} conflicts with a previous mutable resource access ({0}). Allowing this would break Rust's mutability rules. Consider removing the duplicate access.",
std::any::type_name::<T>(), system_state.name);
} else if combined_access.has_read(component_id) {
panic!(
"NonSendMut<{}> in system {} conflicts with a previous immutable resource access ({0}). Allowing this would break Rust's mutability rules. Consider removing the duplicate access.",
std::any::type_name::<T>(), system_state.name);
}
combined_access.add_write(component_id);
let resource_archetype = world.archetypes.resource();
let archetype_component_id = resource_archetype
.get_archetype_component_id(component_id)
.unwrap();
system_state
.archetype_component_access
.add_write(archetype_component_id);
Self {
component_id,
marker: PhantomData,
}
}
}
impl<'a, T: 'static> SystemParamFetch<'a> for NonSendMutState<T> {
type Item = NonSendMut<'a, T>;
#[inline]
unsafe fn get_param(
state: &'a mut Self,
system_state: &'a SystemState,
world: &'a World,
change_tick: u32,
) -> Self::Item {
world.validate_non_send_access::<T>();
let column = world
.get_populated_resource_column(state.component_id)
.unwrap_or_else(|| {
panic!(
"Requested non-send resource does not exist: {}",
std::any::type_name::<T>()
)
});
NonSendMut {
value: &mut *column.get_ptr().as_ptr().cast::<T>(),
ticks: &mut *column.get_ticks_mut_ptr(),
last_change_tick: system_state.last_change_tick,
change_tick,
}
}
}
pub struct OrState<T>(T);
impl<'a> SystemParam for &'a Archetypes {
type Fetch = ArchetypesState;
}
pub struct ArchetypesState;
// SAFE: no component value access
unsafe impl SystemParamState for ArchetypesState {
type Config = ();
fn init(_world: &mut World, _system_state: &mut SystemState, _config: Self::Config) -> Self {
Self
}
}
impl<'a> SystemParamFetch<'a> for ArchetypesState {
type Item = &'a Archetypes;
#[inline]
unsafe fn get_param(
_state: &'a mut Self,
_system_state: &'a SystemState,
world: &'a World,
_change_tick: u32,
) -> Self::Item {
world.archetypes()
}
}
impl<'a> SystemParam for &'a Components {
type Fetch = ComponentsState;
}
pub struct ComponentsState;
// SAFE: no component value access
unsafe impl SystemParamState for ComponentsState {
type Config = ();
fn init(_world: &mut World, _system_state: &mut SystemState, _config: Self::Config) -> Self {
Self
}
}
impl<'a> SystemParamFetch<'a> for ComponentsState {
type Item = &'a Components;
#[inline]
unsafe fn get_param(
_state: &'a mut Self,
_system_state: &'a SystemState,
world: &'a World,
_change_tick: u32,
) -> Self::Item {
world.components()
}
}
impl<'a> SystemParam for &'a Entities {
type Fetch = EntitiesState;
}
pub struct EntitiesState;
// SAFE: no component value access
unsafe impl SystemParamState for EntitiesState {
type Config = ();
fn init(_world: &mut World, _system_state: &mut SystemState, _config: Self::Config) -> Self {
Self
}
}
impl<'a> SystemParamFetch<'a> for EntitiesState {
type Item = &'a Entities;
#[inline]
unsafe fn get_param(
_state: &'a mut Self,
_system_state: &'a SystemState,
world: &'a World,
_change_tick: u32,
) -> Self::Item {
world.entities()
}
}
impl<'a> SystemParam for &'a Bundles {
type Fetch = BundlesState;
}
pub struct BundlesState;
// SAFE: no component value access
unsafe impl SystemParamState for BundlesState {
type Config = ();
fn init(_world: &mut World, _system_state: &mut SystemState, _config: Self::Config) -> Self {
Self
}
}
impl<'a> SystemParamFetch<'a> for BundlesState {
type Item = &'a Bundles;
#[inline]
unsafe fn get_param(
_state: &'a mut Self,
_system_state: &'a SystemState,
world: &'a World,
_change_tick: u32,
) -> Self::Item {
world.bundles()
}
}
#[derive(Debug)]
pub struct SystemChangeTick {
pub last_change_tick: u32,
pub change_tick: u32,
}
impl SystemParam for SystemChangeTick {
type Fetch = SystemChangeTickState;
}
pub struct SystemChangeTickState {}
unsafe impl SystemParamState for SystemChangeTickState {
type Config = ();
fn init(_world: &mut World, _system_state: &mut SystemState, _config: Self::Config) -> Self {
Self {}
}
}
impl<'a> SystemParamFetch<'a> for SystemChangeTickState {
type Item = SystemChangeTick;
unsafe fn get_param(
_state: &mut Self,
system_state: &SystemState,
_world: &World,
change_tick: u32,
) -> Self::Item {
SystemChangeTick {
last_change_tick: system_state.last_change_tick,
change_tick,
}
}
}
macro_rules! impl_system_param_tuple {
($($param: ident),*) => {
impl<$($param: SystemParam),*> SystemParam for ($($param,)*) {
type Fetch = ($($param::Fetch,)*);
}
#[allow(unused_variables)]
#[allow(non_snake_case)]
impl<'a, $($param: SystemParamFetch<'a>),*> SystemParamFetch<'a> for ($($param,)*) {
type Item = ($($param::Item,)*);
#[inline]
unsafe fn get_param(
state: &'a mut Self,
system_state: &'a SystemState,
world: &'a World,
change_tick: u32,
) -> Self::Item {
let ($($param,)*) = state;
($($param::get_param($param, system_state, world, change_tick),)*)
}
}
/// SAFE: implementors of each SystemParamState in the tuple have validated their impls
#[allow(non_snake_case)]
unsafe impl<$($param: SystemParamState),*> SystemParamState for ($($param,)*) {
type Config = ($(<$param as SystemParamState>::Config,)*);
#[inline]
fn init(_world: &mut World, _system_state: &mut SystemState, config: Self::Config) -> Self {
let ($($param,)*) = config;
(($($param::init(_world, _system_state, $param),)*))
}
#[inline]
fn new_archetype(&mut self, _archetype: &Archetype, _system_state: &mut SystemState) {
let ($($param,)*) = self;
$($param.new_archetype(_archetype, _system_state);)*
}
#[inline]
fn apply(&mut self, _world: &mut World) {
let ($($param,)*) = self;
$($param.apply(_world);)*
}
}
};
}
// TODO: consider creating a Config trait with a default() function, then implementing that for
// tuples. that would allow us to go past tuples of len 12
all_tuples!(impl_system_param_tuple, 0, 12, P);