Search code examples
arraysscalaperformance

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?


Solution

  • 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...