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

Add optional single-threaded feature to bevy_ecs/bevy_tasks #6690

Merged
merged 20 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add feature for disabling parallel executors in bevy_ecs
  • Loading branch information
james7132 committed Nov 19, 2022
commit fa906daa12270e41c677b6d69e554378f7a1d3b4
1 change: 1 addition & 0 deletions crates/bevy_ecs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ categories = ["game-engines", "data-structures"]

[features]
trace = []
single-threaded = []
default = ["bevy_reflect"]

[dependencies]
Expand Down
24 changes: 24 additions & 0 deletions crates/bevy_ecs/src/query/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,28 @@ impl<Q: WorldQuery, F: ReadOnlyWorldQuery> QueryState<Q, F> {
);
}

#[inline]
#[cfg(feature = "single-threaded")]
pub fn par_for_each<'w, FN: Fn(ROQueryItem<'w, Q>) + Send + Sync + Clone>(
&mut self,
world: &'w World,
_batch_size: usize,
func: FN,
) {
self.for_each(world, func);
}

#[inline]
#[cfg(feature = "single-threaded")]
pub fn par_for_each_mut<'w, FN: Fn(Q::Item<'w>) + Send + Sync + Clone>(
&mut self,
world: &'w mut World,
_batch_size: usize,
func: FN,
) {
self.for_each_mut(world, func);
}

/// Runs `func` on each query result in parallel.
///
/// This can only be called for read-only queries, see [`Self::par_for_each_mut`] for
Expand All @@ -842,6 +864,7 @@ impl<Q: WorldQuery, F: ReadOnlyWorldQuery> QueryState<Q, F> {
/// The [`ComputeTaskPool`] is not initialized. If using this from a query that is being
/// initialized and run from the ECS scheduler, this should never panic.
#[inline]
#[cfg(not(feature = "single-threaded"))]
pub fn par_for_each<'w, FN: Fn(ROQueryItem<'w, Q>) + Send + Sync + Clone>(
&mut self,
world: &'w World,
Expand All @@ -867,6 +890,7 @@ impl<Q: WorldQuery, F: ReadOnlyWorldQuery> QueryState<Q, F> {
/// The [`ComputeTaskPool`] is not initialized. If using this from a query that is being
/// initialized and run from the ECS scheduler, this should never panic.
#[inline]
#[cfg(not(feature = "single-threaded"))]
pub fn par_for_each_mut<'w, FN: Fn(Q::Item<'w>) + Send + Sync + Clone>(
&mut self,
world: &'w mut World,
Expand Down
13 changes: 11 additions & 2 deletions crates/bevy_ecs/src/schedule/stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use crate::{
schedule::{
graph_utils::{self, DependencyGraphError},
BoxedRunCriteria, DuplicateLabelStrategy, ExclusiveInsertionPoint, GraphNode,
ParallelExecutor, ParallelSystemExecutor, RunCriteriaContainer, RunCriteriaDescriptor,
ParallelSystemExecutor, RunCriteriaContainer, RunCriteriaDescriptor,
RunCriteriaDescriptorOrLabel, RunCriteriaInner, RunCriteriaLabelId, ShouldRun,
SingleThreadedExecutor, SystemContainer, SystemDescriptor, SystemLabelId, SystemSet,
},
world::{World, WorldId},
};
#[cfg(not(feature = "single-threaded"))]
use crate::schedule::ParallelExecutor;
use bevy_ecs_macros::Resource;
use bevy_utils::{tracing::warn, HashMap, HashSet};
use core::fmt::Debug;
Expand Down Expand Up @@ -136,7 +138,14 @@ impl SystemStage {
}

pub fn parallel() -> Self {
Self::new(Box::<ParallelExecutor>::default())
#[cfg(not(feature = "single-threaded"))]
{
Self::new(Box::<ParallelExecutor>::default())
}
#[cfg(feature = "single-threaded")]
{
Self::single_threaded()
}
}

pub fn get_executor<T: ParallelSystemExecutor>(&self) -> Option<&T> {
Expand Down