janvladimirmostert
11/10/2019, 5:25 PMCompletableFuture.await()
, how much overhead does await()
add in the scenario where the CompletableFuture
is not yet done?
public suspend fun <T> CompletionStage<T>.await(): T {
// fast path when CompletableFuture is already done (does not suspend)
if (this is Future<*> && isDone()) {
...
}
// slow path -- suspend
return suspendCancellableCoroutine { cont: CancellableContinuation<T> ->
val consumer = ContinuationConsumer(cont)
whenComplete(consumer)
cont.invokeOnCancellation {
// mayInterruptIfRunning is not used
(this as? CompletableFuture<T>)?.cancel(false)
consumer.cont = null // shall clear reference to continuation to aid GC
}
}
}
Is the overhead just extra class instances being created or is there more happening?octylFractal
11/10/2019, 5:30 PMjanvladimirmostert
11/10/2019, 5:46 PM