Skip to content

Commit 3f15a49

Browse files
committed
Re-use SendDispatcher in Dispatcher to reduce code duplication
1 parent 188e064 commit 3f15a49

File tree

1 file changed

+16
-53
lines changed

1 file changed

+16
-53
lines changed

src/dispatch/dispatcher.rs

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,15 @@ pub type ThreadPoolWrapper = Option<::std::sync::Arc<::rayon::ThreadPool>>;
1414
/// The dispatcher struct, allowing
1515
/// systems to be executed in parallel.
1616
pub struct Dispatcher<'a, 'b> {
17-
stages: Vec<Stage<'a>>,
17+
inner: SendDispatcher<'a>,
1818
thread_local: ThreadLocal<'b>,
19-
#[cfg(feature = "parallel")]
20-
thread_pool: ::std::sync::Arc<::std::sync::RwLock<ThreadPoolWrapper>>,
2119
}
2220

2321
impl<'a, 'b> Dispatcher<'a, 'b> {
2422
/// Sets up all the systems which means they are gonna add default values
2523
/// for the resources they need.
2624
pub fn setup(&mut self, world: &mut World) {
27-
for stage in &mut self.stages {
28-
stage.setup(world);
29-
}
25+
self.inner.setup(world);
3026

3127
for sys in &mut self.thread_local {
3228
sys.setup(world);
@@ -38,9 +34,7 @@ impl<'a, 'b> Dispatcher<'a, 'b> {
3834
/// / or resources from the `World` which are associated with external
3935
/// resources.
4036
pub fn dispose(self, world: &mut World) {
41-
for stage in self.stages {
42-
stage.dispose(world);
43-
}
37+
self.inner.dispose(world);
4438

4539
for sys in self.thread_local {
4640
sys.dispose(world);
@@ -60,12 +54,7 @@ impl<'a, 'b> Dispatcher<'a, 'b> {
6054
/// Please note that this method assumes that no resource
6155
/// is currently borrowed. If that's the case, it panics.
6256
pub fn dispatch(&mut self, world: &World) {
63-
#[cfg(feature = "parallel")]
64-
self.dispatch_par(world);
65-
66-
#[cfg(not(feature = "parallel"))]
67-
self.dispatch_seq(world);
68-
57+
self.inner.dispatch(world);
6958
self.dispatch_thread_local(world);
7059
}
7160

@@ -81,18 +70,7 @@ impl<'a, 'b> Dispatcher<'a, 'b> {
8170
/// is currently borrowed. If that's the case, it panics.
8271
#[cfg(feature = "parallel")]
8372
pub fn dispatch_par(&mut self, world: &World) {
84-
let stages = &mut self.stages;
85-
86-
self.thread_pool
87-
.read()
88-
.unwrap()
89-
.as_ref()
90-
.unwrap()
91-
.install(move || {
92-
for stage in stages {
93-
stage.execute(world);
94-
}
95-
});
73+
self.inner.dispatch_par(world);
9674
}
9775

9876
/// Dispatches the systems (except thread local systems) sequentially.
@@ -103,9 +81,7 @@ impl<'a, 'b> Dispatcher<'a, 'b> {
10381
/// Please note that this method assumes that no resource
10482
/// is currently borrowed. If that's the case, it panics.
10583
pub fn dispatch_seq(&mut self, world: &World) {
106-
for stage in &mut self.stages {
107-
stage.execute_seq(world);
108-
}
84+
self.inner.dispatch_seq(world);
10985
}
11086

11187
/// Dispatch only thread local systems sequentially.
@@ -123,25 +99,14 @@ impl<'a, 'b> Dispatcher<'a, 'b> {
12399
/// Fails and returns the original distpatcher if it contains thread local systems.
124100
pub fn try_into_sendable(self) -> Result<SendDispatcher<'a>, Self> {
125101
let Dispatcher {
126-
stages,
102+
inner: _,
127103
thread_local,
128-
#[cfg(feature = "parallel")]
129-
thread_pool,
130-
} = self;
104+
} = &self;
131105

132106
if thread_local.is_empty() {
133-
Ok(SendDispatcher {
134-
stages,
135-
#[cfg(feature = "parallel")]
136-
thread_pool,
137-
})
107+
Ok(self.inner)
138108
} else {
139-
Err(Dispatcher {
140-
stages,
141-
thread_local,
142-
#[cfg(feature = "parallel")]
143-
thread_pool,
144-
})
109+
Err(self)
145110
}
146111
}
147112

@@ -150,11 +115,7 @@ impl<'a, 'b> Dispatcher<'a, 'b> {
150115
/// how well your systems can make use of multi-threading.
151116
#[cfg(feature = "parallel")]
152117
pub fn max_threads(&self) -> usize {
153-
self.stages
154-
.iter()
155-
.map(Stage::max_threads)
156-
.max()
157-
.unwrap_or(0)
118+
self.inner.max_threads()
158119
}
159120
}
160121

@@ -185,9 +146,11 @@ pub fn new_dispatcher<'a, 'b>(
185146
thread_pool: ::std::sync::Arc<::std::sync::RwLock<ThreadPoolWrapper>>,
186147
) -> Dispatcher<'a, 'b> {
187148
Dispatcher {
188-
stages,
149+
inner: SendDispatcher {
150+
stages,
151+
thread_pool,
152+
},
189153
thread_local,
190-
thread_pool,
191154
}
192155
}
193156

@@ -197,7 +160,7 @@ pub fn new_dispatcher<'a, 'b>(
197160
thread_local: ThreadLocal<'b>,
198161
) -> Dispatcher<'a, 'b> {
199162
Dispatcher {
200-
stages,
163+
inner: SendDispatcher { stages },
201164
thread_local,
202165
}
203166
}

0 commit comments

Comments
 (0)