Reduce receive-timeout state allocations#3334
Draft
He-Pin wants to merge 1 commit into
Draft
Conversation
Motivation: Actors with receive timeout enabled recreated two Tuple2 state objects for every influencing message, adding avoidable allocation pressure to the ActorCell hot path. Modification: Replace immutable tuple snapshots with one lazily allocated mutable state object and a per-invocation change counter. Add a burst-reset regression test and an end-to-end Actor JMH benchmark with GC profiling. Result: On JDK 17 the real Actor benchmark reduces stable allocation from 168.018 to 120.018 B/message, exactly 48 B/message or 28.6%. Throughput remains neutral within the confidence interval. Tests: - actor-tests / Test / testOnly org.apache.pekko.actor.ReceiveTimeoutSpec (13 passed) - bench-jmh / Jmh / compile - ReceiveTimeoutBenchmark with JMH gc profiler on JDK 17.0.17 - +mimaReportBinaryIssues (Scala 2.13 and Scala 3 passed) - +headerCheckAll and checkCodeStyle - scalafmt diff check and git diff --check - Qoder review: No must-fix findings - validatePullRequest not completed locally per user request; rely on CI References: Refs apache#1668
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
An actor with receive timeout enabled currently recreates immutable
(Duration, Cancellable)tuples while cancelling and rescheduling the timeout for every influencing message. This puts two short-livedTuple2objects on theActorCell.invokehot path even though receive-timeout state is mailbox-thread confined.Modification
Intcounter compared only across one message invocation, preserving the previous tuple-reference identity semantics without per-message state allocation.ReceiveTimeoutSpecwith a 100-message reset burst and an explicit assertion that the timeout is measured from the final processed message.Result
JDK 17.0.17, G1, one Actor/dispatcher thread, 10,000 messages per invocation, 3 forks, 5 x 1 second warmup and measurement:
The allocation result is stable across all non-teardown measurement iterations: exactly 48 B/message less, a 28.6% reduction in total allocation for this real Actor path. Throughput confidence intervals overlap, so this PR claims allocation/GC improvement rather than a statistically significant CPU improvement.
The state object is allocated only when an actor first configures receive timeout; actors that never enable it retain the existing null-reference footprint. Public Actor APIs and behavior are unchanged.
References
Refs #1668