29 February 2008

Tags: idea intellij performance tip

Advanced Java programmers are aware that try/catch statements cost many CPU cycles, so it is generally a bad idea to catch exceptions in loop statements. Moreover, even with profilers, it may be hard to find out that a try/catch statement is responsible for a performance bottleneck : you have to take a look at your code, and figure out whether it should be optimized or not. In many cases, you should definitely avoid using try/catch statements in loops.

Fortunately, IntelliJ IDEA has this powerful structural search feature. Let’s explain how to use it in order to find those possible bottlenecks. Structural search is powerful as it allows you to describe ``patterns'' of code to match. No matter the indentation, no matter the statements, you’ll just lookup for structural matches.

In our case, you need to look for something that ``looks like'' this :

for () {
  try {
  } catch () {
  }
}

The corresponding structural search pattern will be :

for ($decl$;$condition$;$increment$) {
 $beforeStatements$;
 try {
  $TryStatement$;
 } catch ($exceptionType$ $ex$) {
  $CatchStatement$;
 }
 $afterStatements$;
}

To use this, either click on the ``search structurally'' item in the search menu, or press Ctrl+Shift+S and enter the pattern :

image

+
+
Then you need to tweak the variables in order to specify the cardinalities. Click on ``Edit variables'', and update :
  • beforeStatements : minimum count 0, maximum count unlimited

  • afterStatements : minimum count 0, maximum count unlimited

  • tryStatement : minimum count 1, maximum count unlimited

  • catchStatement : minimum count 0, maximum count unlimited

The other variables may stay as defaults (min 1, max 1).

Now click on ``Find'' and you’ll probably find some of those nasty constructs. The previous patterns works for classical for loops, you need to tune it to work for Java 5 for each constructs, and don’t forget while loops too.