Skip to content

Re-ordering statements for more optimal parallel future execution #138

Open
@spockz

Description

@spockz

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:

  1. The creation of future2 doesn't depend on the result of future1; and,
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions