Skip to content

perf: avoid double checking value0 Future.#10972

Merged
lrytz merged 1 commit intoscala:2.13.xfrom
He-Pin:futureAwait
Sep 17, 2025
Merged

perf: avoid double checking value0 Future.#10972
lrytz merged 1 commit intoscala:2.13.xfrom
He-Pin:futureAwait

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

@He-Pin He-Pin commented Jan 3, 2025

Motivation:

The current Future.isComplete check the value0 once, and then it will checked it again to extract the value.

Modification:

Use .value to extract the value at the same time.

Result:
Better performance when the future is already completed.

[info] Benchmark                                                 (pool)  (recursion)  (threads)   Mode  Cnt        Score        Error   Units
[info] CompleteFutureBenchmark.success                              fix         1024          1  thrpt    5   990403.006 ±   6431.624  ops/ms
[info] CompleteFutureBenchmark.success:gc.alloc.rate                fix         1024          1  thrpt    5        0.001 ±      0.001  MB/sec
[info] CompleteFutureBenchmark.success:gc.alloc.rate.norm           fix         1024          1  thrpt    5       ≈ 10⁻⁶                 B/op
[info] CompleteFutureBenchmark.success:gc.count                     fix         1024          1  thrpt    5          ≈ 0               counts
[info] CompleteFutureBenchmark.successOption                        fix         1024          1  thrpt    5  1265272.145 ± 336165.695  ops/ms
[info] CompleteFutureBenchmark.successOption:gc.alloc.rate          fix         1024          1  thrpt    5        0.001 ±      0.001  MB/sec
[info] CompleteFutureBenchmark.successOption:gc.alloc.rate.norm     fix         1024          1  thrpt    5       ≈ 10⁻⁶                 B/op
[info] CompleteFutureBenchmark.successOption:gc.count               fix         1024          1  thrpt    5          ≈ 0               counts
[info] CompleteFutureBenchmark.successUnwrap                        fix         1024          1  thrpt    5  1322735.278 ±  25844.808  ops/ms
[info] CompleteFutureBenchmark.successUnwrap:gc.alloc.rate          fix         1024          1  thrpt    5        0.001 ±      0.001  MB/sec
[info] CompleteFutureBenchmark.successUnwrap:gc.alloc.rate.norm     fix         1024          1  thrpt    5       ≈ 10⁻⁶                 B/op
[info] CompleteFutureBenchmark.successUnwrap:gc.count               fix         1024          1  thrpt    5          ≈ 0               counts`

The result if successOption with this modification.

I still checked adding an Future#unwrap = value0() method to avoid the lifting to Some/None ,but seems not help much.

@scala-jenkins scala-jenkins added this to the 2.13.17 milestone Jan 3, 2025
@He-Pin He-Pin force-pushed the futureAwait branch 2 times, most recently from fbd326d to dc8956b Compare January 3, 2025 09:41
@He-Pin
Copy link
Copy Markdown
Contributor Author

He-Pin commented Jan 3, 2025

[error] -- [E149] Syntax Error: /home/travis/build/scala/scala/target/library/src_managed/main/scala3-library-src/scala/Matchable.scala:7:6 
[error] 7 |trait Matchable
[error]   |      ^^^^^^^^^
[error]   |      illegal redefinition of standard class Matchable
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] -- [E149] Syntax Error: /home/travis/build/scala/scala/target/library/src_managed/main/scala3-library-src/scala/AnyKind.scala:7:21 
[error] 7 |final abstract class AnyKind
[error]   |                     ^^^^^^^
[error]   |                     illegal redefinition of standard class AnyKind
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] -- [E007] Type Mismatch Error: /home/travis/build/scala/scala/src/library/scala/concurrent/package.scala:203:45 
[error] 203 |      case CompletedFuture(result) => result.get
[error]     |                                      ^^^^^^^^^^
[error]     |                                      Found:    Any
[error]     |                                      Required: T
[error]     |

@He-Pin
Copy link
Copy Markdown
Contributor Author

He-Pin commented Jan 3, 2025

refs: scala/scala3#22304

@SethTisue SethTisue added the library:concurrent Changes to the concurrency support in stdlib label Jan 6, 2025
@SethTisue SethTisue marked this pull request as draft February 4, 2025 04:39
@SethTisue SethTisue added the performance the need for speed. usually compiler performance, sometimes runtime performance. label Sep 3, 2025
@He-Pin

This comment was marked as resolved.

@He-Pin He-Pin closed this Sep 5, 2025
@He-Pin He-Pin reopened this Sep 5, 2025
@He-Pin
Copy link
Copy Markdown
Contributor Author

He-Pin commented Sep 5, 2025

I just checked the CompletableFuture's get method again, I think we can follow that.

@He-Pin He-Pin marked this pull request as ready for review September 5, 2025 15:16
@He-Pin He-Pin requested a review from a team as a code owner September 5, 2025 15:16
@He-Pin
Copy link
Copy Markdown
Contributor Author

He-Pin commented Sep 16, 2025

I think it is better to just keep it in a library, as you said, this changed the semantics

@He-Pin He-Pin closed this Sep 16, 2025
@SethTisue SethTisue removed this from the 2.13.17 milestone Sep 16, 2025
@He-Pin
Copy link
Copy Markdown
Contributor Author

He-Pin commented Sep 16, 2025

@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.

@SethTisue
Copy link
Copy Markdown
Member

sorry, I don't feel I can help with this PR, but it looks like Lukas has some suggestions

@He-Pin He-Pin reopened this Sep 17, 2025
@scala-jenkins scala-jenkins added this to the 2.13.17 milestone Sep 17, 2025
@He-Pin He-Pin force-pushed the futureAwait branch 2 times, most recently from 01ba761 to c139355 Compare September 17, 2025 04:59
@He-Pin He-Pin requested a review from lrytz September 17, 2025 05:00
@He-Pin
Copy link
Copy Markdown
Contributor Author

He-Pin commented Sep 17, 2025

@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);
    }

@lrytz lrytz force-pushed the futureAwait branch 2 times, most recently from 24aeee1 to 6e13f89 Compare September 17, 2025 09:43
Avoid evaluating `value0` twice on completed Futures for performance.
@lrytz
Copy link
Copy Markdown
Member

lrytz commented Sep 17, 2025

@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 case f: Future[T] match into the extractor to avoid the asInstanceOf[T] on Scala 3 (see also scala/scala3#22304 (comment)).

@lrytz lrytz merged commit c0a2ae6 into scala:2.13.x Sep 17, 2025
3 checks passed
hamzaremmal pushed a commit to scala/scala3 that referenced this pull request Sep 24, 2025
perf: avoid double checking `value0` Future.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

library:concurrent Changes to the concurrency support in stdlib performance the need for speed. usually compiler performance, sometimes runtime performance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants