Java Magazine, March/April 2016
ORACLE COM JAVAMAGAZINE MARCH APRIL 2016 29 inside java if foo null foo bar 3 In this example calling the bar method requires that foo be nonnull If foo is null then attempting to evaluate the second term of this expression would cause a NullPointer Exception The Java lazy evaluation rules ensure that the second term is evaluated only when the first term is true Logical AND terms can be thought of as equivalent to nesting so if foo bar baz is equivalent to if foo if bar if baz This rewriting as nested conditionals also makes it clearer why the unneeded terms are not evaluated Lazy evaluation also applies to logical OR expressions such as if true something The something term of this conditional expression is never evaluated because the value of the expression can be determined before it is evaluated These examples of laziness in expression evaluation are useful in writing eficient and probably just as importantly concise program logic Other forms of laziness are just as beneficial but the connection between decisions made in program flow and the benefit received usually isnt as direct or immediate If the result of a calculation is occasionally or frequently discarded without being used then it makes sense to avoid using the resources required to produce it until it is necessary The most obvious saved resource is CPU cycles but laziness can also save memory in avoided allocations and system resources in avoiding unnecessary files sockets threads database connections and more Depending on the situation these savings can be substantial Implementing laziness can be a critical optimization strategy in improving system performance It improves performance by avoiding unnecessary work rather In a smaller number of applications I found that laziness provided up to a 20 percent reduction in memory usage and a similar reduction in memory churn than improving the efficiency of performing the work Laziness is akin to reducing the number of database queries an application makes by 30 percent as opposed to improving the performance of database queries in the same app by 3 percent Spending your effort on the former if it is feasible is much more efective The Challenge of Lazy Collections The implementation of laziness in the Java Collections Framework which is already quite well optimized came about as a result of analysis of application behavior The Oracle Performance Scalability and Reliability PSR team evaluated the performance of some Oracle frameworks and the applications that ran on those frameworks The PSR team found that it was quite common for both the application and the middleware to allocate ArrayList and HashMap instances that were then never used in the life of the object that contained them About 2 percent of all allocated ArrayList and HashMap instances never received any elements Further analysis found that the collections were used in some cases but werent always needed Some work by the PSR team was done to see if refactoring the application to handle the cases where the collections were needed and
You must have JavaScript enabled to view digital editions.