Why is creating multidimensional 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 interrupted REPL after a few minutes.

Should I expect such long compilation periods or is this a compiler problem / error?

+4
source share
2 answers

In Scala 2.9.0.1, this compiles (and works) just fine, as long as you give enough stack space:

 export JAVA_OPTS="-ss128M" scalac arrays.scala 

The REPL doesn't seem to work, but that doesn't surprise me anymore ...

+5
source

A misleading title, I think, at least with regard to the actual code that you are trying to do.

Help 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) } 

This compiles just fine and works just fine (I don’t even need to change the JVM options):

 $ time scalac -d classes A.scala real 0m5.179s $ time scala -cp classes A [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[Ljava.lang.Object;@872380 real 0m2.461s 

So, this is more about compiling and type inference, including in REPL (which rewrites code and recompiles). It seems that REPL is trying to work somewhere after the kernelrouter phase (tried using scala -Xprint:all ).

+6
source

All Articles