-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #597 from carllerche/improve-thread-notify
Improve thread notify
- Loading branch information
Showing
2 changed files
with
229 additions
and
48 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 |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#![feature(test)] | ||
|
||
extern crate futures; | ||
extern crate test; | ||
|
||
use futures::{Future, Poll, Async}; | ||
use futures::task::{self, Task}; | ||
|
||
use test::Bencher; | ||
|
||
#[bench] | ||
fn thread_yield_single_thread_one_wait(b: &mut Bencher) { | ||
const NUM: usize = 10_000; | ||
|
||
struct Yield { | ||
rem: usize, | ||
} | ||
|
||
impl Future for Yield { | ||
type Item = (); | ||
type Error = (); | ||
|
||
fn poll(&mut self) -> Poll<(), ()> { | ||
if self.rem == 0 { | ||
Ok(Async::Ready(())) | ||
} else { | ||
self.rem -= 1; | ||
task::current().notify(); | ||
Ok(Async::NotReady) | ||
} | ||
} | ||
} | ||
|
||
b.iter(|| { | ||
let y = Yield { rem: NUM }; | ||
y.wait().unwrap(); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn thread_yield_single_thread_many_wait(b: &mut Bencher) { | ||
const NUM: usize = 10_000; | ||
|
||
struct Yield { | ||
rem: usize, | ||
} | ||
|
||
impl Future for Yield { | ||
type Item = (); | ||
type Error = (); | ||
|
||
fn poll(&mut self) -> Poll<(), ()> { | ||
if self.rem == 0 { | ||
Ok(Async::Ready(())) | ||
} else { | ||
self.rem -= 1; | ||
task::current().notify(); | ||
Ok(Async::NotReady) | ||
} | ||
} | ||
} | ||
|
||
b.iter(|| { | ||
for _ in 0..NUM { | ||
let y = Yield { rem: 1 }; | ||
y.wait().unwrap(); | ||
} | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn thread_yield_multi_thread(b: &mut Bencher) { | ||
use std::sync::mpsc; | ||
use std::thread; | ||
|
||
const NUM: usize = 1_000; | ||
|
||
let (tx, rx) = mpsc::sync_channel::<Task>(10_000); | ||
|
||
struct Yield { | ||
rem: usize, | ||
tx: mpsc::SyncSender<Task>, | ||
} | ||
|
||
impl Future for Yield { | ||
type Item = (); | ||
type Error = (); | ||
|
||
fn poll(&mut self) -> Poll<(), ()> { | ||
if self.rem == 0 { | ||
Ok(Async::Ready(())) | ||
} else { | ||
self.rem -= 1; | ||
self.tx.send(task::current()).unwrap(); | ||
Ok(Async::NotReady) | ||
} | ||
} | ||
} | ||
|
||
thread::spawn(move || { | ||
while let Ok(task) = rx.recv() { | ||
task.notify(); | ||
} | ||
}); | ||
|
||
b.iter(move || { | ||
let y = Yield { | ||
rem: NUM, | ||
tx: tx.clone(), | ||
}; | ||
|
||
y.wait().unwrap(); | ||
}); | ||
} |
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