Skip to content

Conversation

@He-Pin
Copy link
Member

@He-Pin He-Pin commented Dec 6, 2025

Optimize it when the size is little.

@He-Pin He-Pin modified the milestones: 2.0.0-M1, 2.0.0-M2 Dec 6, 2025
@He-Pin He-Pin added the t:stream Pekko Streams label Dec 6, 2025
@He-Pin
Copy link
Member Author

He-Pin commented Dec 7, 2025

fixed: #2558

Copy link
Contributor

@mdedetrich mdedetrich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Contributor

Copilot AI left a 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 knownSize to detect empty and single-element iterables
  • Java implementation uses Collection type checks with isEmpty() and size() == 1 conditions
  • 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.

@He-Pin
Copy link
Member Author

He-Pin commented Dec 7, 2025

The current code will miss the x :: Nil case, so I upstream the changes to scala
scala/scala#11188

@mdedetrich
Copy link
Contributor

The current code will miss the x :: Nil case, so I upstream the changes to scala scala/scala#11188

You will also have to do Scala 2.12 and also Scala 3 (aka dotty)

@He-Pin
Copy link
Member Author

He-Pin commented Dec 7, 2025

@mdedetrich No such method in Scala 2.12:) and Scala 3 will sync the stdlib from 2.13.x

@He-Pin He-Pin merged commit a536fe3 into main Dec 7, 2025
18 of 20 checks passed
@He-Pin He-Pin deleted the collection branch December 7, 2025 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

t:stream Pekko Streams

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants