Details
-
Type:
Improvement
-
Status:
Closed
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: None
-
Fix Version/s: 1.9-beta-4, 1.8.4, 1.7.11
-
Component/s: None
-
Labels:None
-
Number of attachments :
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);
}
Screenshot of a YJP profiling session. It shows that 96% of thread blocking is caused by EMC.isInitialized() (

GROOVY-3557) and of that 91% of is caused by DTT.booleanUnbox(Object).