You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
as well as some code her coworkers have written using c++20's `coroutines`,
32
32
using [this](https://github.com/facebook/folly/blob/master/folly/experimental/coro/Collect.h#L321):
33
33
34
-
```
34
+
```C++
35
35
std::vector<folly::coro::Task<Data>> tasks;
36
36
for (constauto& query : queries) {
37
37
tasks.push_back(
@@ -60,7 +60,8 @@ implementation used above, noticing that is works primarily with *tasks*, which
60
60
equivalent to rust `Future`'s.
61
61
62
62
Then it strikes her! `request` is implemented something like this:
63
-
```
63
+
64
+
```rust
64
65
impl Client {
65
66
async fn request(&self) -> Result<Data> {
66
67
let bytes = self.inner.network_request().await?
@@ -77,7 +78,7 @@ This problem hadn't shown up in test cases, where the results from the mocked ne
77
78
78
79
The solution is to spawn tasks (note this requires `'static` futures):
79
80
80
-
```
81
+
```rust
81
82
letmutqueries=futures::stream::iter(...)
82
83
.map(|query|asyncmove {
83
84
letd:Data=tokio::spawn(
@@ -93,18 +94,29 @@ let results = queries.collect::<Vec<Data>>().await;
93
94
Barbara was able to figure this out by reading enough and trying things out, but had that not worked, it
94
95
would have probably required figuring out how to use `perf` or some similar tool.
95
96
96
-
Later on, Barbara gets surprised by this code again. It's now being used as part of a system that handles a very high number of requests per second, but sometimes the system stalls under load. She enlists Grace to help debug, and the two of them identify via `perf` that all the CPU cores are busy running `serialization_libary::from_bytes`. Barbara revisits this solution, and discovers `tokio::task::block_in_place` which she uses to wrap the calls to `serialization_libary::from_bytes`.
97
-
This resolves the problem as seen in production, but leads to Niklaus's code review suggesting the use of `tokio::task::spawn_blocking` inside `request`, instead of `spawn` inside `buffer_unordered`. This discussion is challenging, because the tradeoffs between `spawn` on a `Future` including `block_in_place` and `spawn_blocking` and then not spawning the containing `Future` are subtle and tricky to explain.
97
+
Later on, Barbara gets surprised by this code again. It's now being used as part of a system that handles a very high number of requests per second, but sometimes the system stalls under load. She enlists Grace to help debug, and the two of them identify via `perf` that all the CPU cores are busy running `serialization_libary::from_bytes`. Barbara revisits this solution, and discovers `tokio::task::block_in_place` which she uses to wrap the calls to `serialization_libary::from_bytes`:
This resolves the problem as seen in production, but leads to Niklaus's code review suggesting the use of `tokio::task::spawn_blocking` inside `request`, instead of `spawn` inside `buffer_unordered`. This discussion is challenging, because the tradeoffs between `spawn` on a `Future` including `block_in_place` and `spawn_blocking` and then not spawning the containing `Future` are subtle and tricky to explain. Also, either `block_in_place` and `spawn_blocking` are heavyweight and Barbara would prefer to avoid them when the cost of serialization is low, which is usually a runtime-property of the system.
108
+
98
109
99
110
## 🤔 Frequently Asked Questions
100
111
101
-
### **Is this actually the correct solution?**
112
+
### **Are any of these actually the correct solution?**
102
113
* Only in part. It may cause other kinds of contention or blocking on the runtime. As mentioned above, the deserialization work probably needs to be wrapped in something like [`block_in_place`](https://docs.rs/tokio/1/tokio/task/fn.block_in_place.html), so that other tasks are not starved on the runtime, or might want to use [`spawn_blocking`](https://docs.rs/tokio/1/tokio/task/fn.spawn_blocking.html). There are some important caveats/details that matter:
103
114
* This is dependent on how the runtime works.
104
-
*`block_in_place` + `tokio::spawn` might be better if the caller wants to control concurrency, as spawning is heavyweight when the deserialization work happens to be small.
115
+
*`block_in_place` + `tokio::spawn` might be better if the caller wants to control concurrency, as spawning is heavyweight when the deserialization work happens to be small. However, as mentioned above, this can be complex to reason about, and in some cases, may be as heavyweight as `spawn_blocking`
105
116
*`spawn_blocking`, at least in some executors, cannot be cancelled, a departure from the prototypical cancellation story in async Rust.
106
117
* "Dependently blocking work" in the context of async programming is a hard problem to solve generally. https://github.com/async-rs/async-std/pull/631 was an attempt but the details are making runtime's agnostic blocking are extremely complex.
107
118
* The way this problem manifests may be subtle, and it may be specific production load that triggers it.
119
+
* The outlined solutions have tradeoffs that each only make sense for certain kind of workloads. It may be better to expose the io aspect of the request and the deserialization aspect as separate APIs, but that complicates the library's usage, lays the burden of choosing the tradeoff on the callee (which may not be generally possible).
108
120
### **What are the morals of the story?**
109
121
* Producing concurrent, performant code in Rust async is not always trivial. Debugging performance
0 commit comments