SUN fixed this problem a while ago in version 1.4.1, so this page is purely of historical interest. It doesn't apply to current versions of Java.


Double Performance Problem in Java's Hotspot

Plea

If this performance bug affects you, then please vote for bug #4490869 on Sun's java developer website.  (A free registration is required if you haven't already registered.)  The sooner Sun fixes this, the better it will be for all java programmers who use double variables.

Synopsis

The current version of hotspot (client version) does not enforce the 8-byte alignment of 8-byte quantities, such as doubles, on the stack.  It only enforces 4-byte alignment for all quantities.  Thus doubles may, or may not, end up being 8-byte aligned depending on what has previously been allocated on the stack by the current and any prior (calling) methods.  There is also a significant performance penalty for misaligned double variables (at least on x86 processors and probably most others as well).  

Example

I created a small program to demonstrate this bug.  Its a simple program that just compute the sum of the first 32 Fibonacci numbers.  The tests are repeated and timings are printed for each different method.  All the methods use exactly the same technique for generating the Fibonacci numbers; the only difference is how many variables are allocated on the stack and in what order.  You can see if your java runtime suffers from this problem by following the intstructions below to run this test program.
  1. Download this java file
  2. Compile and run it, noting the performance
  3. Uncomment the "int dummyInt;" line in the main() method
  4. Compile and run again, noting the changed performance
  5. Use a text editor to replace all instances of "double" with "float"
  6. Compile and run again, noting much more consistent performance this time

Sample Results

Here are some sample results generated using JDK1.4 on a dual 1.7GHz Pentium 4 machine under Windows 2000.


using floats
using doubles
using doubles (with dummyInt)
computeFib()
1.6s
3.4s
1.3s
computeFibWithFirstInt()
1.6s
3.7s
2.1s
computeFibWithMiddleInt()
1.6s
2.3s
1.7s
computeFibStatic()
1.5s
1.1s
4.3s

There are several things to note in these results:

Workarounds

There are several possible workarounds for this problem, but none of them are very appealing

What to do

Links to related pages



This page was written by Bruce Walter, who is solely responsible for its content.  You can email comments to me at walter_bruce@hotmail.com