score:53
The JVM doesn't optimize static initializers (which is what this is) to the same level that it optimizes method calls. Unfortunately, when you do a lot of work there, that hurts performance--this is a perfect example of that. This is also one reason why the old Application
trait was considered problematic, and why there is in Scala 2.9 a DelayedInit
trait that gets a bit of compiler help to move stuff from the initializer into a method that's called later on.
(Edit: fixed "constructor" to "initializer". Rather lengthy typo!)
score:41
Code inside a top-level object block is translated to a static initializer on the object's class. The equivalent in Java would be
class Performance{
static{
//expensive calculation
}
public static void main(String[] args){
//use result of expensive calculation
}
}
The HotSpot JVM doesn't perform any optimizations on code encountered during static initializers, under the reasonable heuristic that such code will only be run once.
Source: stackoverflow.com
Related Query
- Why does a small change to this Scala code make such a huge difference to performance?
- Why does the addition of enclosing parentheses change the result of this Scala expression?
- Why does this Scala code throw IllegalAccessError at runtime?
- Why does this code typecheck in Scala 2.11 and what can I do about it?
- Why does a lookup in Map in this particular code take such a surprisingly long time?
- Does it make a difference to the debugger that it is Scala code I'm debugging?
- Scala Equality: Why does this code compile?
- Why does this json4s code work in the scala repl but fail to compile?
- Why does sonar think this scala code is unreachable
- Why does the use of typed variable make this code work
- Scala Why does this code compile?
- Why does this piece of Scala code not compile?
- Why does this Scala code not compile?
- Why this foldLeft scala code does not work?
- Why is this Scala Akka code does not compile?
- Why does this Scala actor code require blocking to work?
- Why is this Scala code with assignment of a val in a parameter working?
- Forward References - why does this code compile?
- Why does groupBy in Scala change the ordering of a list's items?
- Why does this explicit call of a Scala method allow it to be implicitly resolved?
- Why this scala code reports compilation error: recursive value x needs type
- Why does this Scala function compile when the argument does not conform to the type constraint?
- Why this Scala code execute two Futures in one thread?
- Why is this scala code not inferring type?
- Why does Scala fail to find a secondary implicit value in this one particular case?
- Why does this Scala line return a Unit?
- Why does scala (1 to 1000).foreach throw an exception in this case?
- Why is this Scala code slow?
- Why does this Scala for expression using tuples fail to compile?
- scala @switch annotation, does it make any difference to generated byte code?
More Query from same tag
- How to concatenate Char type elements in Scala
- Why does streaming Dataset fail with "Complete output mode not supported when there are streaming aggregations on streaming DataFrames/DataSets... "?
- How to pass parameters to annotation with macro expansions
- Scala: Best Way to Avoid Multiple time doing same operation in different Match-case
- custom folder structure in sbt for scala
- ScalaCheck's generators with ScalaTest's behavior functions
- EitherT filter with error in Scalaz
- How to calculate length of string in a tuple in scala
- Scala: cleaning up constructor parameters
- Scala - concatenate number of Try() results
- Play Framework - add a field to JSON object
- Scala Akka not loading Configuration file with -Dconfig.file option
- Why putStrLn of zio didn't output
- how to create a variable which could accept only certain values?
- IntelliJ - How to run current program regardless of errors in unrelative files
- Format date into another format return wrong date
- overloaded method value ofDim with alternatives in scala
- Which JVM functional languages are well IDE-supported? (IDE: IDEA, Netbeans, Eclipse or similar)
- Generating an image of the Mandelbrot set
- Why does pattern match on case object fail when using -Xfuture
- Why does `ScalaObject` exist?
- The difference between NonFatal and Exception in Scala
- Why Type Check Error are not detected when Pattern Matching on Generic Type in Scala
- Compare nested lists using scala test
- How does this for comprehension build state while assigning to underscore?
- Custom Mapped Column Types In Slick Table Definition
- Query Athena (Add Partition) using using AWS Glue Scala
- Spark RDD vs Dataframe - Data storage
- Map of with object references as keys?
- Type-level flattening of a heterogenous Tree