Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ class TraversalBuilderSpec extends PekkoSpec {
}

"find Source.iterable via TraversalBuilder with getValuePresentedSource" in {
val iterable = List("a")
val iterable = List("a", "b", "c")
TraversalBuilder.getValuePresentedSource(Source(iterable)).get.asInstanceOf[IterableSource[
String]].elements should ===(
iterable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,15 @@ object Source {
* stream will see an individual flow of elements (always starting from the
* beginning) regardless of when they subscribed.
*/
def apply[T](iterable: immutable.Iterable[T]): Source[T, NotUsed] = {
(iterable.knownSize: @switch) match {
case 0 => empty
case 1 => single(iterable.head)
case _ =>
fromGraph(new IterableSource[T](iterable)).withAttributes(DefaultAttributes.iterableSource)
}
def apply[T](iterable: immutable.Iterable[T]): Source[T, NotUsed] = iterable match {
case immutable.Seq() => empty[T]
Copy link
Member

Choose a reason for hiding this comment

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

I don't see how adding extra checks, including instanceof checks, improves performance. Is there any way that we could keep a variant of the old code from the recent other PR that introduced changes?

Copy link
Member Author

@He-Pin He-Pin Dec 8, 2025

Choose a reason for hiding this comment

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

It's vs the materialization

case immutable.Seq(elem: T @unchecked) => single(elem)
case other => (other.knownSize: @switch) match {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a comment saying that if the Iterable has unknown size, that knownSize returns -1?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not unknown size,just the size can or not been cheaply cimputed

case 0 => empty
case 1 => single(iterable.head)
case _ =>
fromGraph(new IterableSource[T](iterable)).withAttributes(DefaultAttributes.iterableSource)
}
}

/**
Expand Down