Wicket Game

A Developers Notebook - Wicket Game

  • Homepage
  • Wicket-CRUDr
  • Imprint
  • About

Automagic optimization in Java - Part II (Method Inlining)

One of the most intuitive optimizations the JVM performs is method inlining. It adresses two performance issues and if kept in mind might lead to a code that's better structured and readable. Methods as a concept are quite expensive. To call a method, the JVM has to find the target for dynamic invocation, has to store the current scope on stack, has to jump to the method in question. After executing the method, the JVM has to retrieve the saved scope from the stack and resume working where it left of. Additionally method calls reduce the effectiveness of other optimization methods.

But thanks to the automatic method inlining that's of (close to) no concern to the developer.

Using method inlining is a double-edged sword for the JVM. While it speeds up the execution and helps other optimizations to perform better, it increases the memory footprint of the application. For the developer, using manual method inlining is a sword that's pretty much single-edged. Its close to blunt on the performance side but will definitely hurt you when it comes to code readability, reusability and general maintainability.

What the JVM does is pretty much the same as what happens when you use the Inline refactoring in your favorite IDE. It replaces the method call with the mathod body of the called method. If this would be done all over the place, the memory footprint of your application would just plain explode, so there are a couple of criteria that the JVM uses to determine which methods will be inlined.

The JVM will generally inline short methods that aren't overriden yet. In older versions, when the JVM wasn't able to undo the optimizations it made, this only applied to methods that were declared final but in the current (as in Java 1.5 and newer) JVMs this restriction was lifted. When the inlined method is overiden later, the inlining will be revoked. Additionally every method that isn't overridden yet and only has one caller will be inlined too.

This last point is interesting since it adresses concerns that are directly linked to code readability issues. Whenever you feel like you could extract some code to a new method to improve readability by replacing a handfull of lines by a single aptly named method call, this method will most likely end up with a single caller and private (which is similar to final in terms of method inlining) and will be inlined by the JVM and thus not affecting the performance at all.

If it has more than one caller or can't be private, you're facing either a design issue or should have refactored the code to a method anyway.

On the other hand, the shortest methods generally found in Java code are getters and setters. These too will bis very likely candidates for inlining. So what's good for code readability and object oriented (encapsuled) design isn't nessecarily bad for the performance. Most common practices don't affect performance at all. In this case it's just refactoring blocks of code to methods and/or the use of lots and lots of small methods (hopefully carrying meaningfull names) but as we'll see in later parts of this series, this is equally true for most if not all the stuff that's part of clean code or readable object oriented code in general.

So even if this part is one of the most important from the point of view of the JVM it's one of the easiest to understand. That's why this description ended up rather short.

Posted by David Hendrix in Performance Comments: (0) Trackbacks: (0)
Defined tags for this entry: java, performance
Related entries by tags:
  • Automagic optimization in Java - Part I (Preface)
  • It's been a slow month
  • Don't take anything for granted - iterating over a java.util.Stack
  • Don't you hate it when your UnitTests break?
  • Create Instances of Annotations (for UnitTests)

Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)

No comments


Add Comment

Standard emoticons like :-) and ;-) are converted to images.
Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
BBCode format allowed
 
Submitted comments will be subject to moderation before being displayed.
 

Archives

  • May 2013
  • April 2013
  • March 2013
  • Recent...
  • Older...

Categories

  • XML Books
  • XML Found and Linked
  • XML Software Development
  • XML Java
  • XML Apache Wicket
  • XML Wicket-CRUDr
  • XML Performance
  • XML Security


All categories

Other reads

Rebel Labs Report - "Pragmatic DevOps: Virtualization & Provisioning with Vagrant & Chef"

Wednesday, May 15. 2013
Automating a complex build process

Wednesday, May 8. 2013
Concurrent garbage collection in Java

Tuesday, May 7. 2013
The great Java app server debate with Tomcat/TomEE & JBoss

Tuesday, May 7. 2013
Fix memory leaks in Java production applications in 5 steps

Tuesday, May 7. 2013

Syndicate This Blog

  • XML RSS 0.91 feed
  • XML RSS 1.0 feed
  • XML RSS 2.0 feed
  • ATOM/XML ATOM 1.0 feed
  • XML RSS 2.0 Comments
  • Add to Google

Blog Administration

Open login screen

Show tagged entries

xml rant
xml java enterprise
xml j2ee
xml dependency injection
xml guildwars assistant
xml wicket
xml gwassist.de
xml wicket-crudr
xml apache
xml apache wicket
xml java
xml software development
 

Layout by David 'Nicktar' Hendrix