Java Magazine, March/April 2016
ORACLE COM JAVAMAGAZINE MARCH APRIL 2016 33 inside java are still unconditionally initialized with final ArrayList and HashMap instances This means that the usage of RestRequest is unchanged No user programs have to add checks for any of its fields being null In practice though we are likely to see that many of the collection instances created will be lazily empty they will have deferred creation of their element arrays providing savings of both memory and CPU cycles In any software system that has been refined over 20 years such as Java its difficult to find any implementation change that is a unilateral benefit for all cases In contemplating this change I made sure that the normal uses of ArrayList and HashMap were not negatively afected The analysis of performance on critical methods like HashMap get typically examines the cost of every Java bytecode and every CPU cycle these are methods that are going to be run trillions of times per year across millions of JVMs Some slight movement of performance cost moving costs from one execution path to a different later path would be acceptable but any degradation in performance would need to be inconsequential and would probably need to be ofset by much larger performance gains elsewhere The analysis of the problem began by looking at application and framework behavior in the hopes that something could be done to reduce the cost of unused collections This type of top down performance analysis is by far the best approach to improving application performance Other opportunities for using laziness have been considered for the Java Collections Framework The most desirable changes would be how HashMaps are built and resized The typical usage pattern of HashMap suggests that the implementation would benefit from using diferent data structures for small maps and for larger maps before the first get operation There are other examples of laziness within the Java class library The most common is to cache the result of hash code computations inside the hashCode method This is used with stellar performance benefits by String and other classes Other caching cases have also been added Some of these caches improve performance by avoiding repeated work others save memory by reusing the same data structures for multiple operations Additional caching cases and other lazy optimizations can be added if proved beneficial in future Java updates There also have been times when a cache was wasteful or actually required too much efort to maintain and it was removed from the implementation In most cases because they dont involve API changes laziness improvements to the Java libraries can be added with significant benefit and little impact Java 8 also introduced a significant new lazy implementation the Streams API This library utilizes laziness as a core principle and frequently delivers much better performance than simpler declarative approaches Laziness is an important optimization that has had substantial benefits in the Java libraries You should strongly consider it if you need to improve the performance of a library thats in use by others and where many of the principal optimizations such as algorithm refinement have already been implemented article Mike Duigou @ mjduigou works on Java based ocean going robots at Liquid Robotics He was previously a developer on the Java Core Libraries team at Oracle and contributed to the core collections and Java 8 lambda libraries Duigou has also enjoyed working on autonomous cars dancing robots and industrial realtime applications learn more How laziness affects the size of an ArrayList allocation
You must have JavaScript enabled to view digital editions.