Details
-
Improvement
-
Status: Closed
-
Major
-
Resolution: Duplicate
-
1.5.6
-
None
-
None
Description
I have hooked up FindBugs (http://findbugs.sourceforge.net/) to report on bugs in a grails project that I am writing.
A number of classes in my app are reporting FindBugs warnings as follows:
Bx: Method com.test.UISession.() invokes inefficient new Long(long) constructor; use Long.valueOf(long) instead
Using new Integer(int) is guaranteed to always result in a new object whereas Integer.valueOf(int) allows caching of values to be done by the compiler, class library, or JVM. Using of cached values avoids object allocation and the code will be faster.
Values between -128 and 127 are guaranteed to have corresponding cached instances and using valueOf is approximately 3.5 times faster than using constructor. For values outside the constant range the performance of both styles is the same.
Unless the class must be compatible with JVMs predating Java 1.5, use either autoboxing or the valueOf() method when creating instances of Long, Integer, Short, Character, and Byte.
I don't have any code that does what the FindBugs error is complaining about, so I took the class file that groovy compiles and decompiled it using Jad to have a look at the source code, which I have attached here.
The offending code appears to be in a static initialiser:
static
{ Long long1; Long long2; Class class1 = com.test.ui.UISession.class; Class class2 = groovy.lang.MetaClass.class; long1 = new Long(0x11a5905dd75L); Long _tmp = long1; __timeStamp = (Long)long1; long2 = new Long(0L); Long _tmp1 = long2; _timeStamp_239_neverHappen1212674334069 = (Long)long2; }Is this a Groovy or a Grails issue. If Groovy could the code be changed so that the constructed Longs are done via Long.valeOf()?