|
|
|
Contrary to the implication of many tips,
methods declared as final
cannot be safely inlined by the compiler,
because the method could have a non-final
declaration at runtime.
To see why, suppose the compiler looks at
class A and subclass B, and sub-subclass C and sees a final
method in A which
it inlines into C. But then at runtime the versions loaded for A and B are different
and the method is not final
in A, and overridden in B. Then C uses the
incorrectly inlined version. This was a bug in some of the early compilers when used with
the -O option. So tips suggesting using final
and the -O option are not valid tips.
On the other hand, final
methods can be inlined after being loaded
into the JVM, because at that point the JVM knows definitely that the method is
final
. So compilers that operate after class loading, such as JIT compilers,
can take advantage of final
methods. Consequently, methods declared
final
could have some performance benefit.
Generally, I wouldn't go out of my way to declare a method or class final
purely for performance reasons. Only after you've definitely identified a performance
problem is this even worth considering.
On the other hand, final
variables,
especially static final
variables, are well worth using as standard.
The following pages have their detailed tips extracted below
The following detailed tips have been extracted from the raw tips page
http://www.oreilly.com/catalog/javapt/chapter/ch04.html
Chapter 4 of "Java Performance Tuning", "Object Creation". (Page last updated September 2000, Added 2000-10-23, Author Jack Shirazi, Publisher O'Reilly). Tips:
final
modifier on instance-variable definitions to create immutable internally accessible objects.
http://www.cs.cmu.edu/~jch/java/optimization.html
For years, Jonathan Hardwick's old but classic site was the only coherent Java performance tuning site on the web. He built it while doing his PhD. It wasn't updated beyond March 1998, when he moved to Microsoft, but most tips are still useful and valid. The URL is for the top page, there are another eight pages. Thanks Jonathan. (Page last updated March 1998, Added 2000-10-23, Author Jonathan Hardwick, Publisher Hardwick). Tips:
http://library.cs.tuiasi.ro/programming/java/cutting_edge_java_game_programming/ewtoc.html
"Cutting Edge Java Game Programming". Oldish but still useful intro book to games programming using Java. (Page last updated 1996, Added 2001-06-18, Author Neil Bartlett, Steve Simkin , Publisher Coriolis). Tips:
http://www.glenmccl.com/jperf/
Glen McCluskey's paper with 30 tuning tips, now free. (Page last updated October 1999, Added 2000-10-23, Author Glen McCluskey, Publisher McCluskey). Tips:
final
classes can be faster.
http://www.javaworld.com/javaworld/jw-04-2001/jw-0406-syslog.html
Using the Syslog class for logging (Page last updated April 2001, Added 2001-04-20, Author Nate Sammons, Publisher JavaWorld). Tips:
static final boolean
s to wrap logging statements so that they can be easily turned off or eliminated.
http://www.bastie.de/resource/res/mjp.pdf and http://www.bastie.de/java/mjperformance/contents.html
Performance tuning report in German. Thanks to Peter Kofler for extracting the tips. (Page last updated November 2001, Added 2001-07-20, Author Sebastian Ritter, Publisher Ritter). Tips:
http://jsl.jcon.org/javaperformance.html
An assortment of tips (Page last updated 2000, Added 2000-10-23, Author Curt Smith, Publisher Smith). Tips:
http://www.sys-con.com/java/article.cfm?id=1149
Performance tuning (Page last updated September 2001, Added 2001-10-22, Author James McGovern, Publisher Java Developers Journal). Tips:
http://www-1.ibm.com/servers/eserver/zseries/software/java/perform.html
Both Java specific and Java on OS/390 tips. (Page last updated 2000, Added 2000-10-23, Author ?, Publisher IBM). Tips:
http://www-106.ibm.com/developerworks/ibm/library/i-tuning/?open
Tuning the IBM JVM and Linux (Page last updated May 2001, Added 2001-10-22, Authors Duc Vianney and James Phelan, Publisher IBM). Tips:
http://www.awlonline.com/product/0,2627,0201616467,00.html
Peter Haggar's Practical Java Programming Language Guide. (Page last updated 2000, Added 2001-01-19, Author Peter Haggar, Publisher Addison-Wesley). Tips:
http://www-106.ibm.com/developerworks/java/library/j-jtctips/j-jtc0319a.html
Finalizers (Page last updated March 2002, Added 2002-04-26, Author Phil Vickers, Publisher IBM). Tips:
http://www.protomatter.com/nate/java-optimization/
Various tips. (Page last updated 1999?, Added 2000-10-23, Author Nate Sammons, Publisher Sammons). Tips:
http://www.joot.com/articles/practices.html
Good Java practices, some of which are good for performance. (Page last updated January 2001, Added 2001-01-19, Author Dave Jarvis, Publisher JOOT). Tips:
final
classes.
http://www-4.ibm.com/software/os/warp/performance/javatip.htm
IBM's list of Java performance tuning tips (same page, two URLs). (Page last updated 2000, Added 2000-10-23, Author ?, Publisher IBM). Tips:
http://www.as400.ibm.com/developer/java/faq/perffaq.html
Some IBM Java performance tips. Although intended for AS/400 Java, many tips are generally applicable (Page last updated ?, Added 2000-10-23, Author ?, Publisher IBM). Tips:
static final
when creating constants.