Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtual Machine Optimization

Tags:

java

jvm

jit

cil

I am messing around with a toy interpreter in Java and I was considering trying to write a simple compiler that can generate bytecode for the Java Virtual Machine. Which got me thinking, how much optimization needs to be done by compilers that target virtual machines such as JVM and CLI?

Do Just In Time (JIT) compilers do constant folding, peephole optimizations etc?

like image 835
grom Avatar asked Aug 21 '08 14:08

grom


2 Answers

Optimisation is what makes JVMs viable as environments for long running applications, you can bet that SUN, IBM and friends are doing their best to ensure they can optimise your bytecode and JIT-compiled code in an efficient a manner as possible.

With that being said, if you think you can pre-optimise your bytecode then it probably won't do much harm.

It is worth being aware, however, that JVMs can tend towards performing better (and not crashing) when presented with just the sort of bytecode the Java compiler tends to construct. It is not unknown for optimisations to be missed or even for the JVM to crash when permutations of bytecode occur that are correct but unlike what would be produced by javac. Hopefully that sort of thing is more in the past now, but may be something to be aware of.

like image 135
Mike Tunnicliffe Avatar answered Oct 25 '22 05:10

Mike Tunnicliffe


Optimising bytecode is probably an oxymoron in most cases

I don't think that's true. Optimizations like hoisting loop invariants and propagating constants can never hurt, even if the JVM is smart enough to do them on its own, by simple virtue of making the code do less work.

like image 43
DrPizza Avatar answered Oct 25 '22 06:10

DrPizza