Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-574

HashCodeBuilder append(Object) could be more efficient

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.4
    • 2.5
    • lang.builder.*
    • None
    • Sun JDK 1.6.0_17

    Description

      See: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang3/builder/HashCodeBuilder.java?view=markup

      If you use reflectionHashCode, you will eventually call reflectionAppend(Object) for every field (see lines 859 to 890). There is a long list of instanceOf checks for the various types of arrays. My concern is that having an array field is relatively rare, yet the code does 9 if/else checks before computing the hashCode for one element. I am thinking that it would make a lot of sense to wrap those if/else checks for the various array types in an isArray check (see Class.isArray).

      If you look at the JDK source code for Arrays.deepToString, you will see that it checks if the class is an array before checking the various types. Note that since it gets the Class, it doesn't need to execute instanceOf which is likely more expensive...

      I have not done profiling, but I would not be surprised if short circuiting these checks could improve performance by an order of magnitude. The assumption is that getting the Class of an Object and asking it if it is an array is cheaper than asking instanceOf 9 times.

      Attachments

        Activity

          People

            Unassigned Unassigned
            awhitford Anthony Whitford
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: