Details
-
Improvement
-
Status: Closed
-
Major
-
Resolution: Fixed
-
None
-
None
-
None
Description
In profiling a Grails application, org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.booleanUnbox(Object) is showing up as one of the top "blockers" (groovy.lang.ExpandoMetaClass.isInitialized() is actually causing the blocking since EMC gets always used in Grails).
The booleanUnbox/castToBoolean method should be optimized for the case when the Object is already a boolean. Currently it goes threw a lot of unnecessary layers also for boolean values.
current implementation:
public static boolean castToBoolean(Object object) { // null is always false if (object == null) { return false; } // if the object is not null, try to call an asBoolean() method on the object return (Boolean)InvokerHelper.invokeMethod(object, "asBoolean", InvokerHelper.EMPTY_ARGS); }
improved implementation:
public static boolean castToBoolean(Object object) { // null is always false if (object == null) { return false; } if (object.getClass() == Boolean.class) { // equality check is enough and faster than instanceof check, no need to check superclasses since Boolean is final return ((Boolean)object).booleanValue(); } // if the object is not null, try to call an asBoolean() method on the object return (Boolean)InvokerHelper.invokeMethod(object, "asBoolean", InvokerHelper.EMPTY_ARGS); }