Skip to content

Commit

Permalink
Optimize even more by adding sequential groups to stages
Browse files Browse the repository at this point in the history
Add rayon and arrayvec dependencies

Removes rayon_core

Add RunningTime and System::running_time

This allows for further optimizations.

Do not rustfmt macro invocations in system module

Reexport RunningTime

Removed debug printlns
  • Loading branch information
torkleyy committed Jun 5, 2017
1 parent 2f04b69 commit 457f86c
Show file tree
Hide file tree
Showing 8 changed files with 553 additions and 180 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ exclude = ["bors.toml", ".travis.yml"]
travis-ci = { repository = "torkleyy/shred" }

[dependencies]
arrayvec = "0.3"
fnv = "1"
mopa = "0.2"
pulse = "0.5"
rayon = "0.7"
rayon-core = { version = "1.0", features = ["unstable"] }
rayon = { version = "0.7", features = ["unstable"] }
shred-derive = { path = "shred-derive", version = "0.2" }
smallvec = "0.4"

Expand Down
2 changes: 1 addition & 1 deletion src/dispatch/async.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::{Arc, Mutex};

use pulse::Signal;
use rayon_core::ThreadPool;
use rayon::ThreadPool;

use dispatch::ThreadLocal;
use dispatch::stage::Stage;
Expand Down
62 changes: 19 additions & 43 deletions src/dispatch/builder.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
use fnv::FnvHashMap;
use smallvec::SmallVec;

use dispatch::{Dispatcher, SystemDependencies, SystemId, SystemInfo, ThreadLocal};
use dispatch::{Dispatcher, SystemId, ThreadLocal};
use dispatch::stage::StagesBuilder;
use system::{System, SystemData};

#[derive(Default)]
pub struct IdGen {
current: usize,
}

impl IdGen {
pub fn next(&mut self) -> SystemId {
let id = self.current;
self.current += 1;

SystemId(id)
}
}
use system::System;

/// Builder for the [`Dispatcher`].
///
Expand Down Expand Up @@ -67,12 +52,12 @@ impl IdGen {
///
#[derive(Default)]
pub struct DispatcherBuilder<'a, 'b> {
id_gen: IdGen,
current_id: usize,
map: FnvHashMap<String, SystemId>,
stages_builder: StagesBuilder<'a>,
thread_local: ThreadLocal<'b>,
#[cfg(not(target_os = "emscripten"))]
thread_pool: Option<::std::sync::Arc<::rayon_core::ThreadPool>>,
thread_pool: Option<::std::sync::Arc<::rayon::ThreadPool>>,
}

impl<'a, 'b> DispatcherBuilder<'a, 'b> {
Expand All @@ -86,7 +71,7 @@ impl<'a, 'b> DispatcherBuilder<'a, 'b> {
}

/// Adds a new system with a given name and a list of dependencies.
/// Please not that the dependency should be added before
/// Please note that the dependency should be added before
/// you add the depending system.
///
/// # Panics
Expand All @@ -95,31 +80,15 @@ impl<'a, 'b> DispatcherBuilder<'a, 'b> {
pub fn add<T>(mut self, system: T, name: &str, dep: &[&str]) -> Self
where T: for<'c> System<'c> + Send + 'a
{
let id = self.id_gen.next();
let mut reads: Vec<_> = T::SystemData::reads(0);
let writes = T::SystemData::writes(0);
let id = self.next_id();

reads.sort();
reads.dedup();

let mut reads = SmallVec::from_vec(reads);
reads.shrink_to_fit();

let mut writes = SmallVec::from_vec(writes);
writes.shrink_to_fit();

let dependencies: SystemDependencies = dep.iter()
.map(|x| {
*self.map
.get(x.to_owned())
.expect("No such system registered")
})
let dependencies = dep.iter()
.map(|x| *self.map.get(*x).expect("No such system registered"))
.collect();

self.map.insert(name.to_owned(), id);

self.stages_builder
.insert(SystemInfo::new(dependencies, id, reads, Box::new(system), writes));
self.stages_builder.insert(dependencies, id, system);

self
}
Expand Down Expand Up @@ -155,7 +124,7 @@ impl<'a, 'b> DispatcherBuilder<'a, 'b> {
/// Attach a rayon thread pool to the builder
/// and use that instead of creating one.
#[cfg(not(target_os = "emscripten"))]
pub fn with_pool(mut self, pool: ::std::sync::Arc<::rayon_core::ThreadPool>) -> Self {
pub fn with_pool(mut self, pool: ::std::sync::Arc<::rayon::ThreadPool>) -> Self {
self.thread_pool = Some(pool);

self
Expand Down Expand Up @@ -183,10 +152,17 @@ impl<'a, 'b> DispatcherBuilder<'a, 'b> {
d
}

fn next_id(&mut self) -> SystemId {
let id = self.current_id;
self.current_id += 1;

SystemId(id)
}

#[cfg(not(target_os = "emscripten"))]
fn create_thread_pool() -> ::std::sync::Arc<::rayon_core::ThreadPool> {
fn create_thread_pool() -> ::std::sync::Arc<::rayon::ThreadPool> {
use std::sync::Arc;
use rayon_core::{Configuration, ThreadPool};
use rayon::{Configuration, ThreadPool};

Arc::new(ThreadPool::new(Configuration::new()).expect("Invalid thread pool configuration"))
}
Expand Down
36 changes: 3 additions & 33 deletions src/dispatch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub use self::async::AsyncDispatcher;

use smallvec::SmallVec;

use res::{Resources, ResourceId};
use res::Resources;
use system::RunNow;

use self::stage::Stage;
Expand All @@ -20,7 +20,7 @@ pub struct Dispatcher<'a, 'b> {
stages: Vec<Stage<'a>>,
thread_local: ThreadLocal<'b>,
#[cfg(not(target_os = "emscripten"))]
thread_pool: ::std::sync::Arc<::rayon_core::ThreadPool>,
thread_pool: ::std::sync::Arc<::rayon::ThreadPool>,
}

impl<'a, 'b> Dispatcher<'a, 'b> {
Expand Down Expand Up @@ -85,38 +85,8 @@ impl<'a, 'b> Dispatcher<'a, 'b> {
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct SystemId(pub usize);

pub struct SystemInfo<'a> {
dependencies: SystemDependencies,
id: SystemId,
reads: SystemReads,
system: SystemExecSend<'a>,
writes: SystemWrites,
}

impl<'a> SystemInfo<'a> {
pub fn new(dependencies: SystemDependencies,
id: SystemId,
reads: SystemReads,
system: SystemExecSend<'a>,
writes: SystemWrites)
-> Self {
SystemInfo {
dependencies,
id,
reads,
system,
writes,
}
}
}

type SystemDependencies = SmallVec<[SystemId; 8]>;
type SystemExec<'b> = Box<for<'a> RunNow<'a> + 'b>;
type SystemExecSend<'b> = Box<for<'a> RunNow<'a> + Send + 'b>;
type SystemReads = SmallVec<[ResourceId; 12]>;
type SystemWrites = SmallVec<[ResourceId; 12]>;

type ThreadLocal<'a> = SmallVec<[SystemExec<'a>; 4]>;
type ThreadLocal<'a> = SmallVec<[Box<for<'b> RunNow<'b> + 'a>; 4]>;

#[cfg(test)]
mod tests {
Expand Down
Loading

0 comments on commit 457f86c

Please sign in to comment.