RFR JDK-7143928 : (coll) Optimize for Empty ArrayList and HashMap

Mike Duigou mike.duigou at oracle.com
Wed Mar 27 00:25:59 UTC 2013

Hello all;

This is a review for optimization work that came out of internal analysis of Oracle's Java applications. It's based upon analysis that shows that in large applications as much as 10% of maps and lists are initialized but never receive any entries. A smaller number spend a large proportion of their lifetime empty. We've found similar results across other workloads as well. This patch is not a substitute for pre-sizing your collections and maps--doing so will *always* have better results.

This patch extends HashMap and ArrayList to provide special handling for newly created instances that avoids creating the backing array until needed. There is a very small additional cost for detecting when to inflate the map or list that is measurable in interpreted tests but disappears in JITed code. 


We expect that should this code prove successful in Java 8 it will be backported to Java 7 updates.

The unit test may appear to be somewhat unrelated. It was created after resolving a bug in an early version of this patch to detect the issue encountered (LinkedHashMap.init() was not being called in readObject() when the map was empty).


More information about the core-libs-dev mailing list