Skip to content
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

Use of japi.Util.immutableSeq(j.u.List) is inefficient because it iterates over the list to create a new Seq #1488

Closed
pjfanning opened this issue Sep 20, 2024 · 1 comment

Comments

@pjfanning
Copy link
Contributor

pjfanning commented Sep 20, 2024

def immutableSeq[T](iterable: java.lang.Iterable[T]): immutable.Seq[T] =
iterable match {
case imm: immutable.Seq[_] => imm.asInstanceOf[immutable.Seq[T]]
case other =>
val i = other.iterator()
if (i.hasNext) {
val builder = new immutable.VectorBuilder[T]
while ({ builder += i.next(); i.hasNext }) ()
builder.result()
} else EmptyImmutableSeq
}

Follow up to #1250.

It's used in a lot of places internally but the most performance sensitive area is the pekko-stream code (eg Flow, Source, etc).
The obvious solution of using pekko.util.ccompat.JavaConverters to convert using .asScala.toSeq doesn't compile in Scala 2.12 because toSeq gives a scala.Seq instead of a scala.immutable.Seq.

We could use .asScala.toList but that is likely to be slower. We could also a new Util.immutableWrappingSeq function that has different implementations for Scala 2.12 (possibly delegate to existing immutableSeq) and Scala 2.13+ that uses .asScala.toSeq.

@pjfanning
Copy link
Contributor Author

Some changes are in Pekko 1.1.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant