6

Over time, Sun's JVM and JIT have gotten pretty smart. Things that used to be common knowledge as being a necessary micro-optimization are no longer needed, because it gets taken care of for you.

For example, it used to be the case that you should mark all possible classes as final, so the JVM inlines as much code as possible. However now, the JIT knows whether your class is final based on what classes get loaded in at runtime, and if you load a class to make the original one non-final-able, it un-inlines the methods and un-marks it as final.

What other smart micro-optimizations does the JVM or JIT do for you?

EDIT: I made this a community wiki; I'd like to collect these over time.

Alex Beardsley
  • 20,988
  • 15
  • 52
  • 67

3 Answers3

6

It's beyond impressive. All of these are things you can't do in C++ (certainly to the same extent Java does). Keep in mind that early versions of Java started the "slow" reputation by not having these things, and we keep improving significantly over time. This is still a big research area.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • well java might be smart but it still uses alot of ressources – Roch Nov 25 '09 at 13:36
  • 2
    @mnml it's less than what the popular opinion is. to create an object i think they have it down to something like 2 or 3 instructions. yes something like C++ is generally faster, but 'resources' isn't just CPU and memory; it's man hours too. hardware is cheap, people are expensive. over the long term, java will often win out in performance because its JIT is amazing and will optimize the app more than most C++ apps could ever dream of. – Alex Beardsley Apr 19 '10 at 20:07
  • 1
    @Alex wish I could give you rep for pointing out people are resources. As terrible as it is to believe that. Your time as an employee will often cost the company more money than say springing for an extra terabyte in the HDD or that better CPU in ye olde web servier. – Tim Bender Jan 28 '11 at 18:51
  • C++ doesn't need escape analysis for fast allocation (on the stack or in registers) of local vars with automatic storage. `int foo(){ struct bar myvar(); }` just uses stack space. Returning a pointer to it (or at least using such a pointer) would be undefined behaviour because the object lifetime ends at the end of the function. Unlike Java, where not using using dynamic allocation is just an invisible optimization, not something you have to manage manually. So that one's kind of tied up in the fundamental differences in Java's managed GCed memory model vs. C++'s manual management. – Peter Cordes Apr 08 '22 at 09:28
4

Oracle has a wiki on Performance techniques used in the Hotspot JVM.

Alex Beardsley
  • 20,988
  • 15
  • 52
  • 67
Brett Kail
  • 33,593
  • 2
  • 85
  • 90
3

Java is smarter at inlining as it can

  • inline code only available at runtime or even dynamically generated.
  • inline virtual methods (up to two at once)
  • perform escape analysis on inlined methods and the methods they were inlined to. (Much harder to do in C++)
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130