Skip to content

Commit

Permalink
Fix benchmarks and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
james7132 committed Apr 13, 2024
1 parent db9afd7 commit 281b601
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
55 changes: 29 additions & 26 deletions benches/executor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::thread::available_parallelism;

use async_executor::{Executor, LeakedExecutor};
use async_executor::{Executor, StaticExecutor};
use criterion::{criterion_group, criterion_main, Criterion};
use futures_lite::{future, prelude::*};

Expand All @@ -26,7 +26,7 @@ fn run(f: impl FnOnce(), multithread: bool) {
});
}

fn run_leaked(executor: LeakedExecutor, f: impl FnOnce(), multithread: bool) {
fn run_static(executor: StaticExecutor, f: impl FnOnce(), multithread: bool) {
let limit = if multithread {
available_parallelism().unwrap().get()
} else {
Expand All @@ -53,23 +53,23 @@ fn create(c: &mut Criterion) {
}

fn running_benches(c: &mut Criterion) {
let leaked = Executor::new().leak();
for with_leaked in [false, true] {
let static_executor = Executor::new().leak();
for with_static in [false, true] {
for (group_name, multithread) in [("single_thread", false), ("multi_thread", true)].iter() {
let prefix = if with_leaked {
"leaked_executor"
let prefix = if with_static {
"static_executor"
} else {
"executor"
};
let mut group = c.benchmark_group(group_name.to_string());

group.bench_function(format!("{}::spawn_one", prefix), |b| {
if with_leaked {
run_leaked(
leaked,
if with_static {
run_static(
static_executor,
|| {
b.iter(|| {
future::block_on(async { leaked.spawn(async {}).await });
future::block_on(async { static_executor.spawn(async {}).await });
});
},
*multithread,
Expand All @@ -86,7 +86,7 @@ fn running_benches(c: &mut Criterion) {
}
});

if !with_leaked {
if !with_static {
group.bench_function("executor::spawn_batch", |b| {
run(
|| {
Expand All @@ -104,15 +104,15 @@ fn running_benches(c: &mut Criterion) {
}

group.bench_function(format!("{}::spawn_many_local", prefix), |b| {
if with_leaked {
run_leaked(
leaked,
if with_static {
run_static(
static_executor,
|| {
b.iter(move || {
future::block_on(async {
let mut tasks = Vec::new();
for _ in 0..LIGHT_TASKS {
tasks.push(leaked.spawn(async {}));
tasks.push(static_executor.spawn(async {}));
}
for task in tasks {
task.await;
Expand Down Expand Up @@ -157,31 +157,34 @@ fn running_benches(c: &mut Criterion) {
}

#[allow(clippy::manual_async_fn)]
fn go_leaked(
executor: LeakedExecutor,
fn go_static(
executor: StaticExecutor,
i: usize,
) -> impl Future<Output = ()> + Send + 'static {
async move {
if i != 0 {
executor
.spawn(async move {
let fut = go_leaked(executor, i - 1).boxed();
let fut = go_static(executor, i - 1).boxed();
fut.await;
})
.await;
}
}
}

if with_leaked {
run_leaked(
leaked,
if with_static {
run_static(
static_executor,
|| {
b.iter(move || {
future::block_on(async {
let mut tasks = Vec::new();
for _ in 0..TASKS {
tasks.push(leaked.spawn(go_leaked(leaked, STEPS)));
tasks.push(
static_executor
.spawn(go_static(static_executor, STEPS)),
);
}
for task in tasks {
task.await;
Expand Down Expand Up @@ -212,15 +215,15 @@ fn running_benches(c: &mut Criterion) {
});

group.bench_function(format!("{}::yield_now", prefix), |b| {
if with_leaked {
run_leaked(
leaked,
if with_static {
run_static(
static_executor,
|| {
b.iter(move || {
future::block_on(async {
let mut tasks = Vec::new();
for _ in 0..TASKS {
tasks.push(leaked.spawn(async move {
tasks.push(static_executor.spawn(async move {
for _ in 0..STEPS {
future::yield_now().await;
}
Expand Down
20 changes: 10 additions & 10 deletions src/leaked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use crate::{debug_state, Executor, LocalExecutor, State};
use async_task::{Builder, Runnable, Task};
use slab::Slab;
use std::{
cell::UnsafeCell,
fmt,
future::Future,
panic::{RefUnwindSafe, UnwindSafe},
marker::PhantomData,
cell::UnsafeCell,
panic::{RefUnwindSafe, UnwindSafe},
};

impl Executor<'static> {
Expand Down Expand Up @@ -93,7 +93,10 @@ impl LocalExecutor<'static> {
*active = Slab::new();
}

StaticLocalExecutor { state, marker_: PhantomData }
StaticLocalExecutor {
state,
marker_: PhantomData,
}
}
}

Expand Down Expand Up @@ -299,10 +302,7 @@ impl StaticLocalExecutor {
/// println!("Hello world");
/// });
/// ```
pub fn spawn<T: 'static>(
&self,
future: impl Future<Output = T> + 'static,
) -> Task<T> {
pub fn spawn<T: 'static>(&self, future: impl Future<Output = T> + 'static) -> Task<T> {
let (runnable, task) = Builder::new()
.propagate_panic(true)
.spawn_local(|()| future, self.schedule());
Expand All @@ -324,8 +324,8 @@ impl StaticLocalExecutor {
//
// - `future` is not `Send` but `StaticLocalExecutor` is `!Sync`,
// `try_tick`, `tick` and `run` can only be called from the origin
// thread of the `StaticLocalExecutor`. Similarly, `spawn_scoped` can only
// be called from the origin thread, ensuring that `future` and the executor
// thread of the `StaticLocalExecutor`. Similarly, `spawn_scoped` can only
// be called from the origin thread, ensuring that `future` and the executor
// share the same origin thread. The `Runnable` can be scheduled from other
// threads, but because of the above `Runnable` can only be called or
// dropped on the origin thread.
Expand Down Expand Up @@ -415,4 +415,4 @@ impl StaticLocalExecutor {
state.notify();
}
}
}
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
html_logo_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"
)]
#[cfg_attr(docsrs, feature(doc_auto_cfg))]

use std::fmt;
use std::marker::PhantomData;
use std::panic::{RefUnwindSafe, UnwindSafe};
Expand Down

0 comments on commit 281b601

Please sign in to comment.