Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
2.4
-
None
-
Sun JDK 1.6.0_17
Description
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.