<p>I am messing around with <a href="http://code.google.com/p/zemscript/" rel="noreferrer">a toy interpreter in Java</a> 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?</p>

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

[{'answer_id': 20141, 'author': 'Skizz', 'author_id': 1898, 'author_profile': 'https://Stackoverflow.com/users/1898', 'pm_score': 0, 'selected': False, 'text': '<p>Optimising bytecode is probably an oxymoron in most cases. Unless you control the VM, you have no idea what it does to speed up code execution, if anything. The compiler would need to know the details of the VM in order to generate optimised code.</p>\n'}, {'answer_id': 20151, 'author': 'DrPizza', 'author_id': 2131, 'author_profile': 'https://Stackoverflow.com/users/2131', 'pm_score': 2, 'selected': False, 'text': "<blockquote>\n  <p>Optimising bytecode is probably an oxymoron in most cases</p>\n</blockquote>\n\n<p>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.</p>\n"}, {'answer_id': 20870, 'author': 'dlinsin', 'author_id': 198, 'author_profile': 'https://Stackoverflow.com/users/198', 'pm_score': 4, 'selected': True, 'text': '<p>I\'m just gonna add two links which explain <a href="http://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/" rel="nofollow noreferrer">Java\'s bytecode</a> pretty well and some of the <a href="http://www.ibm.com/developerworks/java/library/j-benchmark1.html" rel="nofollow noreferrer">various optimization</a> of the JVM during runtime.</p>\n'}, {'answer_id': 78690, 'author': 'Mike Tunnicliffe', 'author_id': 13956, 'author_profile': 'https://Stackoverflow.com/users/13956', 'pm_score': 3, 'selected': False, 'text': "<p>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.</p>\n\n<p>With that being said, if you think you can pre-optimise your bytecode then it probably won't do much harm.</p>\n\n<p>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.</p>\n"}, {'answer_id': 374308, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>Obfuscators such as ProGuard will perform many static optimisations on your bytecode for you.</p>\n'}, {'answer_id': 377671, 'author': 'Nathaniel Flath', 'author_id': 41241, 'author_profile': 'https://Stackoverflow.com/users/41241', 'pm_score': 1, 'selected': False, 'text': '<p>The HotSpot compiler will optimize your code at runtime better than is possible at compile-time - it has more information to work with, after all.  The only time you should be optimizing the bytecode instead of just your algorithm is when you are targeting mobile devices, such as the Blackberry, where the JVM for that platform is not powerful enough to optimize code at runtime and just executes the bytecode.</p>\n'}, {'answer_id': 402571, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Note to Aseraphim:</p>\n\n<p>It can also be useful to optimise bytecode for non-embedded applications in some limited cases:</p>\n\n<ol>\n<li><p>When delivering code over the wire, eg for WebStart apps, to minimise deliverable/cache size and because you don't necessarily know the capability/speed of the client.</p></li>\n<li><p>For code that you know is performance critical and used at start-up before (say) HotSpot has had time to gather any stats.</p></li>\n</ol>\n\n<p>Again, the transformations that a good optimiser/obfuscator performs can be very helpful.</p>\n"}]
['https://Stackoverflow.com/questions/20127', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/486/']