From 97a74c89a33aafca4a1d602292ce6bfa3239c4d2 Mon Sep 17 00:00:00 2001 From: Zakarum Date: Thu, 18 Oct 2018 12:08:23 +0300 Subject: [PATCH] Make it possible to disable parallel feature --- Cargo.toml | 8 +++++--- src/bitset.rs | 5 ++++- src/join.rs | 20 +++++++++++++++++++- src/lib.rs | 7 +++++-- src/prelude.rs | 8 +++++--- src/storage/mod.rs | 7 ++++++- src/storage/restrict.rs | 7 ++++++- src/world/entity.rs | 5 ++++- 8 files changed, 54 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dcc9a5364..59f8b1753 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,14 +24,14 @@ travis-ci = { repository = "slide-rs/specs" } crossbeam = "0.4.1" derivative = "1" fnv = "1.0" -hibitset = { version = "0.5.2", features = ["parallel"] } +hibitset = { version = "0.5.2", default-features = false } log = "0.4" mopa = "0.2" -shred = "0.7.0" +shred = { version = "0.7.0", default-features = false } shrev = "1.0.0" shred-derive = "0.5.0" tuple_utils = "0.2" -rayon = "1.0.0" +rayon = { version = "1.0.0", optional = true } nonzero_signed = "1.0.1" futures = { version = "0.1", optional = true } @@ -41,6 +41,8 @@ serde = { version = "1.0", optional = true, features = ["serde_derive"] } rudy = { version = "0.1", optional = true } [features] +default = ["parallel"] +parallel = ["rayon", "shred/parallel", "hibitset/parallel"] common = ["futures"] nightly = ["shred/nightly"] diff --git a/src/bitset.rs b/src/bitset.rs index 906f195cc..b7bfe94f3 100644 --- a/src/bitset.rs +++ b/src/bitset.rs @@ -6,7 +6,9 @@ use hibitset::{AtomicBitSet, BitSet, BitSetAnd, BitSetLike, BitSetNot, BitSetOr, BitSetXor}; -use join::{Join, ParJoin}; +use join::Join; +#[cfg(feature = "parallel")] +use join::ParJoin; use world::Index; macro_rules! define_bit_join { @@ -25,6 +27,7 @@ macro_rules! define_bit_join { } } + #[cfg(feature = "parallel")] unsafe impl<$( $lifetime, )* $( $arg ),*> ParJoin for $bitset where $( $arg: BitSetLike ),* { } diff --git a/src/join.rs b/src/join.rs index 55a42dd38..867803350 100644 --- a/src/join.rs +++ b/src/join.rs @@ -1,10 +1,18 @@ //! Joining of components for iteration over entities with specific components. use std; +#[cfg(feature = "parallel")] use std::cell::UnsafeCell; -use hibitset::{BitIter, BitProducer, BitSetAll, BitSetAnd, BitSetLike}; +use hibitset::{BitIter, BitSetAll, BitSetAnd, BitSetLike}; + +#[cfg(feature = "parallel")] +use hibitset::BitProducer; + +#[cfg(feature = "parallel")] use rayon::iter::plumbing::{bridge_unindexed, Folder, UnindexedConsumer, UnindexedProducer}; + +#[cfg(feature = "parallel")] use rayon::iter::ParallelIterator; use std::ops::{Deref, DerefMut}; use tuple_utils::Split; @@ -241,6 +249,7 @@ pub trait Join { /// The purpose of the `ParJoin` trait is to provide a way /// to access multiple storages in parallel at the same time with /// the merged bit set. +#[cfg(feature = "parallel")] pub unsafe trait ParJoin: Join { /// Create a joined parallel iterator over the contents. fn par_join(self) -> JoinParIter @@ -399,9 +408,11 @@ impl std::iter::Iterator for JoinIter { } /// `JoinParIter` is a `ParallelIterator` over a group of `Storages`. +#[cfg(feature = "parallel")] #[must_use] pub struct JoinParIter(J); +#[cfg(feature = "parallel")] impl ParallelIterator for JoinParIter where J: Join + Send, @@ -424,6 +435,7 @@ where } } +#[cfg(feature = "parallel")] struct JoinProducer<'a, J> where J: Join + Send, @@ -435,6 +447,7 @@ where values: &'a UnsafeCell, } +#[cfg(feature = "parallel")] impl<'a, J> JoinProducer<'a, J> where J: Join + Send, @@ -447,6 +460,7 @@ where } } +#[cfg(feature = "parallel")] unsafe impl<'a, J> Send for JoinProducer<'a, J> where J: Join + Send, @@ -455,6 +469,7 @@ where J::Mask: 'a + Send + Sync, {} +#[cfg(feature = "parallel")] impl<'a, J> UnindexedProducer for JoinProducer<'a, J> where J: Join + Send, @@ -523,6 +538,7 @@ macro_rules! define_open { unconstrained } } + #[cfg(feature = "parallel")] unsafe impl<$($from,)*> ParJoin for ($($from),*,) where $($from: ParJoin),*, ($(<$from as Join>::Mask,)*): BitAnd, @@ -582,6 +598,7 @@ macro_rules! immutable_resource_join { } } + #[cfg(feature = "parallel")] unsafe impl<'a, 'b, T> ParJoin for &'a $ty where &'a T: ParJoin, @@ -616,6 +633,7 @@ macro_rules! mutable_resource_join { } } + #[cfg(feature = "parallel")] unsafe impl<'a, 'b, T> ParJoin for &'a mut $ty where &'a mut T: ParJoin, diff --git a/src/lib.rs b/src/lib.rs index 065473b01..36ef5ebfb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -194,6 +194,7 @@ extern crate hibitset; extern crate log; extern crate nonzero_signed; extern crate mopa; +#[cfg(feature = "parallel")] extern crate rayon; extern crate shrev; extern crate tuple_utils; @@ -222,12 +223,14 @@ pub mod storage; pub mod world; pub use hibitset::BitSet; -pub use join::{Join, ParJoin}; +pub use join::Join; +#[cfg(feature = "parallel")] +pub use join::ParJoin; pub use shred::{Accessor, Dispatcher, DispatcherBuilder, Read, ReadExpect, Resources, RunNow, StaticAccessor, System, SystemData, Write, WriteExpect}; pub use shrev::ReaderId; -#[cfg(not(target_os = "emscripten"))] +#[cfg(feature = "parallel")] pub use shred::AsyncDispatcher; pub use changeset::ChangeSet; diff --git a/src/prelude.rs b/src/prelude.rs index 522605f0e..0c3c9d8f3 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -3,14 +3,16 @@ //! Contains all of the most common traits, structures, pub use hibitset::BitSet; -pub use join::{Join, ParJoin}; +pub use join::Join; +#[cfg(feature = "parallel")] +pub use join::ParJoin; pub use shred::{Accessor, Dispatcher, DispatcherBuilder, Read, ReadExpect, Resources, RunNow, StaticAccessor, System, SystemData, Write, WriteExpect}; pub use shrev::ReaderId; -#[cfg(not(target_os = "emscripten"))] +#[cfg(feature = "parallel")] pub use rayon::iter::ParallelIterator; -#[cfg(not(target_os = "emscripten"))] +#[cfg(feature = "parallel")] pub use shred::AsyncDispatcher; pub use changeset::ChangeSet; diff --git a/src/storage/mod.rs b/src/storage/mod.rs index e87081e5e..dd5160ae8 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -19,7 +19,9 @@ use shred::{CastFrom, Fetch}; use self::drain::Drain; use error::{Error, WrongGeneration}; -use join::{Join, ParJoin}; +use join::Join; +#[cfg(feature = "parallel")] +use join::ParJoin; use world::{Component, EntitiesRes, Entity, Generation, Index}; mod data; @@ -52,6 +54,7 @@ impl<'a> Join for AntiStorage<'a> { unsafe impl<'a> DistinctStorage for AntiStorage<'a> {} +#[cfg(feature = "parallel")] unsafe impl<'a> ParJoin for AntiStorage<'a> {} /// A dynamic storage. @@ -478,6 +481,7 @@ where } } +#[cfg(feature = "parallel")] unsafe impl<'a, 'e, T, D> ParJoin for &'a Storage<'e, T, D> where T: Component, @@ -508,6 +512,7 @@ where } } +#[cfg(feature = "parallel")] unsafe impl<'a, 'e, T, D> ParJoin for &'a mut Storage<'e, T, D> where T: Component, diff --git a/src/storage/restrict.rs b/src/storage/restrict.rs index 91ca2b9e3..547cc8db2 100644 --- a/src/storage/restrict.rs +++ b/src/storage/restrict.rs @@ -5,7 +5,10 @@ use std::ops::{Deref, DerefMut}; use hibitset::BitSet; use shred::Fetch; -use join::{Join, ParJoin}; +use join::Join; + +#[cfg(feature = "parallel")] +use join::ParJoin; use storage::{MaskedStorage, Storage, UnprotectedStorage}; use world::{Component, EntitiesRes, Entity, Index}; @@ -76,6 +79,7 @@ where phantom: PhantomData<(C, Restrict)>, } +#[cfg(feature = "parallel")] unsafe impl<'rf, 'st: 'rf, C, S, B> ParJoin for &'rf mut RestrictedStorage<'rf, 'st, C, S, B, MutableParallelRestriction> where @@ -85,6 +89,7 @@ where { } +#[cfg(feature = "parallel")] unsafe impl<'rf, 'st: 'rf, C, S, B, Restrict> ParJoin for &'rf RestrictedStorage<'rf, 'st, C, S, B, Restrict> where diff --git a/src/world/entity.rs b/src/world/entity.rs index 71d02d6bf..de78d53f4 100644 --- a/src/world/entity.rs +++ b/src/world/entity.rs @@ -6,7 +6,9 @@ use nonzero_signed::NonZeroI32; use shred::Read; use error::WrongGeneration; -use join::{Join, ParJoin}; +use join::Join; +#[cfg(feature = "parallel")] +use join::ParJoin; use storage::WriteStorage; use world::Component; @@ -327,6 +329,7 @@ impl<'a> Join for &'a EntitiesRes { } } +#[cfg(feature = "parallel")] unsafe impl<'a> ParJoin for &'a EntitiesRes {} /// An entity builder from `EntitiesRes`. Allows building an entity with its