What are general tips or golden rules to speed up your Swing
application? In
other words, where exactly should you look for if instructed to "speed
up"
your swing application. (SWT and AWT are not the available alternatives!
)
TIA,
- Manish
Probably a question that should be asked on comp.lang.java.gui, but here
goes:
If you have an existing application that you are trying to tune you really
need a good profiler to tell you where the bottle necks are, but here's a
few tips to get you started (in no particular order):
1. Responsiveness
Without actually improving the performance of your code you can make your
GUI appear faster by providing feedback to the user. Appropriate use of
progress bars and wait cursors will at least let the user know that
something is happening and there are articles that claim the user
perceives a 20% performance increase from this alone. I don't know if
this is accurate but the user will be less irritated if they know what's
happening.
2. Data Models
When using components that require you to provide a data model (such as
JTable, JTree or JList) make sure you have an efficient one. For JTables
in particular, it is rarely a good idea to use the default model. The
getValueAt method is a good place to start looking, this needs to be a
cheap call.
3. Renderers
I have seen some pretty horrific custom cell rendering code for tables.
The cell renderers are invoked for every visible cell in the table each
time the table is repainted. This can result in a ridiculous number of
calls to the getTableCellRendererComponent method, so the rendering code
needs to be as fast as possible. Only do the stuff that is absolutely
necessary in the renderer call (i.e. that stuff that changes from cell to
cell). Prepare everything else in advance. Use caching to avoid
repeating calculations or object allocation. You can usually write your
renderer code in such a way that you don't have to allocate any objects in
the actual renderer call. This advice applies to other components that
may use custom renderers, such as JTree, JLists, JComboBoxes, etc., as
well as to JTables.
4. Custom Painting
If you implement your own painting code for a component it is a good idea
to profile it. Much of the advice for renderers applies for other
painting as well. Your paint routines will be invoked frequently.
5. Garbage Collection
Tuning the settings on your VM's garbage collector will help eliminate
long pauses (possibly at the expense of overall throughput). For GUIs
this is important as a long pause with the application being unresponsive
will irritate the user.
6. Threading
Intelligent use of threads can make a big difference to the responsiveness
and therefore the perceived performance of your GUI. Don't do everything
on the event dispatch thread, consider delegating expensive processing
that doesn't directly update the GUI to other threads. You will need to
be aware of the threading issues in Swing and make appropriate use of the
methods in SwingUtilities such as invokeLater and invokeAndWait.
Hope that helps. Not all of these will be issues in your GUI, you really
need a profiler such as JProbe, or at least put some timing code in your
classes, to make sure you are optimising the right areas.
Dan.