Open
Description
It is a well known issue with for-comprehensions where the futures in the code block below are not executed as parallel as possible:
for {
val1 <- getSomeFuture1()
val2 <- getSomeFuture2()
} yield ...
We can manually solve this by rewriting the code as the code block:
val future1 = getSomeFuture1()
val future2 = getSomeFuture2()
for {
val1 <- future1
val2 <- future2
} yield ...
This transformation can be done only when the following two criteria are met:
- The creation of future2 doesn't depend on the result of future1; and,
- The evaluation of the value of future1 doesn't influence the evaluation of getSomeFuture2 and future2.
This manual transformation is quite tedious. This transformation is pretty mechanical and therefore ideally suited for a macro or compiler optimisation. Also, we need to a way to verify the two conditions hold, unless we leave that to the developer.
The reason why I suggest performing the transformation on the level of futures instead of await/async is so it can be used in more scenarios.