Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite BiLock lock/unlock to be allocation free #2384

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
108 changes: 108 additions & 0 deletions futures-util/benches/bilock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#![feature(test)]

#[cfg(feature = "bilock")]
mod bench {
use futures::executor::block_on;
use futures::task::Poll;
use futures_test::task::noop_context;
use futures_util::lock::BiLock;

use std::mem::drop;
extern crate test;
use test::Bencher;

#[bench]
fn contended(b: &mut Bencher) {
let mut ctx = noop_context();

b.iter(|| {
let (mut x, mut y) = BiLock::new(1);

for _ in 0..1000 {
let x_guard = match x.poll_lock(&mut ctx) {
Poll::Ready(guard) => guard,
_ => panic!(),
};

// Try poll second lock while first lock still holds the lock
match y.poll_lock(&mut ctx) {
Poll::Pending => (),
_ => panic!(),
};

drop(x_guard);

let y_guard = match y.poll_lock(&mut ctx) {
Poll::Ready(guard) => guard,
_ => panic!(),
};

drop(y_guard);
}
(x, y)
});
}

#[bench]
fn lock_unlock(b: &mut Bencher) {
let mut ctx = noop_context();

b.iter(|| {
let (mut x, mut y) = BiLock::new(1);

for _ in 0..1000 {
let x_guard = match x.poll_lock(&mut ctx) {
Poll::Ready(guard) => guard,
_ => panic!(),
};

drop(x_guard);

let y_guard = match y.poll_lock(&mut ctx) {
Poll::Ready(guard) => guard,
_ => panic!(),
};

drop(y_guard);
}
(x, y)
})
}

#[bench]
fn concurrent(b: &mut Bencher) {
use std::thread;

b.iter(|| {
let (mut x, mut y) = BiLock::new(false);
const ITERATION_COUNT: usize = 1000;

let a = thread::spawn(move || {
let mut count = 0;
while count < ITERATION_COUNT {
let mut guard = block_on(x.lock());
if *guard {
*guard = false;
count += 1;
}
x = guard.unlock();
}
});

let b = thread::spawn(move || {
let mut count = 0;
while count < ITERATION_COUNT {
let mut guard = block_on(y.lock());
if !*guard {
*guard = true;
count += 1;
}
y = guard.unlock();
}
});

a.join().unwrap();
b.join().unwrap();
})
}
}
126 changes: 0 additions & 126 deletions futures-util/benches_disabled/bilock.rs

This file was deleted.

26 changes: 13 additions & 13 deletions futures-util/src/io/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct WriteHalf<T> {
handle: BiLock<T>,
}

fn lock_and_then<T, U, E, F>(lock: &BiLock<T>, cx: &mut Context<'_>, f: F) -> Poll<Result<U, E>>
fn lock_and_then<T, U, E, F>(lock: &mut BiLock<T>, cx: &mut Context<'_>, f: F) -> Poll<Result<U, E>>
where
F: FnOnce(Pin<&mut T>, &mut Context<'_>) -> Poll<Result<U, E>>,
{
Expand Down Expand Up @@ -53,45 +53,45 @@ impl<T: Unpin> WriteHalf<T> {

impl<R: AsyncRead> AsyncRead for ReadHalf<R> {
fn poll_read(
self: Pin<&mut Self>,
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
lock_and_then(&self.handle, cx, |l, cx| l.poll_read(cx, buf))
lock_and_then(&mut self.handle, cx, |l, cx| l.poll_read(cx, buf))
}

fn poll_read_vectored(
self: Pin<&mut Self>,
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &mut [IoSliceMut<'_>],
) -> Poll<io::Result<usize>> {
lock_and_then(&self.handle, cx, |l, cx| l.poll_read_vectored(cx, bufs))
lock_and_then(&mut self.handle, cx, |l, cx| l.poll_read_vectored(cx, bufs))
}
}

impl<W: AsyncWrite> AsyncWrite for WriteHalf<W> {
fn poll_write(
self: Pin<&mut Self>,
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
lock_and_then(&self.handle, cx, |l, cx| l.poll_write(cx, buf))
lock_and_then(&mut self.handle, cx, |l, cx| l.poll_write(cx, buf))
}

fn poll_write_vectored(
self: Pin<&mut Self>,
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>],
) -> Poll<io::Result<usize>> {
lock_and_then(&self.handle, cx, |l, cx| l.poll_write_vectored(cx, bufs))
lock_and_then(&mut self.handle, cx, |l, cx| l.poll_write_vectored(cx, bufs))
}

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
lock_and_then(&self.handle, cx, |l, cx| l.poll_flush(cx))
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
lock_and_then(&mut self.handle, cx, |l, cx| l.poll_flush(cx))
}

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
lock_and_then(&self.handle, cx, |l, cx| l.poll_close(cx))
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
lock_and_then(&mut self.handle, cx, |l, cx| l.poll_close(cx))
}
}

Expand Down