Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
BoxyUwU authored Aug 29, 2020
2 parents f15590c + 17e7642 commit 0ea4076
Show file tree
Hide file tree
Showing 89 changed files with 1,050 additions and 171 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ bevy_sprite = { path = "crates/bevy_sprite", version = "0.1" }
bevy_transform = { path = "crates/bevy_transform", version = "0.1" }
bevy_text = { path = "crates/bevy_text", version = "0.1" }
bevy_ui = { path = "crates/bevy_ui", version = "0.1" }
bevy_utils = { path = "crates/bevy_utils", version = "0.1" }
bevy_window = { path = "crates/bevy_window", version = "0.1" }
bevy_tasks = { path = "crates/bevy_tasks", version = "0.1" }

# bevy (optional)
bevy_audio = { path = "crates/bevy_audio", optional = true, version = "0.1" }
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ keywords = ["bevy"]
# bevy
bevy_derive = { path = "../bevy_derive", version = "0.1" }
bevy_ecs = { path = "../bevy_ecs", version = "0.1" }
bevy_tasks = { path = "../bevy_tasks", version = "0.1" }
bevy_math = { path = "../bevy_math", version = "0.1" }

# other
libloading = "0.6"
log = { version = "0.4", features = ["release_max_level_info"] }
serde = { version = "1.0", features = ["derive"]}
serde = { version = "1.0", features = ["derive"]}
8 changes: 7 additions & 1 deletion crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::app_builder::AppBuilder;
use crate::{app_builder::AppBuilder, DefaultTaskPoolOptions};
use bevy_ecs::{ParallelExecutor, Resources, Schedule, World};

#[allow(clippy::needless_doctest_main)]
Expand Down Expand Up @@ -63,6 +63,12 @@ impl App {
}

pub fn run(mut self) {
// Setup the default bevy task pools
self.resources
.get_cloned::<DefaultTaskPoolOptions>()
.unwrap_or_else(DefaultTaskPoolOptions::default)
.create_default_pools(&mut self.resources);

self.startup_schedule.initialize(&mut self.resources);
self.startup_executor.run(
&mut self.startup_schedule,
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ mod app_builder;
mod event;
mod plugin;
mod schedule_runner;
mod task_pool_options;

pub use app::*;
pub use app_builder::*;
pub use bevy_derive::DynamicPlugin;
pub use event::*;
pub use plugin::*;
pub use schedule_runner::*;
pub use task_pool_options::*;

pub mod prelude {
pub use crate::{
Expand Down
147 changes: 147 additions & 0 deletions crates/bevy_app/src/task_pool_options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
use bevy_ecs::Resources;
use bevy_tasks::{AsyncComputeTaskPool, ComputeTaskPool, IOTaskPool, TaskPoolBuilder};

/// Defines a simple way to determine how many threads to use given the number of remaining cores
/// and number of total cores
#[derive(Clone)]
pub struct TaskPoolThreadAssignmentPolicy {
/// Force using at least this many threads
pub min_threads: usize,
/// Under no circumstance use more than this many threads for this pool
pub max_threads: usize,
/// Target using this percentage of total cores, clamped by min_threads and max_threads. It is
/// permitted to use 1.0 to try to use all remaining threads
pub percent: f32,
}

impl TaskPoolThreadAssignmentPolicy {
/// Determine the number of threads to use for this task pool
fn get_number_of_threads(&self, remaining_threads: usize, total_threads: usize) -> usize {
assert!(self.percent >= 0.0);
let mut desired = (total_threads as f32 * self.percent).round() as usize;

// Limit ourselves to the number of cores available
desired = desired.min(remaining_threads);

// Clamp by min_threads, max_threads. (This may result in us using more threads than are
// available, this is intended. An example case where this might happen is a device with
// <= 2 threads.
bevy_math::clamp(desired, self.min_threads, self.max_threads)
}
}

/// Helper for configuring and creating the default task pools. For end-users who want full control,
/// insert the default task pools into the resource map manually. If the pools are already inserted,
/// this helper will do nothing.
#[derive(Clone)]
pub struct DefaultTaskPoolOptions {
/// If the number of physical cores is less than min_total_threads, force using min_total_threads
pub min_total_threads: usize,
/// If the number of physical cores is grater than max_total_threads, force using max_total_threads
pub max_total_threads: usize,

/// Used to determine number of IO threads to allocate
pub io: TaskPoolThreadAssignmentPolicy,
/// Used to determine number of async compute threads to allocate
pub async_compute: TaskPoolThreadAssignmentPolicy,
/// Used to determine number of compute threads to allocate
pub compute: TaskPoolThreadAssignmentPolicy,
}

impl Default for DefaultTaskPoolOptions {
fn default() -> Self {
DefaultTaskPoolOptions {
// By default, use however many cores are available on the system
min_total_threads: 1,
max_total_threads: std::usize::MAX,

// Use 25% of cores for IO, at least 1, no more than 4
io: TaskPoolThreadAssignmentPolicy {
min_threads: 1,
max_threads: 4,
percent: 0.25,
},

// Use 25% of cores for async compute, at least 1, no more than 4
async_compute: TaskPoolThreadAssignmentPolicy {
min_threads: 1,
max_threads: 4,
percent: 0.25,
},

// Use all remaining cores for compute (at least 1)
compute: TaskPoolThreadAssignmentPolicy {
min_threads: 1,
max_threads: std::usize::MAX,
percent: 1.0, // This 1.0 here means "whatever is left over"
},
}
}
}

impl DefaultTaskPoolOptions {
/// Create a configuration that forces using the given number of threads.
pub fn with_num_threads(thread_count: usize) -> Self {
let mut options = Self::default();
options.min_total_threads = thread_count;
options.max_total_threads = thread_count;

options
}

/// Inserts the default thread pools into the given resource map based on the configured values
pub fn create_default_pools(&self, resources: &mut Resources) {
let total_threads = bevy_math::clamp(
bevy_tasks::logical_core_count(),
self.min_total_threads,
self.max_total_threads,
);

let mut remaining_threads = total_threads;

if !resources.contains::<IOTaskPool>() {
// Determine the number of IO threads we will use
let io_threads = self
.io
.get_number_of_threads(remaining_threads, total_threads);
remaining_threads -= io_threads;

resources.insert(IOTaskPool(
TaskPoolBuilder::default()
.num_threads(io_threads)
.thread_name("IO Task Pool".to_string())
.build(),
));
}

if !resources.contains::<AsyncComputeTaskPool>() {
// Determine the number of async compute threads we will use
let async_compute_threads = self
.async_compute
.get_number_of_threads(remaining_threads, total_threads);
remaining_threads -= async_compute_threads;

resources.insert(AsyncComputeTaskPool(
TaskPoolBuilder::default()
.num_threads(async_compute_threads)
.thread_name("Async Compute Task Pool".to_string())
.build(),
));
}

if !resources.contains::<ComputeTaskPool>() {
// Determine the number of compute threads we will use
// This is intentionally last so that an end user can specify 1.0 as the percent
let compute_threads = self
.compute
.get_number_of_threads(remaining_threads, total_threads);

resources.insert(ComputeTaskPool(
TaskPoolBuilder::default()
.num_threads(compute_threads)
.thread_name("Compute Task Pool".to_string())
.build(),
));
}
}
}
1 change: 1 addition & 0 deletions crates/bevy_asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bevy_app = { path = "../bevy_app", version = "0.1" }
bevy_ecs = { path = "../bevy_ecs", version = "0.1" }
bevy_type_registry = { path = "../bevy_type_registry", version = "0.1" }
bevy_property = { path = "../bevy_property", version = "0.1" }
bevy_utils = { path = "../bevy_utils", version = "0.1" }

# other
uuid = { version = "0.8", features = ["v4", "serde"] }
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_asset/src/asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use crate::{
};
use anyhow::Result;
use bevy_ecs::{Res, Resource, Resources};
use bevy_utils::{HashMap, HashSet};
use crossbeam_channel::TryRecvError;
use parking_lot::RwLock;
use std::{
collections::{HashMap, HashSet},
env, fs, io,
path::{Path, PathBuf},
sync::Arc,
Expand Down Expand Up @@ -185,7 +185,7 @@ impl AssetServer {
#[cfg(feature = "filesystem_watcher")]
pub fn filesystem_watcher_system(asset_server: Res<AssetServer>) {
use notify::event::{Event, EventKind, ModifyKind};
let mut changed = HashSet::new();
let mut changed = HashSet::default();

while let Some(filesystem_watcher) = asset_server.filesystem_watcher.read().as_ref() {
let result = match filesystem_watcher.receiver.try_recv() {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
use bevy_app::{prelude::Events, AppBuilder};
use bevy_ecs::{FromResources, IntoQuerySystem, ResMut, Resource};
use bevy_type_registry::RegisterType;
use std::collections::HashMap;
use bevy_utils::HashMap;

/// Events that happen on assets of type `T`
pub enum AssetEvent<T: Resource> {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ bevy_derive = { path = "../bevy_derive", version = "0.1" }
bevy_ecs = { path = "../bevy_ecs", version = "0.1" }
bevy_property = { path = "../bevy_property", version = "0.1" }
bevy_type_registry = { path = "../bevy_type_registry", version = "0.1" }
bevy_math = { path = "../bevy_math", version = "0.1" }
bevy_math = { path = "../bevy_math", version = "0.1" }
bevy_utils = { path = "../bevy_utils", version = "0.1" }
4 changes: 2 additions & 2 deletions crates/bevy_core/src/label.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use bevy_ecs::prelude::*;
use bevy_property::Properties;
use bevy_utils::{HashMap, HashSet};
use std::{
borrow::Cow,
collections::{HashMap, HashSet},
fmt::Debug,
ops::{Deref, DerefMut},
};
Expand All @@ -29,7 +29,7 @@ where
T: IntoIterator<Item = L>,
{
fn from(value: T) -> Self {
let mut labels = HashSet::new();
let mut labels = HashSet::default();
for label in value {
labels.insert(label.into());
}
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_diagnostic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ profiler = ["bevy_ecs/profiler"]
bevy_app = { path = "../bevy_app", version = "0.1" }
bevy_core = { path = "../bevy_core", version = "0.1" }
bevy_ecs = { path = "../bevy_ecs", version = "0.1" }
bevy_utils = { path = "../bevy_utils", version = "0.1" }

# other
uuid = { version = "0.8", features = ["v4", "serde"] }
parking_lot = "0.10"
parking_lot = "0.10"
3 changes: 2 additions & 1 deletion crates/bevy_diagnostic/src/diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bevy_utils::HashMap;
use std::{
collections::{HashMap, VecDeque},
collections::VecDeque,
time::{Duration, SystemTime},
};
use uuid::Uuid;
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_diagnostic/src/system_profiler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{Diagnostic, DiagnosticId, Diagnostics};
use bevy_ecs::{Profiler, Res, ResMut};
use bevy_utils::HashMap;
use parking_lot::RwLock;
use std::{borrow::Cow, collections::HashMap, sync::Arc, time::Instant};
use std::{borrow::Cow, sync::Arc, time::Instant};

#[derive(Debug)]
struct SystemRunInfo {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ecs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ profiler = []

[dependencies]
bevy_hecs = { path = "hecs", features = ["macros", "serialize"], version = "0.1" }
bevy_tasks = { path = "../bevy_tasks", version = "0.1" }
bevy_utils = { path = "../bevy_utils", version = "0.1" }
rand = "0.7.2"
rayon = "1.3"
crossbeam-channel = "0.4.2"
fixedbitset = "0.3.0"
downcast-rs = "1.1.1"
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/hecs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ serialize = ["serde"]

[dependencies]
bevy_hecs_macros = { path = "macros", version = "0.1.3", optional = true }
hashbrown = { version = "0.8.0", default-features = false, features = ["ahash", "inline-more"] }
bevy_utils = { path = "../../bevy_utils", version = "0.1" }
lazy_static = { version = "1.4.0", optional = true, features = ["spin_no_std"] }
serde = { version = "1", features = ["derive"], optional = true}
rand = "0.7.3"
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/hecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn derive_bundle(input: TokenStream) -> TokenStream {

#path::lazy_static::lazy_static! {
static ref ELEMENTS: [TypeId; #n] = {
let mut dedup = std::collections::HashSet::new();
let mut dedup = #path::bevy_utils::HashSet::default();
for &(ty, name) in [#((std::any::TypeId::of::<#tys>(), std::any::type_name::<#tys>())),*].iter() {
if !dedup.insert(ty) {
panic!("{} has multiple {} fields; each type must occur at most once!", stringify!(#ident), name);
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_ecs/hecs/src/archetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ use crate::alloc::{
vec,
vec::Vec,
};
use bevy_utils::{HashMap, HashMapExt};
use core::{
any::{type_name, TypeId},
cell::UnsafeCell,
mem,
ptr::{self, NonNull},
};

use hashbrown::HashMap;

use crate::{borrow::AtomicBorrow, query::Fetch, Access, Component, Query};

/// A collection of entities having the same component types
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/hecs/src/entities.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// modified by Bevy contributors

use bevy_utils::HashMap;
use core::fmt;
use hashbrown::HashMap;
#[cfg(feature = "std")]
use std::error::Error;

Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_ecs/hecs/src/entity_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ use crate::alloc::{
vec,
vec::Vec,
};

use bevy_utils::HashSet;
use core::{
any::TypeId,
mem::{self, MaybeUninit},
ptr,
};

use hashbrown::HashSet;

use crate::{archetype::TypeInfo, Component, DynamicBundle};

/// Helper for incrementally constructing a bundle of components with dynamic component types
Expand Down Expand Up @@ -59,7 +59,7 @@ impl EntityBuilder {
storage: Box::new([]),
info: Vec::new(),
ids: Vec::new(),
id_set: HashSet::new(),
id_set: HashSet::default(),
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_ecs/hecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ pub use world::{ArchetypesGeneration, Component, ComponentError, Iter, SpawnBatc
// Unstable implementation details needed by the macros
#[doc(hidden)]
pub use archetype::TypeInfo;
#[doc(hidden)]
pub use bevy_utils;
#[cfg(feature = "macros")]
#[doc(hidden)]
pub use lazy_static;
Expand Down
Loading

0 comments on commit 0ea4076

Please sign in to comment.