7

When you want to squeeze the last bit of performance from your code, you would want to utilize the JIT optimization as best as you can. For example, marking method final for easy method inlining , avoid polymorphism in critical places, etc.

But I couldn't find any reference or list of options that a java programmer can use to 'hint' the JIT compiler for faster code? Shouldn't we have a list of 'best programming' practice for low latency performance from JIT ?

3
  • Not an answer to your question, but if you're interested in high performance Java, you might be interested in the little-known fastutil library. It's a high performance extension of the collections framework. Commented Jun 30, 2011 at 23:44
  • Can you clarify why you are interested in this? Do you have a performance issue? or is it purely academic? :-) Commented Jun 30, 2011 at 23:52
  • The performance issues I have could mostly be fixed by tuning the GC or improving the algorithm. However, if I had to go beyond that, for example image processing, pattern recognition etc, I would like to know what are the other performance optimization tools I have in my hand. You can call it academic. Commented Jul 1, 2011 at 2:23

2 Answers 2

11

The best way to write JIT-friendly code is to write straight, simple code because this is what the JIT looks for and knows how to optimize. No tricks!

Also different JVM's have different JIT's, so in order to be certain your code works well with all of them you must not rely on any of them.

The usual way to improve JIT-performance is through external configuration of the JVM. As most JVM's these days know how to inline code small method calls directly, most performance gains come from configuring the garbage collector. Here much effort is used in avoiding having to stop your program while collecting, and you can tweak quite a bit with your knowledge of how the underlying hardware is configured and what works better than others. But not the Java code, it needs to be straight and simple.

Sign up to request clarification or add additional context in comments.

7 Comments

I'd recommend trying to find the slides or PDF for the "Java Technology Performance Myths Exposed" JavaOne talk by Cliff Click (Hotspot hacker, ex-Sun, been at Azul Systems for a while). His blog also always has interesting JVM internals discussions. And, btw, the "make methods final to help with inlining" "optimization" is just a myth.
@vanza Well the slides are extremely hard to find. But there's at least one advantage of a final method: The JIT doesn't have to infer somehow that there's no adequate subclass but can always avoid the dynamic invoke - which should gain some small performance gains in some cases. Not that that will be noticeable but hey ;)
@Voo: Hotspot already avoids dynamic invokes whenever possible (e.g., interface or abstract class with single concrete class loaded that implements it), the "final" is not needed. In the spirit of the original message, and what I've always heard from JIT hackers: use "final" (and other language features) when it makes sense in your code, not because you think it will make your code faster.
@Voo The JVM classloader already knows what classes have been loaded. If a subclass has never been loaded then there's no need for dynamic runtime method lookup.
@Voo & @vanza, I always knew that final adds performance benefit until recently I read something about JVM tricks to avoid polymorphism. My question is, how do I learn these things/tricks and know what to optimize and what not to optimize? I am mostly interested in Sun JVM which is mostly used in production systems.
|
7

The HotSpot authors and others have repeatedly stated that the best approach is to write simple straightforward code.

1 Comment

+1: The JIT looks for common patterns it knows how to optimise. If you write confusing code or code with unusual constructs, it will confuse the JIT (which is easily done, its just a program) and it won't optimise the code as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.