Search code examples
javascalastatic-initializationpredef

Force scala.Predef initialization on application start without affecting the code


Initialization of scala.Predef class is lazy heavyweight operation which may cause unexpected slowdown of application and will become a trouble in situations when timing matters (like programming contests).

val a = new Array[Integer](10)
a(5) = 3 //slowdown on this line

So can I turn off it's laziness and force scala.Predef initialization on application start using only scala compiler or VM options without making changes in the code?


Solution

  • No you can't. You can initialize an object by calling it, like this

    Predef    // ensures the body of Predef is initialized
    val a = new Array[Integer](10)
    a(5) = 3
    

    Still, you will probably not have initialized the ArrayOps class which is involved in a.apply. Lazy class initialization is a property of the JVM. If you do benchmarks, that's why you usually give it a "warmup" run first, so that all involved classes have been loaded first.