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

Optimize EnumMap.equals

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 8u25
    • Fix Version/s: 9
    • Component/s: core-libs

      Backports

        Description

        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.

          Attachments

            Issue Links

              Activity

                People

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

                  Dates

                  Created:
                  Updated:
                  Resolved: