Skip to content

Commit 1170966

Browse files
committed
Directly schedule tasks instead of using Runnable::schedule
1 parent c10412c commit 1170966

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ exclude = ["/.*"]
1818
# Adds support for executors optimized for use in static variables.
1919
static = []
2020

21+
[lib]
22+
bench = false
23+
2124
[dependencies]
2225
async-task = "4.4.0"
2326
concurrent-queue = "2.5.0"

src/lib.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,10 @@ impl<'a> Executor<'a> {
282282
.spawn_unchecked(|()| future, Self::schedule(state));
283283
entry.insert(runnable.waker());
284284

285-
runnable.schedule();
285+
// `Runnable::schedule` has an extra clone/drop of the Waker, which can
286+
// be skipped by directly scheduling instead of calling `Runnable::schedule`.
287+
Self::schedule_runnable(&state, runnable);
288+
286289
task
287290
}
288291

@@ -353,12 +356,16 @@ impl<'a> Executor<'a> {
353356
fn schedule(state: Pin<&'a State>) -> impl Fn(Runnable) + Send + Sync + 'a {
354357
// TODO: If possible, push into the current local queue and notify the ticker.
355358
move |runnable| {
356-
let result = state.queue.push(runnable);
357-
debug_assert!(result.is_ok()); // Since we use unbounded queue, push will never fail.
358-
state.notify();
359+
Self::schedule_runnable(&state, runnable);
359360
}
360361
}
361362

363+
fn schedule_runnable(state: &State, runnable: Runnable) {
364+
let result = state.queue.push(runnable);
365+
debug_assert!(result.is_ok()); // Since we use unbounded queue, push will never fail.
366+
state.notify();
367+
}
368+
362369
/// Returns a pointer to the inner state.
363370
#[inline]
364371
fn state(&self) -> Pin<&'a State> {

src/static_executors.rs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,11 @@ impl StaticExecutor {
192192
let (runnable, task) = Builder::new()
193193
.propagate_panic(true)
194194
.spawn(|()| future, self.schedule());
195-
runnable.schedule();
195+
196+
// `Runnable::schedule` has an extra clone/drop of the Waker, which can
197+
// be skipped by directly scheduling instead of calling `Runnable::schedule`.
198+
Self::schedule_runnable(&self.state, runnable);
199+
196200
task
197201
}
198202

@@ -219,7 +223,11 @@ impl StaticExecutor {
219223
.propagate_panic(true)
220224
.spawn_unchecked(|()| future, self.schedule())
221225
};
222-
runnable.schedule();
226+
227+
// `Runnable::schedule` has an extra clone/drop of the Waker, which can
228+
// be skipped by directly scheduling instead of calling `Runnable::schedule`.
229+
Self::schedule_runnable(&self.state, runnable);
230+
223231
task
224232
}
225233

@@ -294,11 +302,16 @@ impl StaticExecutor {
294302
let state: &'static State = &self.state;
295303
// TODO: If possible, push into the current local queue and notify the ticker.
296304
move |runnable| {
297-
let result = state.queue.push(runnable);
298-
debug_assert!(result.is_ok()); // Since we use unbounded queue, push will never fail.
299-
state.notify();
305+
Self::schedule_runnable(state, runnable);
300306
}
301307
}
308+
309+
#[inline]
310+
fn schedule_runnable(state: &'static State, runnable: Runnable) {
311+
let result = state.queue.push(runnable);
312+
debug_assert!(result.is_ok()); // Since we use unbounded queue, push will never fail.
313+
state.notify();
314+
}
302315
}
303316

304317
impl Default for StaticExecutor {
@@ -375,7 +388,11 @@ impl StaticLocalExecutor {
375388
let (runnable, task) = Builder::new()
376389
.propagate_panic(true)
377390
.spawn_local(|()| future, self.schedule());
378-
runnable.schedule();
391+
392+
// `Runnable::schedule` has an extra clone/drop of the Waker, which can
393+
// be skipped by directly scheduling instead of calling `Runnable::schedule`.
394+
Self::schedule_runnable(&self.state, runnable);
395+
379396
task
380397
}
381398

@@ -408,7 +425,11 @@ impl StaticLocalExecutor {
408425
.propagate_panic(true)
409426
.spawn_unchecked(|()| future, self.schedule())
410427
};
411-
runnable.schedule();
428+
429+
// `Runnable::schedule` has an extra clone/drop of the Waker, which can
430+
// be skipped by directly scheduling instead of calling `Runnable::schedule`.
431+
Self::schedule_runnable(&self.state, runnable);
432+
412433
task
413434
}
414435

@@ -480,11 +501,16 @@ impl StaticLocalExecutor {
480501
let state: &'static State = &self.state;
481502
// TODO: If possible, push into the current local queue and notify the ticker.
482503
move |runnable| {
483-
let result = state.queue.push(runnable);
484-
debug_assert!(result.is_ok()); // Since we use unbounded queue, push will never fail.
485-
state.notify();
504+
Self::schedule_runnable(state, runnable);
486505
}
487506
}
507+
508+
#[inline]
509+
fn schedule_runnable(state: &'static State, runnable: Runnable) {
510+
let result = state.queue.push(runnable);
511+
debug_assert!(result.is_ok()); // Since we use unbounded queue, push will never fail.
512+
state.notify();
513+
}
488514
}
489515

490516
impl Default for StaticLocalExecutor {

0 commit comments

Comments
 (0)