Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the creation of multi-dimensional arrays so slow in Scala?

Consider this code:

Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array(Array())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))

I aborted the REPL after several minutes.

Should I expect such long compilation times or is this a problem/bug of the compiler?

like image 534
soc Avatar asked Dec 21 '22 08:12

soc


2 Answers

Misleading title, I think, at least with respect to the actual code you're trying.

Let's help the type inferencer...

object A extends App {
  val x = Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Array[Nothing]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]()
  println(x)
}

That compiles just fine and runs just fine (I don't even have to modify JVM options):

$ time scalac -d classes A.scala

real    0m5.179s

$ time scala -cp classes A
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[Ljava.lang.Object;@872380

real    0m2.461s

So it's more about compilation and type inference including in the REPL (which rewrites the code and recompiles). The REPL seems to struggle somewhere after the explicitrouter phase (tried using scala -Xprint:all).

like image 177
huynhjl Avatar answered Dec 23 '22 20:12

huynhjl


On Scala 2.9.0.1 this compiles (and runs) just fine as long as you give scalac enough stack space:

export JAVA_OPTS="-ss128M"
scalac arrays.scala

It doesn't seem to work in the REPL though, but that doesn't really surprise me anymore...

like image 29
Kim Stebel Avatar answered Dec 23 '22 20:12

Kim Stebel