-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
413 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use criterion::{black_box, criterion_group, criterion_main, Criterion}; | ||
use mpz_common::{ | ||
executor::{test_mt_executor, test_st_executor}, | ||
Context, | ||
}; | ||
use pollster::block_on; | ||
use scoped_futures::ScopedFutureExt; | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("context"); | ||
|
||
// Measures the overhead of making a `Context::blocking` call, which | ||
// moves the context to a worker thread and back. | ||
group.bench_function("st/blocking", |b| { | ||
let (mut ctx, _) = test_st_executor(1024); | ||
b.iter(|| { | ||
block_on(async { | ||
ctx.blocking(|ctx| { | ||
async move { | ||
black_box(ctx.id()); | ||
} | ||
.scope_boxed() | ||
}) | ||
.await | ||
.unwrap(); | ||
}); | ||
}) | ||
}); | ||
|
||
// Measures the overhead of making a `Context::blocking` call, which | ||
// moves the context to a worker thread and back. | ||
group.bench_function("mt/blocking", |b| { | ||
let (mut exec_a, _) = test_mt_executor(8); | ||
|
||
let mut ctx = block_on(exec_a.new_thread()).unwrap(); | ||
|
||
b.iter(|| { | ||
block_on(async { | ||
ctx.blocking(|ctx| { | ||
async move { | ||
black_box(ctx.id()); | ||
} | ||
.scope_boxed() | ||
}) | ||
.await | ||
.unwrap(); | ||
}); | ||
}) | ||
}); | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
//! CPU backend shim. | ||
|
||
use cfg_if::cfg_if; | ||
|
||
cfg_if! { | ||
if #[cfg(feature = "force-st")] { | ||
pub use st::SingleThreadedBackend as CpuBackend; | ||
} else if #[cfg(feature = "rayon")] { | ||
pub use rayon_backend::RayonBackend as CpuBackend; | ||
} else { | ||
pub use st::SingleThreadedBackend as CpuBackend; | ||
} | ||
} | ||
|
||
#[cfg(any(feature = "force-st", not(feature = "rayon")))] | ||
mod st { | ||
use futures::Future; | ||
|
||
/// A single-threaded CPU backend. | ||
#[derive(Debug)] | ||
pub struct SingleThreadedBackend; | ||
|
||
impl SingleThreadedBackend { | ||
/// Execute a future on the CPU backend. | ||
#[inline] | ||
pub fn blocking_async<F>(fut: F) -> impl Future<Output = F::Output> + Send | ||
where | ||
F: Future + Send + 'static, | ||
F::Output: Send, | ||
{ | ||
fut | ||
} | ||
|
||
/// Execute a closure on the CPU backend. | ||
#[inline] | ||
pub fn blocking<F, R>(f: F) -> impl Future<Output = R> + Send | ||
where | ||
F: FnOnce() -> R + Send + 'static, | ||
R: Send + 'static, | ||
{ | ||
async move { f() } | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use pollster::block_on; | ||
|
||
#[test] | ||
fn test_st_backend_blocking() { | ||
let output = block_on(SingleThreadedBackend::blocking(|| 42)); | ||
assert_eq!(output, 42); | ||
} | ||
|
||
#[test] | ||
fn test_st_backend_blocking_async() { | ||
let output = block_on(SingleThreadedBackend::blocking_async(async { 42 })); | ||
assert_eq!(output, 42); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(all(feature = "rayon", not(feature = "force-st")))] | ||
mod rayon_backend { | ||
use futures::{channel::oneshot, Future}; | ||
use pollster::block_on; | ||
|
||
/// A Rayon CPU backend. | ||
#[derive(Debug)] | ||
pub struct RayonBackend; | ||
|
||
impl RayonBackend { | ||
/// Execute a future on the CPU backend. | ||
pub fn blocking_async<F>(fut: F) -> impl Future<Output = F::Output> + Send | ||
where | ||
F: Future + Send + 'static, | ||
F::Output: Send, | ||
{ | ||
async move { | ||
let (sender, receiver) = oneshot::channel(); | ||
rayon::spawn(move || { | ||
let output = block_on(fut); | ||
_ = sender.send(output); | ||
}); | ||
receiver.await.expect("worker thread does not drop channel") | ||
} | ||
} | ||
|
||
/// Execute a closure on the CPU backend. | ||
pub fn blocking<F, R>(f: F) -> impl Future<Output = R> + Send | ||
where | ||
F: FnOnce() -> R + Send + 'static, | ||
R: Send + 'static, | ||
{ | ||
async move { | ||
let (sender, receiver) = oneshot::channel(); | ||
rayon::spawn(move || { | ||
_ = sender.send(f()); | ||
}); | ||
receiver.await.expect("worker thread does not drop channel") | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_rayon_backend_blocking() { | ||
let output = block_on(RayonBackend::blocking(|| 42)); | ||
assert_eq!(output, 42); | ||
} | ||
|
||
#[test] | ||
fn test_rayon_backend_blocking_async() { | ||
let output = block_on(RayonBackend::blocking_async(async { 42 })); | ||
assert_eq!(output, 42); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.