Best (scala and other java vm-oriented languages) optimizations

I just read about scala implementing generics specialization , and it really caught my attention to increasing the speed that they achieved with this function. I was wondering what other functions have languages ​​designed for java vm that really make them work better than java? I know that the farther the generated code moves away from java, the further its performance drops. So I really wonder what other functions the language can implement to achieve better performance in java.

Please do not answer this question when talking about scala really nice features on top of java, I am talking strictly about performance.

Also, if you have suggestions that have not yet been implemented, answer!

Thanks!

+6
java performance optimization programming-languages scala
source share
3 answers

Scala does support tail call optimization , which Java, only through the JVM, does not fully support it yet .

+13
source share

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.

+5
source share

This may be too trivial / old / well known, but the Java compiler optimizes string literals and concatenations using the String and StringBuilders pool:

  String a = "a"; String b = "a"; String c = a + b + someUserInput; 

really be closer to

  String a = "a"; String b = a; String a_b = "aa"; String c = new StringBuilder(a_b).append(someUserInput).toString(); 

or maybe even (not sure)

  String a_b = "aa"; String a = a_b.substring(0,1); String b = a; String c = new StringBuilder(a_b).append(someUserInput).toString(); 

In addition, the optimization focus for the Java compiler is shifted from compilation to bytecode (javac) to compilation from bytecode to native code (Hotspot). I think there were more optimizations in javac, but they found this to be a bit premature, given that Hotspot can do a lot more thorough work here (and use the runtime knowledge of the actual hardware and usage patterns).

Another interesting aspect of this is that optimizing Hotspot can improve performance after writing and compiling code. For example, the StringBuilder optimization above used to use the (slightly less efficient) StringBuffer class to Java 5. To get the latest improvements, you will need to recompile the code (which is still better than manually optimizing to use StringBuffer before, in which case you really you will need to update the code).

+3
source share

All Articles