A recent profling session pointed out that some of our processing threads were blocking on java.util.Date construction. This is troubling, because it's something we do many thousands of times per second, and blocked threads are pretty bad!
A bit of digging led me to TimeZone.getDefault(). This, for some insanely fucked up reason, makes a synchronized call to TimeZone.getDefaultInAppContext(). The call is synchronized because it attempts to load the default time zone from the sun.awt.AppContext. What. The. Fuck. I don't know what people were smoking when they wrote this, but I hope they enjoyed it ...
Unfortunately, Date doesn't have a constructor which takes a TimeZone argument, so it always calls getDefault() instead.
I decided to run some microbenchmarks. I benchmarked four different ways of creating Dates:
// date-short: new Date();
//date-long: new Date(year, month, date, hrs, min, sec);
// calendar: Calendar cal = Calendar.getInstance(TimeZone); cal.set(year, month, date, hourOfDay, minute, second) cal.getTime();
// cached-cleared-calendar: // Same as calendar, but with Calendar.getInstance() outside of the loop, // and a cal.clear() call in the loop.
I tested single threaded performance, where 1M Dates were created using each method in a single thread. Then multi-threaded with 4 threads, each thread creating 250k Dates. In other words: both methods ended up creating the same number of Dates.
With exception of date-long, all methods speed up by a factor of 2 when multi-threaded. (The machine only has 2 physical cores). The date-long method actually slows down when multi-threaded. This is because of lock contention in the synchronized TimeZone acquisition.
The JavaDoc for Date suggests replacing the date-long call by a calendar call. Performance-wise, this is not a very good suggestion: its single-threaded performance is twice as bad as that of Date unless you reuse the same Calendar instance. Even multi-threaded it's outperformed by date-long. This is simply not acceptable.
Fortunately, the cached-cleared-calendar option performs very well. You could easily store a ThreadLocal reference to an instance of a Calendar and clear it whenever you need to use it.
More important than the raw duration of the Date creation, is the synchronization overhead. Every time a thread has to wait to enter a synchronized block, it could end up being rescheduled or swapped out. This reduces the predictability of performance. Keeping synchronization down to a minimum (or zero, in this case) increases predictability and liveness of the application in general.
Before anyone mentions it: yes, I'm aware that the long Date constructors are deprecated. Unfortunately, they are what Joda uses when converting to Java Dates. I've proposed a patch, but while doing a bit more research for this blog post, I've come to the conclusion that my patch needs a bit of refining as it is still too slow (though it no longer blocks). In the mean while, I hope that the -kind?- folks at Oracle will reconsider their shoddy implementation.
I've also heard rumours that Joda will somehow, magically, replace java.util.Date in JDK 8. Not sure how that's going to work with backwards compatibility. I'd be much happier if java.util.Date would stop sucking quite as much. And if SimpleDateFormat were made thread-safe. And ... the list goes on.