-
Notifications
You must be signed in to change notification settings - Fork 186
chore: Optimize Source.from iterable. #2556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
fixed: #2558 |
mdedetrich
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR optimizes Source.from methods in both Scala and Java APIs to use more efficient specialized sources (Source.empty and Source.single) when the input iterable is known to be small (0 or 1 elements), avoiding the overhead of creating an IterableSource for these trivial cases.
- Scala implementation uses pattern matching on
knownSizeto detect empty and single-element iterables - Java implementation uses
Collectiontype checks withisEmpty()andsize() == 1conditions - Test adjustments ensure materialization semantics are preserved in edge case scenarios
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| stream/src/main/scala/org/apache/pekko/stream/scaladsl/Source.scala | Adds optimization to Source.apply to use empty or single sources when iterable has 0 or 1 elements based on knownSize |
| stream/src/main/scala/org/apache/pekko/stream/javadsl/Source.scala | Adds optimization to Source.from to use empty() or single() when Java Collection has 0 or 1 elements |
| stream-tests/src/test/scala/org/apache/pekko/stream/scaladsl/SourceSpec.scala | Adds test case covering the optimized paths for empty, single, and multiple element sources |
| stream-tests/src/test/scala/org/apache/pekko/stream/scaladsl/FlowFlatMapPrefixSpec.scala | Wraps empty list sources with lazySource to preserve materialization timing semantics in edge case tests |
| stream-tests/src/test/java/org/apache/pekko/stream/javadsl/SourceTest.java | Adds test case for Java Source.from covering empty, single, and multiple element iterables |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
The current code will miss the |
You will also have to do Scala 2.12 and also Scala 3 (aka dotty) |
|
@mdedetrich No such method in Scala 2.12:) and Scala 3 will sync the stdlib from 2.13.x |
Optimize it when the size is little.