1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#![feature(test, async_await)]
extern crate test;
mod baseline {
use futures::executor;
use futures::prelude::*;
use std::pin::Pin;
use std::task::{Context, Poll};
#[bench]
fn smoke(b: &mut test::Bencher) {
b.iter(|| {
executor::block_on(async {
juliex::spawn(async move {});
})
});
}
#[bench]
fn notify_self(b: &mut test::Bencher) {
b.iter(|| {
executor::block_on(async {
struct Task {
depth: usize,
}
impl Future for Task {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.depth += 1;
if self.depth == 300 {
Poll::Ready(())
} else {
cx.waker().wake_by_ref();
Poll::Pending
}
}
}
let tasks = (0..300)
.map(|_| {
spawn(async move {
Task { depth: 0 }.await;
})
})
.collect::<Vec<_>>();
for task in tasks {
task.await;
}
})
});
}
#[bench]
fn spawn_many(b: &mut test::Bencher) {
b.iter(|| {
executor::block_on(async {
let tasks = (0..25_000).map(|_| spawn(async {})).collect::<Vec<_>>();
for task in tasks {
task.await;
}
})
});
}
#[bench]
fn poll_reactor(b: &mut test::Bencher) {
use futures::compat::Compat01As03;
use futures::future::FutureExt;
use futures01::{future, Async};
use tokio::reactor::Registration;
b.iter(|| {
executor::block_on(async {
let tasks = (0..300)
.map(|_| {
spawn(async {
let (r, s) = mio::Registration::new2();
let registration = Registration::new();
registration.register(&r).unwrap();
let mut depth = 0;
let mut capture = Some(r);
spawn(
Compat01As03::new(future::poll_fn(move || loop {
if registration.poll_read_ready().unwrap().is_ready() {
depth += 1;
if depth == 300 {
capture.take().unwrap();
return Ok(Async::Ready(()));
}
} else {
s.set_readiness(mio::Ready::readable()).unwrap();
return Ok(Async::NotReady);
}
}))
.map(|_: Result<(), ()>| ()),
)
})
})
.collect::<Vec<_>>();
for task in tasks {
task.await;
}
})
});
}
/// Spawn function for juliex to get back a handle
pub fn spawn<F, T>(fut: F) -> JoinHandle<T>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
let (tx, rx) = futures::channel::oneshot::channel();
let fut = async move {
let t = fut.await;
let _ = tx.send(t);
};
juliex::spawn(fut);
JoinHandle { rx }
}
/// Handle returned from Juliex.
// We should patch Juliex to support this natively, and be more efficient on channel use.
#[derive(Debug)]
pub struct JoinHandle<T> {
pub(crate) rx: futures::channel::oneshot::Receiver<T>,
}
impl<T> Future for JoinHandle<T> {
type Output = T;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.rx.poll_unpin(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(Ok(t)) => Poll::Ready(t),
Poll::Ready(Err(_)) => panic!(), // TODO: Is this OK? Print a better error message?
}
}
}
}