-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(s2n-quic-core): add spsc channel (#1614)
- Loading branch information
Showing
24 changed files
with
1,829 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -682,3 +682,30 @@ jobs: | |
name: "dhat / report" | ||
status: "success" | ||
url: "${{ steps.s3.outputs.URL }}" | ||
|
||
loom: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
crate: [quic/s2n-quic-core] | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
submodules: true | ||
|
||
- uses: actions-rs/[email protected] | ||
id: toolchain | ||
with: | ||
toolchain: stable | ||
profile: minimal | ||
override: true | ||
|
||
- uses: camshaft/rust-cache@v1 | ||
with: | ||
key: ${{ matrix.crate }} | ||
|
||
- name: ${{ matrix.crate }} | ||
# run the tests with release mode since some of the loom models can be expensive | ||
run: cd ${{ matrix.crate }} && cargo test --release loom | ||
env: | ||
RUSTFLAGS: --cfg loom -Cdebug-assertions |
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,80 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use criterion::{BenchmarkId, Criterion, Throughput}; | ||
use crossbeam_channel::bounded; | ||
use s2n_quic_core::sync::spsc; | ||
|
||
pub fn benchmarks(c: &mut Criterion) { | ||
spsc_benches(c); | ||
} | ||
|
||
fn spsc_benches(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("spsc"); | ||
|
||
for i in [1, 64, 1024, 4096] { | ||
group.throughput(Throughput::Elements(i as _)); | ||
group.bench_with_input(BenchmarkId::new("s2n/send_recv", i), &i, |b, input| { | ||
let (mut sender, mut receiver) = spsc::channel(*input); | ||
b.iter(|| { | ||
{ | ||
let mut slice = sender.try_slice().unwrap().unwrap(); | ||
while slice.push(123usize).is_ok() {} | ||
} | ||
|
||
{ | ||
let mut slice = receiver.try_slice().unwrap().unwrap(); | ||
while slice.pop().is_some() {} | ||
} | ||
}); | ||
}); | ||
group.bench_with_input( | ||
BenchmarkId::new("crossbeam/send_recv", i), | ||
&i, | ||
|b, input| { | ||
let (sender, receiver) = bounded(*input); | ||
b.iter(|| { | ||
{ | ||
while sender.try_send(123usize).is_ok() {} | ||
} | ||
|
||
{ | ||
while receiver.try_recv().is_ok() {} | ||
} | ||
}); | ||
}, | ||
); | ||
|
||
group.bench_with_input(BenchmarkId::new("s2n/send_recv_iter", i), &i, |b, input| { | ||
let (mut sender, mut receiver) = spsc::channel(*input); | ||
b.iter(|| { | ||
{ | ||
let mut slice = sender.try_slice().unwrap().unwrap(); | ||
let _ = slice.extend(&mut core::iter::repeat(123usize)); | ||
} | ||
|
||
{ | ||
let mut slice = receiver.try_slice().unwrap().unwrap(); | ||
slice.clear(); | ||
} | ||
}); | ||
}); | ||
group.bench_with_input( | ||
BenchmarkId::new("crossbeam/send_recv_iter", i), | ||
&i, | ||
|b, input| { | ||
let (sender, receiver) = bounded(*input); | ||
b.iter(|| { | ||
{ | ||
while sender.try_send(123usize).is_ok() {} | ||
} | ||
|
||
{ | ||
for _ in receiver.try_iter() {} | ||
} | ||
}); | ||
}, | ||
); | ||
} | ||
group.finish(); | ||
} |
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
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,5 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#[cfg(feature = "alloc")] | ||
pub mod spsc; |
3 changes: 3 additions & 0 deletions
3
quic/s2n-quic-core/src/sync/__fuzz___/sync__spsc__tests__model/corpus.tar.gz
Git LFS file not shown
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,42 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
mod primitive; | ||
mod recv; | ||
mod send; | ||
mod slice; | ||
mod state; | ||
|
||
use slice::*; | ||
use state::*; | ||
|
||
pub use recv::{Receiver, RecvSlice}; | ||
pub use send::{SendSlice, Sender}; | ||
|
||
#[inline] | ||
pub fn channel<T>(capacity: usize) -> (Sender<T>, Receiver<T>) { | ||
let state = State::new(capacity); | ||
let sender = Sender(state.clone()); | ||
let receiver = Receiver(state); | ||
(sender, receiver) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
type Result<T, E = ClosedError> = core::result::Result<T, E>; | ||
|
||
#[derive(Clone, Copy, Debug)] | ||
pub struct ClosedError; | ||
|
||
#[derive(Clone, Copy, Debug)] | ||
pub enum PushError<T> { | ||
Full(T), | ||
Closed, | ||
} | ||
|
||
impl<T> From<ClosedError> for PushError<T> { | ||
fn from(_error: ClosedError) -> Self { | ||
Self::Closed | ||
} | ||
} |
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 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#[cfg(all(loom, test))] | ||
mod loom { | ||
use ::core::task::Waker; | ||
use ::loom::future::AtomicWaker as Inner; | ||
|
||
pub use ::loom::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct AtomicWaker(Inner); | ||
|
||
impl AtomicWaker { | ||
pub fn new() -> Self { | ||
Self(Inner::new()) | ||
} | ||
|
||
pub fn wake(&self) { | ||
self.0.wake(); | ||
} | ||
|
||
pub fn take(&self) -> Option<Waker> { | ||
self.0.take_waker() | ||
} | ||
|
||
pub fn register(&self, waker: &Waker) { | ||
self.0.register_by_ref(waker); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(all(loom, test))] | ||
pub use self::loom::*; | ||
|
||
mod core { | ||
pub use ::core::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; | ||
pub use atomic_waker::AtomicWaker; | ||
} | ||
|
||
#[cfg(not(all(loom, test)))] | ||
pub use self::core::*; | ||
|
||
/// Indicates if the type is a zero-sized type | ||
/// | ||
/// This can be used to optimize the code to avoid needless calculations. | ||
pub trait IsZst { | ||
const IS_ZST: bool; | ||
} | ||
|
||
impl<T> IsZst for T { | ||
const IS_ZST: bool = ::core::mem::size_of::<T>() == 0; | ||
} |
Oops, something went wrong.