Apologies if this is a duplicate - I did a few searches and didn't quite find what I need.
We have a performance critical piece of our application that converts a Play 2.0 Enumerator
(can be thought of as a Stream
) of incoming data to a List
(or similar). We will use the fold
method on Enumerator
and the question is what will be the most performant way to do it. (I will use Stream
instead of Enumerator
in the code, but the idea should be the same.)
val incoming: Stream[Int] = ???
val result: Seq[Int] = incoming.fold(Seq.empty)(_ + _)
val result2: Seq[Int] = incoming.fold(MutableList.empty(_ += _).toSeq
So the question is essentially, how does repeatedly appending to an immutable Vector
compare to repeatedly appending to a mutable MutableList
or ListBuffer
in performance critical code? We've thrown out just List
because we need O(1)
appending (not prepending). But does the mutable data-structure buy us anything in terms of performance or garbage collection?