optimize: avoid Holder allocation in ordered mapAsync for already-completed futures#3018
Merged
He-Pin merged 1 commit intoMay 31, 2026
Merged
Conversation
…pleted futures Motivation: Ordered `MapAsync` allocates a `Holder` and performs a buffer enqueue/dequeue round-trip for every element, even when the future returned by the user function is already completed successfully and can be emitted immediately. With a synchronously-completing function (caching, `Future.successful`, ...) this is pure per-element garbage. Modification: In `MapAsync.onPush`, add a zero-allocation fast path: when the future is already completed with a non-null element, nothing is buffered ahead of it (so ordering is already satisfied) and downstream demand is present, push the element straight through without allocating a `Holder` or touching the buffer. The not-yet-completed case is matched first so the pending path is unchanged (single type check, never evaluates the fast-path guard). Result: JMH MapAsyncBenchmark (parallelism=4, spawn=false): 18.0M -> 20.3M ops/s (+12%), with the per-element `Holder` allocation eliminated (confirmed via async-profiler alloc profile). No regression on the asynchronous path (spawn=true). All 27 FlowMapAsyncSpec tests pass; binary compatible (internal API). References: bench-jmh MapAsyncBenchmark; relates to the existing already-completed-future optimization (#20217).
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes the ordered MapAsync GraphStage to avoid per-element Holder allocation and buffer round-trips when the user function returns an already-successfully-completed Future and ordering is trivially satisfied (no buffered elements ahead, downstream demand present).
Changes:
- Adds a fast path in
MapAsync.onPushto immediatelypushalready-completed successful (non-null) results whenbuffer.isEmptyandisAvailable(out). - Keeps the pending (
None) path behavior/cost essentially unchanged by matching it first, and retains existing failure/supervision handling for other completed cases.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Ordered
MapAsyncallocates aHolderand performs a buffer enqueue/dequeue round-trip for every element, even when the future returned by the user function is already completed successfully and could be emitted immediately. With a synchronously-completing function (caching,Future.successful, conditional async, ...) this is pure per-element garbage.Note:
mapAsync(1)delegates tomapAsyncUnordered(1)(Flow.scala), andMapAsyncUnorderedalready pushes completed results straight through without aHolder. The allocation only existed in the ordered stage atparallelism >= 2.Modification
In
MapAsync.onPush, add a zero-allocation fast path: when the future is already completed with a non-null element, nothing is buffered ahead of it (so ordering is already satisfied) and downstream demand is present, push the element straight through without allocating aHolderor touching the buffer.The not-yet-completed (
None) case is matched first, so the pending path is unchanged: a single type check that never evaluates the fast-path guard (behaviourally and cost-wise identical to before).Result
JMH
MapAsyncBenchmark(parallelism=4,spawn=false): 18.0M -> 20.3M ops/s (+12%), with the per-elementHolderallocation eliminated (confirmed via async-profiler alloc profile:Holderdisappears; remaining allocation is the user'sSuccessbox fromFuture.successful). No regression on the asynchronous path (spawn=true, within run-to-run noise).Tests
sbt "stream-tests/testOnly *FlowMapAsyncSpec"— 27/27 green. Existing coverage already exercises the fast path: "produce future elements in order" mixesFuture.successfulwith async futures, andmapAsync(2)(Future.successful(_))drives ordered + already-completed.sbt stream/Compile/compile,stream/scalafmt,stream/headerCheck,stream/mimaReportBinaryIssues— all green (binary compatible, internal API only).References
MapAsyncBenchmark