EDIT: See comments to this answer for answer to this question. TLDR: The bottleneck is scaling the image, but profiling shows this as a problem in the ImageIcon constructor.
$ java -version
Picked up _JAVA_OPTIONS: -Dswing.aatext=true -Dawt.useSystemAAFontSettings=on
java version "1.7.0_45"
OpenJDK Runtime Environment (IcedTea 2.4.3) (ArchLinux build 7.u45_2.4.3-1-x86_64)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)
My application loads 1,380 icons at startup. The loading code is shown below. Using an SSD disk, the ImageIcon constructor calls is actually more expensive than the disk reads (the disk reads might use some cache though..)
Loading the icons takes 5.4s, which 3.2s is spent in the ImageIcon constructor.
Is there a way to speed up ImageIcon construction or use something else?
private ImageIcon setIcon(LogicalIcon iconInfo, int size) {
ImageIcon icon = iconImages.get(iconInfo.getIconName());
if(icon == null) {
BufferedImage image = null;
try {
image = ImageIO.read(getBestPreviewIcon(iconInfo).getFile());
icon = new ImageIcon(ImageUtils.scaleDownTo(image, new Dimension(size, size)));
iconImages.put(iconInfo.getIconName(), icon);
} catch (IOException e) {
// FIXME: Add a "Missing Image" icon
e.printStackTrace();
}
}
setIcon(icon);
return icon;
}