On Wed, May 2, 2018 at 2:43 PM, Michael Rasmussen <
michael.rasmus...@roguewave.com> wrote:

> But getComponentType itself calls isArray, so you are paying the native
> method overhead anyway (though it is intrinsic).
>

Ohhh, I had forgotten I had looked at  getComponentType earlier.  So
calling getArray directly should not really give any improvement for leaf
nodes.

I tried the variant below, but it gives no performance difference on my
naive non-jmh benchmark.  (you are benchmarking jdk11 head, right)

    public static int deepHashCode(Object a[]) {
        if (a == null)
            return 0;

        int result = 1;

        for (Object element : a) {
            final int elementHash;
            final Class<?> cl;
            if (element == null)
                elementHash = 0;
            else if (!(cl = element.getClass()).isArray())
                elementHash = element.hashCode();
            else if (element instanceof Object[])
                elementHash = deepHashCode((Object[]) element);
            else
                elementHash = primitiveArrayHashCode(element, cl);

            result = 31 * result + elementHash;
        }

        return result;
    }

    private static int primitiveArrayHashCode(Object a, Class<?> cl) {
        return
            (cl == byte[].class)    ? hashCode((byte[]) a)    :
            (cl == int[].class)     ? hashCode((int[]) a)     :
            (cl == long[].class)    ? hashCode((long[]) a)    :
            (cl == char[].class)    ? hashCode((char[]) a)    :
            (cl == short[].class)   ? hashCode((short[]) a)   :
            (cl == boolean[].class) ? hashCode((boolean[]) a) :
            (cl == double[].class)  ? hashCode((double[]) a)  :
            // If new primitive types are ever added, this method must be
            // expanded or we will fail here with ClassCastException.
            hashCode((float[]) a);
    }

Reply via email to