Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8062849

Optimize EnumMap.equals

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 9
    • 8u25
    • core-libs

        FULL PRODUCT VERSION :


        A DESCRIPTION OF THE PROBLEM :
        In the implementation of java.util.EnumMap.equals, when testing against another EnumMap, it iterates and compares the contents of both maps, but this iteration could/should be skipped whenever the maps are a different size.

        Current code:

            private boolean equals(EnumMap<?,?> em) {
                if (em.keyType != keyType)
                    return size == 0 && em.size == 0;
                
                // Key types match, compare each value
                for (int i = 0; i < keyUniverse.length; i++) {
                    Object ourValue = vals[i];
                    Object hisValue = em.vals[i];
                    if (hisValue != ourValue &&
                        (hisValue == null || !hisValue.equals(ourValue)))
                        return false;
                }
                return true;
            }

        Proposed change:

            private boolean equals(EnumMap<?,?> em) {
                if (size != em.size)
                    return false;
                
                if (keyType != em.keyType)
                    return size == 0;
                
                // Sizes and key types match, compare each value
                for (int i = 0; i < keyUniverse.length; i++) {
                    Object ourValue = vals[i];
                    Object hisValue = em.vals[i];
                    if (hisValue != ourValue &&
                        (hisValue == null || !hisValue.equals(ourValue)))
                        return false;
                }
                
                return true;
            }


        REPRODUCIBILITY :
        This bug can be reproduced always.

              sdrach Steve Drach (Inactive)
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: