-
Notifications
You must be signed in to change notification settings - Fork 0
/
coro_dfs.cpp
324 lines (253 loc) · 5.48 KB
/
coro_dfs.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include <benchmark/benchmark.h>
#include <iostream>
#include <vector>
#include <coroutine>
#include <chrono>
#include <coro/single_pass.hpp>
#include <boost/coroutine2/coroutine.hpp>
using namespace std;
inline namespace common {
struct Node {
int value;
Node* left;
Node* right;
Node(int x) : value(x), left(nullptr), right(nullptr) {
}
};
Node* MakeFullTree(int n, int s = 0) {
auto p = new Node(s);
if (n == 1) {
return p;
}
n /= 2;
p->left = MakeFullTree(n, s + 1);
p->right = MakeFullTree(n, s + 2 * n);
return p;
}
} // namespace common
namespace slow {
using generator = coro::SinglePass<int>;
generator Dfs(Node* p) {
if (!p) {
co_return;
}
if (!p->left && !p->right) {
co_yield p->value;
co_return;
}
// not only you alloc, but each leaf will have to
// propagate through whole stack of co_yield's
for (int x : Dfs(p->left)) {
co_yield x;
}
for (int x : Dfs(p->right)) {
co_yield x;
}
}
} // namespace slow
static void BM_Slow(benchmark::State& state) {
auto root = MakeFullTree(state.range(0));
for (auto _ : state) {
int res = 0;
for (int x : slow::Dfs(root)) {
res += x;
}
}
}
namespace {
struct Stack {
vector<char> buffer;
size_t end;
size_t frame_size;
Stack(size_t size) : buffer(size), end(0), frame_size(0) {
}
void* BackAddress() {
return buffer.data() + end - frame_size;
}
void Pop() {
end -= frame_size;
}
void* New(size_t count) {
frame_size = count;
end += frame_size;
return BackAddress();
}
bool Empty() {
return !end;
}
};
struct Wait {
bool await_ready() const noexcept {
return false;
}
void await_resume() const noexcept {
}
coroutine_handle<> await_suspend(coroutine_handle<> coro) const noexcept {
return handle;
}
coroutine_handle<> handle;
};
struct SingleValue {
bool ok{false};
int value{0};
void Set(int x) {
ok = true;
value = x;
}
int Pop() {
ok = false;
return value;
}
bool IsSet() const {
return ok;
}
};
struct Task {
Stack stack;
SingleValue value;
bool HasStack();
auto Yield(int x);
void Resume();
Task() : stack(2'000), value() {
}
};
// thread locals on macOS are extremely slow (x2-x3 slower here)
// thread locals on Linux (tested on ubuntu) are OK though
// https://github.com/rust-lang/rust/issues/60141 looks relevant
static Task* task = new Task();
struct Coro {
struct promise_type {
public:
friend struct Coro;
// disable HALO
__attribute__((noinline)) Coro get_return_object() {
return {std::coroutine_handle<promise_type>::from_promise(*this)};
}
std::suspend_always initial_suspend() const {
return {};
}
auto final_suspend() const noexcept {
auto& stack = task->stack;
stack.Pop();
if (stack.Empty()) {
return Wait{std::noop_coroutine()};
}
return Wait{std::coroutine_handle<promise_type>::from_address(
stack.BackAddress())};
}
void unhandled_exception() {
terminate();
}
void return_void() {
}
auto yield_value(int x) {
task->value.Set(x);
return suspend_always{};
}
static void* operator new(size_t count) {
return task->stack.New(count);
}
static void operator delete(void* ptr) {
}
};
using coro_handle = std::coroutine_handle<promise_type>;
Coro(coro_handle h) : handle(h) {
}
~Coro() {
if (handle) {
handle.destroy();
}
}
auto operator co_await() {
return Wait{handle};
}
coro_handle handle{};
};
bool Task::HasStack() {
return not stack.Empty();
}
void Task::Resume() {
coroutine_handle<Coro::promise_type>::from_address(stack.BackAddress())
.resume();
}
Coro CoroDfs(Node* p) {
if (!p) {
co_return;
}
if (!p->left && !p->right) {
co_yield p->value;
co_return;
}
co_await CoroDfs(p->left);
co_await CoroDfs(p->right);
}
} // namespace
static void BM_Coro(benchmark::State& state) {
auto root = MakeFullTree(state.range(0));
for (auto _ : state) {
int res = 0;
for (auto call = CoroDfs(root); task->HasStack(); task->Resume()) {
if (task->value.IsSet()) {
res += task->value.Pop();
}
}
}
}
namespace fast {
void Dfs(Node* p, auto&& leaf_callback) {
if (!p) {
return;
}
if (!p->left && !p->right) {
leaf_callback(p->value);
return;
}
Dfs(p->left, leaf_callback);
Dfs(p->right, leaf_callback);
}
} // namespace fast
namespace fiber {
using coro_t = boost::coroutines2::coroutine<int>;
auto Dfs(Node* p) {
coro_t::pull_type source([p](coro_t::push_type& sink) {
fast::Dfs(p, sink);
});
return source;
}
} // namespace fiber
static void BM_Fiber(benchmark::State& state) {
auto root = MakeFullTree(state.range(0));
for (auto _ : state) {
int res = 0;
for (int x : fiber::Dfs(root)) {
res += x;
}
}
}
static void BM_Fast(benchmark::State& state) {
auto root = MakeFullTree(state.range(0));
for (auto _ : state) {
int res = 0;
fast::Dfs(root, [&res](int x) {
res += x;
});
}
}
BENCHMARK(BM_Slow) //
->Arg(1 << 14) //
->Arg(1 << 20); //
BENCHMARK(BM_Coro) //
->Arg(1 << 14) //
->Arg(1 << 20) //
->Arg(1 << 22) //
->Arg(1 << 24); //
BENCHMARK(BM_Fiber) //
->Arg(1 << 14) //
->Arg(1 << 20) //
->Arg(1 << 22) //
->Arg(1 << 24); //
BENCHMARK(BM_Fast) //
->Arg(1 << 14) //
->Arg(1 << 20) //
->Arg(1 << 22) //
->Arg(1 << 24); //