Skip to content

await() on already-completed future could return value synchronously #73

Closed
@danarmak

Description

@danarmak

await(future) always compiles to future.onComplete(...), even if the future is already completed. (I think so based on the output of scalac --Ymacro-debug-lite in the 2.10 branch.) Why not return the value synchronously if it is already available?

Here's a simple implementation as a macro wrapping await():

  def fastAwait[T](fut: Future[T]): T = macro fastAwaitImpl[T]

  def fastAwaitImpl[T: c.WeakTypeTag](c: Context)(fut: c.Expr[Future[T]]): c.Expr[T] = {
    import c.universe._
    val tree =  q"{ val f = $fut; if (f.isCompleted) f.value.get.get else await(f) }"
    c.Expr[T](tree)
  }

I have code that passes very large amounts of short-lived Futures around. Many of these are already completed when awaited. This optimization helps (from initial tests) and I'm considering using it in my code - is there any reason not to do this?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions