Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename apply_system_buffers to apply_deferred #8726

Merged
merged 10 commits into from
Jun 2, 2023
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ current changes on git with [previous release tags][git_tag_comparison].
- [ECS: Add a missing impl of `ReadOnlySystemParam` for `Option<NonSend<>>`][7245]
- [ECS: add a spawn_on_external method to allow spawning on the scope’s thread or an external thread][7415]
- [ECS: Add const `Entity::PLACEHOLDER`][6761]
- [ECS: Add example to show how to use `apply_system_buffers`][7793]
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
- [ECS: Add example to show how to use `apply_deferred`][7793]
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
- [ECS: Add logging variants of system piping][6751]
- [ECS: Add safe constructors for untyped pointers `Ptr` and `PtrMut`][6539]
- [ECS: Add unit test with system that panics][7491]
Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1043,12 +1043,12 @@ category = "ECS (Entity Component System)"
wasm = false

[[example]]
name = "apply_system_buffers"
path = "examples/ecs/apply_system_buffers.rs"
name = "apply_deferred"
path = "examples/ecs/apply_deferred.rs"

[package.metadata.example.apply_system_buffers]
[package.metadata.example.apply_deferred]
name = "Apply System Buffers"
description = "Show how to use `apply_system_buffers` system"
description = "Show how to use `apply_deferred` system"
category = "ECS (Entity Component System)"
wasm = false

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ impl<'a, 'w, 's, E: Event> IntoIterator for &'a mut EventReader<'w, 's, E> {
/// // custom events to unknown 3rd party plugins (modding API).
/// //
/// // NOTE: the event won't actually be sent until commands get applied during
/// // apply_system_buffers.
/// // apply_deferred.
/// commands.add(|w: &mut World| {
/// w.send_event(MyEvent);
/// });
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub mod prelude {
query::{Added, AnyOf, Changed, Or, QueryState, With, Without},
removal_detection::RemovedComponents,
schedule::{
apply_state_transition, apply_system_buffers, common_conditions::*, Condition,
apply_deferred, apply_state_transition, common_conditions::*, Condition,
IntoSystemConfigs, IntoSystemSet, IntoSystemSetConfig, IntoSystemSetConfigs, NextState,
OnEnter, OnExit, OnTransition, Schedule, Schedules, State, States, SystemSet,
},
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/schedule/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,8 +1000,8 @@ where
!self.condition.run(input, world)
}

fn apply_buffers(&mut self, world: &mut World) {
self.condition.apply_buffers(world);
fn apply_deferred(&mut self, world: &mut World) {
self.condition.apply_deferred(world);
}

fn initialize(&mut self, world: &mut World) {
Expand Down
16 changes: 8 additions & 8 deletions crates/bevy_ecs/src/schedule/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(super) trait SystemExecutor: Send + Sync {
fn kind(&self) -> ExecutorKind;
fn init(&mut self, schedule: &SystemSchedule);
fn run(&mut self, schedule: &mut SystemSchedule, world: &mut World);
fn set_apply_final_buffers(&mut self, value: bool);
fn set_apply_final_deferred(&mut self, value: bool);
}

/// Specifies how a [`Schedule`](super::Schedule) will be run.
Expand All @@ -35,7 +35,7 @@ pub enum ExecutorKind {
/// other things, or just trying minimize overhead.
#[cfg_attr(target_arch = "wasm32", default)]
SingleThreaded,
/// Like [`SingleThreaded`](ExecutorKind::SingleThreaded) but calls [`apply_buffers`](crate::system::System::apply_buffers)
/// Like [`SingleThreaded`](ExecutorKind::SingleThreaded) but calls [`apply_deferred`](crate::system::System::apply_deferred)
/// immediately after running each system.
Simple,
/// Runs the schedule using a thread pool. Non-conflicting systems can run in parallel.
Expand Down Expand Up @@ -77,18 +77,18 @@ impl SystemSchedule {
}
}

/// Instructs the executor to call [`apply_buffers`](crate::system::System::apply_buffers)
/// on the systems that have run but not applied their buffers.
/// Instructs the executor to call [`System::apply_deferred`](crate::system::System::apply_deferred)
/// on the systems that have run but not applied their [`Deferred`](crate::system::Deferred) system parameters (like [`Commands`](crate::prelude::Commands)) or other system buffers.
///
/// **Notes**
/// - This function (currently) does nothing if it's called manually or wrapped inside a [`PipeSystem`](crate::system::PipeSystem).
/// - Modifying a [`Schedule`](super::Schedule) may change the order buffers are applied.
#[allow(unused_variables)]
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
pub fn apply_system_buffers(world: &mut World) {}
pub fn apply_deferred(world: &mut World) {}

/// Returns `true` if the [`System`](crate::system::System) is an instance of [`apply_system_buffers`].
pub(super) fn is_apply_system_buffers(system: &BoxedSystem) -> bool {
/// Returns `true` if the [`System`](crate::system::System) is an instance of [`apply_deferred`].
pub(super) fn is_apply_deferred(system: &BoxedSystem) -> bool {
use std::any::Any;
// deref to use `System::type_id` instead of `Any::type_id`
system.as_ref().type_id() == apply_system_buffers.type_id()
system.as_ref().type_id() == apply_deferred.type_id()
}
26 changes: 12 additions & 14 deletions crates/bevy_ecs/src/schedule/executor/multi_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ use crate::{
archetype::ArchetypeComponentId,
prelude::Resource,
query::Access,
schedule::{
is_apply_system_buffers, BoxedCondition, ExecutorKind, SystemExecutor, SystemSchedule,
},
schedule::{is_apply_deferred, BoxedCondition, ExecutorKind, SystemExecutor, SystemSchedule},
system::BoxedSystem,
world::{unsafe_world_cell::UnsafeWorldCell, World},
};
Expand Down Expand Up @@ -108,8 +106,8 @@ pub struct MultiThreadedExecutor {
completed_systems: FixedBitSet,
/// Systems that have run but have not had their buffers applied.
unapplied_systems: FixedBitSet,
/// Setting when true applies system buffers after all systems have run
apply_final_buffers: bool,
/// Setting when true applies deferred system buffers after all systems have run
apply_final_deferred: bool,
/// When set, tells the executor that a thread has panicked.
panic_payload: Arc<Mutex<Option<Box<dyn Any + Send>>>>,
/// When set, stops the executor from running any more systems.
Expand All @@ -127,8 +125,8 @@ impl SystemExecutor for MultiThreadedExecutor {
ExecutorKind::MultiThreaded
}

fn set_apply_final_buffers(&mut self, value: bool) {
self.apply_final_buffers = value;
fn set_apply_final_deferred(&mut self, value: bool) {
self.apply_final_deferred = value;
}

fn init(&mut self, schedule: &SystemSchedule) {
Expand Down Expand Up @@ -230,10 +228,10 @@ impl SystemExecutor for MultiThreadedExecutor {
},
);

if self.apply_final_buffers {
if self.apply_final_deferred {
// Do one final apply buffers after all systems have completed
// Commands should be applied while on the scope's thread, not the executor's thread
let res = apply_system_buffers(&self.unapplied_systems, systems, world);
let res = apply_deferred(&self.unapplied_systems, systems, world);
if let Err(payload) = res {
let mut panic_payload = self.panic_payload.lock().unwrap();
*panic_payload = Some(payload);
Expand Down Expand Up @@ -278,7 +276,7 @@ impl MultiThreadedExecutor {
skipped_systems: FixedBitSet::new(),
completed_systems: FixedBitSet::new(),
unapplied_systems: FixedBitSet::new(),
apply_final_buffers: true,
apply_final_deferred: true,
panic_payload: Arc::new(Mutex::new(None)),
stop_spawning: false,
}
Expand Down Expand Up @@ -556,14 +554,14 @@ impl MultiThreadedExecutor {

let sender = self.sender.clone();
let panic_payload = self.panic_payload.clone();
if is_apply_system_buffers(system) {
if is_apply_deferred(system) {
// TODO: avoid allocation
let unapplied_systems = self.unapplied_systems.clone();
self.unapplied_systems.clear();
let task = async move {
#[cfg(feature = "trace")]
let system_guard = system_span.enter();
let res = apply_system_buffers(&unapplied_systems, systems, world);
let res = apply_deferred(&unapplied_systems, systems, world);
#[cfg(feature = "trace")]
drop(system_guard);
// tell the executor that the system finished
Expand Down Expand Up @@ -681,7 +679,7 @@ impl MultiThreadedExecutor {
}
}

fn apply_system_buffers(
fn apply_deferred(
unapplied_systems: &FixedBitSet,
systems: &[SyncUnsafeCell<BoxedSystem>],
world: &mut World,
Expand All @@ -690,7 +688,7 @@ fn apply_system_buffers(
// SAFETY: none of these systems are running, no other references exist
let system = unsafe { &mut *systems[system_index].get() };
let res = std::panic::catch_unwind(AssertUnwindSafe(|| {
system.apply_buffers(world);
system.apply_deferred(world);
}));
if let Err(payload) = res {
eprintln!(
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_ecs/src/schedule/executor/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
};

/// A variant of [`SingleThreadedExecutor`](crate::schedule::SingleThreadedExecutor) that calls
/// [`apply_buffers`](crate::system::System::apply_buffers) immediately after running each system.
/// [`apply_deferred`](crate::system::System::apply_deferred) immediately after running each system.
#[derive(Default)]
pub struct SimpleExecutor {
/// Systems sets whose conditions have been evaluated.
Expand All @@ -23,7 +23,7 @@ impl SystemExecutor for SimpleExecutor {
ExecutorKind::Simple
}

fn set_apply_final_buffers(&mut self, _: bool) {
fn set_apply_final_deferred(&mut self, _: bool) {
// do nothing. simple executor does not do a final sync
}

Expand Down Expand Up @@ -89,7 +89,7 @@ impl SystemExecutor for SimpleExecutor {
std::panic::resume_unwind(payload);
}

system.apply_buffers(world);
system.apply_deferred(world);
}

self.evaluated_sets.clear();
Expand Down
26 changes: 12 additions & 14 deletions crates/bevy_ecs/src/schedule/executor/single_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use fixedbitset::FixedBitSet;
use std::panic::AssertUnwindSafe;

use crate::{
schedule::{
is_apply_system_buffers, BoxedCondition, ExecutorKind, SystemExecutor, SystemSchedule,
},
schedule::{is_apply_deferred, BoxedCondition, ExecutorKind, SystemExecutor, SystemSchedule},
world::World,
};

Expand All @@ -22,17 +20,17 @@ pub struct SingleThreadedExecutor {
completed_systems: FixedBitSet,
/// Systems that have run but have not had their buffers applied.
unapplied_systems: FixedBitSet,
/// Setting when true applies system buffers after all systems have run
apply_final_buffers: bool,
/// Setting when true applies deferred system buffers after all systems have run
apply_final_deferred: bool,
}

impl SystemExecutor for SingleThreadedExecutor {
fn kind(&self) -> ExecutorKind {
ExecutorKind::SingleThreaded
}

fn set_apply_final_buffers(&mut self, apply_final_buffers: bool) {
self.apply_final_buffers = apply_final_buffers;
fn set_apply_final_deferred(&mut self, apply_final_deferred: bool) {
self.apply_final_deferred = apply_final_deferred;
}

fn init(&mut self, schedule: &SystemSchedule) {
Expand Down Expand Up @@ -87,10 +85,10 @@ impl SystemExecutor for SingleThreadedExecutor {
}

let system = &mut schedule.systems[system_index];
if is_apply_system_buffers(system) {
if is_apply_deferred(system) {
#[cfg(feature = "trace")]
let system_span = info_span!("system", name = &*name).entered();
self.apply_system_buffers(schedule, world);
self.apply_deferred(schedule, world);
#[cfg(feature = "trace")]
system_span.exit();
} else {
Expand All @@ -109,8 +107,8 @@ impl SystemExecutor for SingleThreadedExecutor {
}
}

if self.apply_final_buffers {
self.apply_system_buffers(schedule, world);
if self.apply_final_deferred {
self.apply_deferred(schedule, world);
}
self.evaluated_sets.clear();
self.completed_systems.clear();
Expand All @@ -123,14 +121,14 @@ impl SingleThreadedExecutor {
evaluated_sets: FixedBitSet::new(),
completed_systems: FixedBitSet::new(),
unapplied_systems: FixedBitSet::new(),
apply_final_buffers: true,
apply_final_deferred: true,
}
}

fn apply_system_buffers(&mut self, schedule: &mut SystemSchedule, world: &mut World) {
fn apply_deferred(&mut self, schedule: &mut SystemSchedule, world: &mut World) {
for system_index in self.unapplied_systems.ones() {
let system = &mut schedule.systems[system_index];
system.apply_buffers(world);
system.apply_deferred(world);
}

self.unapplied_systems.clear();
Expand Down
18 changes: 9 additions & 9 deletions crates/bevy_ecs/src/schedule/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,12 @@ impl Schedule {
self
}

/// Set whether the schedule applies buffers on final time or not. This is a catchall
/// incase a system uses commands but was not explicitly ordered after a
/// [`apply_system_buffers`](crate::prelude::apply_system_buffers). By default this
/// Set whether the schedule applies deferred system buffers on final time or not. This is a catch-all
/// in case a system uses commands but was not explicitly ordered before an instance of
/// [`apply_deferred`](crate::prelude::apply_deferred). By default this
/// setting is true, but may be disabled if needed.
pub fn set_apply_final_buffers(&mut self, apply_final_buffers: bool) -> &mut Self {
self.executor.set_apply_final_buffers(apply_final_buffers);
pub fn set_apply_final_deferred(&mut self, apply_final_deferred: bool) -> &mut Self {
self.executor.set_apply_final_deferred(apply_final_deferred);
self
}

Expand Down Expand Up @@ -287,17 +287,17 @@ impl Schedule {
}
}

/// Directly applies any accumulated system buffers (like [`Commands`](crate::prelude::Commands)) to the `world`.
/// Directly applies any accumulated [`Deferred`](crate::system::Deferred) system parameters (like [`Commands`](crate::prelude::Commands)) to the `world`.
///
/// Like always, system buffers are applied in the "topological sort order" of the schedule graph.
/// Like always, deferred system parameters are applied in the "topological sort order" of the schedule graph.
/// As a result, buffers from one system are only guaranteed to be applied before those of other systems
/// if there is an explicit system ordering between the two systems.
///
/// This is used in rendering to extract data from the main world, storing the data in system buffers,
/// before applying their buffers in a different world.
pub fn apply_system_buffers(&mut self, world: &mut World) {
pub fn apply_deferred(&mut self, world: &mut World) {
for system in &mut self.executable.systems {
system.apply_buffers(world);
system.apply_deferred(world);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_ecs/src/system/combinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ where
)
}

fn apply_buffers(&mut self, world: &mut World) {
self.a.apply_buffers(world);
self.b.apply_buffers(world);
fn apply_deferred(&mut self, world: &mut World) {
self.a.apply_deferred(world);
self.b.apply_deferred(world);
}

fn initialize(&mut self, world: &mut World) {
Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_ecs/src/system/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ pub trait Command: Send + 'static {
///
/// Since each command requires exclusive access to the `World`,
/// all queued commands are automatically applied in sequence
/// when the [`apply_system_buffers`] system runs.
/// when the [`apply_deferred`] system runs.
///
/// The command queue of an individual system can also be manually applied
/// by calling [`System::apply_buffers`].
/// Similarly, the command queue of a schedule can be manually applied via [`Schedule::apply_system_buffers`].
/// by calling [`System::apply_deferred`].
/// Similarly, the command queue of a schedule can be manually applied via [`Schedule::apply_deferred`].
///
/// Each command can be used to modify the [`World`] in arbitrary ways:
/// * spawning or despawning entities
Expand All @@ -68,7 +68,7 @@ pub trait Command: Send + 'static {
///
/// # Usage
///
/// Add `mut commands: Commands` as a function argument to your system to get a copy of this struct that will be applied the next time a copy of [`apply_system_buffers`] runs.
/// Add `mut commands: Commands` as a function argument to your system to get a copy of this struct that will be applied the next time a copy of [`apply_deferred`] runs.
/// Commands are almost always used as a [`SystemParam`](crate::system::SystemParam).
///
/// ```
Expand Down Expand Up @@ -100,9 +100,9 @@ pub trait Command: Send + 'static {
/// # }
/// ```
///
/// [`System::apply_buffers`]: crate::system::System::apply_buffers
/// [`apply_system_buffers`]: crate::schedule::apply_system_buffers
/// [`Schedule::apply_system_buffers`]: crate::schedule::Schedule::apply_system_buffers
/// [`System::apply_deferred`]: crate::system::System::apply_deferred
/// [`apply_deferred`]: crate::schedule::apply_deferred
/// [`Schedule::apply_deferred`]: crate::schedule::Schedule::apply_deferred
#[derive(SystemParam)]
pub struct Commands<'w, 's> {
queue: Deferred<'s, CommandQueue>,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/system/exclusive_function_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ where
}

#[inline]
fn apply_buffers(&mut self, _world: &mut World) {
fn apply_deferred(&mut self, _world: &mut World) {
// "pure" exclusive systems do not have any buffers to apply.
// Systems made by piping a normal system with an exclusive system
// might have buffers to apply, but this is handled by `PipeSystem`.
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/system/function_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ where
}

#[inline]
fn apply_buffers(&mut self, world: &mut World) {
fn apply_deferred(&mut self, world: &mut World) {
let param_state = self.param_state.as_mut().expect(Self::PARAM_MESSAGE);
F::Param::apply(param_state, &self.system_meta, world);
}
Expand Down
Loading