Java Reference
In-Depth Information
If you have some experience with Java performance, you may be surprised that compilation has
been discussed for an entire chapter without mentioning the final keyword. In some circles, the
final keyword is thought to be an important factor in performance because it is believed to allow
the JIT compiler to make better choices about inlining and other optimizations.
That idea may have had some validity in the dim past, but it has not been true for many, many
years (if it ever was). Still, it is a persistent rumor. For the record, then, you should use the final
keyword whenever it makes sense: for an immutable object or primitive value you don't want to
change, for parameters to certain inner classes, and so on. But the presence or absence of the fi-
nal keyword will not affect the performance of an application.
This chapter also contains a lot of background about how the compiler works. One reason for
this is so you can understand some of the general recommendations made in Chapter 1 re-
garding small methods and simple code, and the effects of the compiler on microbenchmarks
that were described in Chapter 2 . In particular:
1. Don't be afraid of small methods—and in particular getters and setters—because they
are easily inlined. If you have a feeling that the method overhead can be expensive,
you're correct in theory (we showed that removing inlining has a huge impact on per-
formance). But it's not the case in practice, since the compiler fixes that problem.
2. Code that needs to be compiled sits in a compilation queue. The more code in the
queue, the longer the program will take to achieve optimal performance.
3. Although you can (and should) size the code cache, it is still a finite resource.
4. The simpler the code, the more optimizations that can be performed on it. Profile
feedback and escape analysis can yield much faster code, but complex loop structures
and large methods limit their effectiveness.
Finally, if you profile your code and find some surprising methods at the top of your pro-
file—methods you expect shouldn't be there—you can use the information here to look into
what the compiler is doing and to make sure it can handle the way your code is written.
Search WWH ::

Custom Search