Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would Scala Range iterator buffer -- sometimes?

In Scala 2.9.1, this works fine:

scala> (1 to Int.MaxValue).sum
res6: Int = -1073741824

Yet this runs out of heap space:

scala> (1 to Int.MaxValue).toIterator.sum
java.lang.OutOfMemoryError: GC overhead limit exceeded

But maddeningly, this works:

scala> (1 to Int.MaxValue).iterator.sum
res8: Int = -1073741824

Why should any of those be different?

like image 542
Jay Hacker Avatar asked Dec 27 '22 10:12

Jay Hacker


1 Answers

toIterator is defined in TraversableLike as

def toIterator: Iterator[A] = toStream.iterator

so it creates a Stream in the background which keeps all elements in memory while iterating.

(Edit: I think the stream structure isn’t the problem here actually. However, toStream itself calls toBuffer which in turn copies every single value.)

iterator on the other hand is defined in IndexedSeqLike which uses a specialised structure which does not keep any elements in memory.

like image 139
Debilski Avatar answered Jan 12 '23 08:01

Debilski