perf: avoid double checking value0 Future.#10972
Conversation
fbd326d to
dc8956b
Compare
|
|
refs: scala/scala3#22304 |
This comment was marked as resolved.
This comment was marked as resolved.
|
I just checked the |
|
I think it is better to just keep it in a library, as you said, this changed the semantics |
|
@SethTisue I'm not sure why @viktorklang needs to check the duration even though the Future is already completed, but it seems I can't change that at least in Scala 2.13.17 Hope we can avoid that check in some edition, eg, in Scala 3.9 maybe, that's patten is quite common in Akka. |
|
sorry, I don't feel I can help with this PR, but it looks like Lukas has some suggestions |
01ba761 to
c139355
Compare
|
@SethTisue Can we avoid checking the duration when the future is already completed? eg: in Netty, Apache License public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
Object result = this.result;
if (!isDone0(result)) {
if (!await(timeout, unit)) {
throw new TimeoutException();
}
result = this.result;
}
if (result == SUCCESS || result == UNCANCELLABLE) {
return null;
}
Throwable cause = cause0(result);
if (cause == null) {
return (V) result;
}
if (cause instanceof CancellationException) {
throw (CancellationException) cause;
}
throw new ExecutionException(cause);
} |
24aeee1 to
6e13f89
Compare
Avoid evaluating `value0` twice on completed Futures for performance.
|
@He-Pin we can discuss a change of semantics of course, but it should not be done in this PR which is only supposed to improve performance. I moved the |
perf: avoid double checking `value0` Future.
Motivation:
The current
Future.isCompletecheck thevalue0once, and then it will checked it again to extract the value.Modification:
Use
.valueto extract the value at the same time.Result:
Better performance when the future is already completed.
The result if
successOptionwith this modification.I still checked adding an
Future#unwrap = value0()method to avoid the lifting toSome/None,but seems not help much.