Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-5059

Improve DefaultTypeTransformation.booleanUnbox performance

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.9-beta-4, 1.8.4, 1.7.11
    • 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);
          }
      

      Attachments

        1. Valinta_113.jpeg
          240 kB
          Lari Hotari

        Activity

          People

            blackdrag Jochen Theodorou
            lhotari Lari Hotari
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: