From 45adcec2d42ad83aa58af5ddb7beca4cf266c494 Mon Sep 17 00:00:00 2001 From: Zachary Dremann Date: Mon, 29 May 2023 21:43:51 -0400 Subject: [PATCH 1/5] The size of the waker has decreased --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 94bb35d..7828e9f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1178,7 +1178,7 @@ fn receiver_waker_size() { (false, false) => 0, (false, true) => 16, (true, false) => 8, - (true, true) => 24, + (true, true) => 16, }; assert_eq!(mem::size_of::(), expected); } From ef0a40504ab1468bb0507cd0187106d0d2f55576 Mon Sep 17 00:00:00 2001 From: Zachary Dremann Date: Mon, 29 May 2023 23:02:09 -0400 Subject: [PATCH 2/5] Cleanup some benchmarks --- benches/benches.rs | 124 +++++++++++++++++++++++++++++---------------- 1 file changed, 81 insertions(+), 43 deletions(-) diff --git a/benches/benches.rs b/benches/benches.rs index 438d46a..0b525f1 100644 --- a/benches/benches.rs +++ b/benches/benches.rs @@ -1,74 +1,112 @@ -use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use criterion::{black_box, criterion_group, criterion_main, Criterion, BenchmarkGroup, BenchmarkId, Throughput, BatchSize}; use std::mem; use std::time::{Duration, Instant}; +use criterion::measurement::WallTime; criterion_group!(benches, bench); criterion_main!(benches); macro_rules! bench_send_and_recv { - ($c:expr, $($type:ty => $value:expr);+) => { + ($c:expr; $($size:expr),+ $(,)?) => { // Sanity check that all $values are of $type. - $(let _: $type = $value;)* { let mut group = $c.benchmark_group("create_channel"); - $(group.bench_function(stringify!($type), |b| { - b.iter(oneshot::channel::<$type>) - });)* + $(create::<$size>(&mut group);)+ group.finish(); } { - let mut group = $c.benchmark_group("create_and_send"); - $(group.bench_function(stringify!($type), |b| { - b.iter(|| { - let (sender, _receiver) = oneshot::channel(); - sender.send(black_box($value)).unwrap() - }); - });)* + let mut group = $c.benchmark_group("send"); + $(send::<$size>(&mut group);)+ group.finish(); } { - let mut group = $c.benchmark_group("create_and_send_on_closed"); - $(group.bench_function(stringify!($type), |b| { - b.iter(|| { - let (sender, _) = oneshot::channel(); - sender.send(black_box($value)).unwrap_err() - }); - });)* + let mut group = $c.benchmark_group("send_on_closed"); + $(send_on_closed::<$size>(&mut group);)+ group.finish(); } { - let mut group = $c.benchmark_group("create_send_and_recv"); - $(group.bench_function(stringify!($type), |b| { - b.iter(|| { - let (sender, receiver) = oneshot::channel(); - sender.send(black_box($value)).unwrap(); - receiver.recv().unwrap() - }); - });)* + let mut group = $c.benchmark_group("send_and_recv"); + $(send_and_recv::<$size>(&mut group);)+ group.finish(); } { - let mut group = $c.benchmark_group("create_send_and_recv_ref"); - $(group.bench_function(stringify!($type), |b| { - b.iter(|| { - let (sender, receiver) = oneshot::channel(); - sender.send(black_box($value)).unwrap(); - receiver.recv_ref().unwrap() - }); - });)* + let mut group = $c.benchmark_group("send_and_recv_ref"); + $(send_and_recv_ref::<$size>(&mut group);)+ group.finish(); } }; } +fn create(group: &mut BenchmarkGroup) { + group.bench_with_input(BenchmarkId::from_parameter(N), &N, |b, _| { + b.iter(oneshot::channel::<[u8; N]>); + }); +} + +fn send(group: &mut BenchmarkGroup) { + group.throughput(Throughput::Bytes(N as u64)); + group.bench_with_input(BenchmarkId::from_parameter(N), &N, |b, _| { + b.iter_batched( + || oneshot::channel::<[u8; N]>(), + |(sender, _receiver)| { + sender.send(black_box([0b10101010u8; N])).unwrap() + }, + BatchSize::SmallInput, + ); + }); +} + +fn send_on_closed(group: &mut BenchmarkGroup) { + group.throughput(Throughput::Bytes(N as u64)); + group.bench_with_input(BenchmarkId::from_parameter(N), &N, |b, _| { + b.iter_batched( + || oneshot::channel::<[u8; N]>(), + |(sender, receiver)| { + drop(receiver); + sender.send(black_box([0b10101010u8; N])).unwrap_err() + }, + BatchSize::SmallInput, + ); + }); +} + +fn send_and_recv(group: &mut BenchmarkGroup) { + group.throughput(Throughput::Bytes(N as u64)); + group.bench_with_input(BenchmarkId::from_parameter(N), &N, |b, _| { + b.iter_batched( + || oneshot::channel::<[u8; N]>(), + |(sender, receiver)| { + sender.send(black_box([0b10101010u8; N])).unwrap(); + receiver.recv().unwrap() + }, + BatchSize::SmallInput, + ); + }); +} + +fn send_and_recv_ref(group: &mut BenchmarkGroup) { + group.throughput(Throughput::Bytes(N as u64)); + group.bench_with_input(BenchmarkId::from_parameter(N), &N, |b, _| { + b.iter_batched( + || oneshot::channel::<[u8; N]>(), + |(sender, receiver)| { + sender.send(black_box([0b10101010u8; N])).unwrap(); + receiver.recv_ref().unwrap() + }, + BatchSize::SmallInput, + ); + }); +} + fn bench(c: &mut Criterion) { - bench_send_and_recv!(c, - () => (); - u8 => 7u8; - usize => 9876usize; - u128 => 1234567u128; - [u8; 64] => [0b10101010u8; 64]; - [u8; 4096] => [0b10101010u8; 4096] + bench_send_and_recv!(c; + 0, + 1, + 8, + 16, + 64, + 1024, + 4096, ); bench_try_recv(c); From 4f2ca409eca2f4b83853868888570736cd6646cc Mon Sep 17 00:00:00 2001 From: Zachary Dremann Date: Mon, 29 May 2023 23:13:39 -0400 Subject: [PATCH 3/5] Bump criterion version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3add8cb..65bfdb2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ async = [] loom = { version = "0.5.3", features = ["futures"] } [dev-dependencies] -criterion = "0.3" +criterion = { version = "0.5", features = ["html_reports"] } [target.'cfg(not(loom))'.dev-dependencies] tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros", "time"] } From 7387b4fcb8ed5d70bba0c7d1e1d741fa6996e814 Mon Sep 17 00:00:00 2001 From: Zachary Dremann Date: Mon, 29 May 2023 23:21:19 -0400 Subject: [PATCH 4/5] Add a recv only successful benchmark --- benches/benches.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/benches/benches.rs b/benches/benches.rs index 0b525f1..5a24482 100644 --- a/benches/benches.rs +++ b/benches/benches.rs @@ -34,6 +34,11 @@ macro_rules! bench_send_and_recv { $(send_and_recv_ref::<$size>(&mut group);)+ group.finish(); } + { + let mut group = $c.benchmark_group("recv"); + $(recv::<$size>(&mut group);)+ + group.finish(); + } }; } @@ -98,6 +103,21 @@ fn send_and_recv_ref(group: &mut BenchmarkGroup) { }); } +fn recv(group: &mut BenchmarkGroup) { + group.throughput(Throughput::Bytes(N as u64)); + group.bench_with_input(BenchmarkId::from_parameter(N), &N, |b, _| { + b.iter_batched( + || { + let (sender, receiver) = oneshot::channel::<[u8; N]>(); + sender.send(black_box([0b10101010u8; N])).unwrap(); + receiver + }, + |receiver| receiver.recv().unwrap(), + BatchSize::SmallInput, + ); + }); +} + fn bench(c: &mut Criterion) { bench_send_and_recv!(c; 0, From 879e3de43960ef2561852746b5683138f70a2e35 Mon Sep 17 00:00:00 2001 From: Zachary Dremann Date: Tue, 30 May 2023 00:07:43 -0400 Subject: [PATCH 5/5] Implment Sync for Sender/Receiver --- src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 7828e9f..064ac33 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -228,6 +228,8 @@ pub struct Receiver { unsafe impl Send for Sender {} unsafe impl Send for Receiver {} +unsafe impl Sync for Sender {} +unsafe impl Sync for Receiver {} impl Unpin for Receiver {} impl Sender {