Scala optimizes things like
val nullSafeToString = "" + foo
to
val nullSafeToString = String.valueOf(foo)
Java doesn't work, in Java it looks like
val nullSafeToString = new StringBuilder("").append(foo)
which takes 4 times more bytecode than Scala simpler code, and creates a new instance of StringBuilder and a useless String .
On the one hand, the JVM was created to support what they planned to use in Java, even if the JVM runs only bytecode.
There are features that have been missing for centuries to better support functional (tailcalls) and untyped languages (InvokeDynamic).
But on the other hand, I am surprised how painless the JIT compiler can optimize highly abstracted code and layers of indirection and ultimately with code that runs as fast as Java code.
I think that the “Pimp My Library” template would not be so popular if the JVM could not remove the created instances of these simple implicit + class RichFoo by analyzing the escape code.
soc
source share