-
Enhancement
-
Resolution: Fixed
-
P4
-
8u25
-
b76
-
x86
-
windows_2003
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8136039 | emb-9 | Paul Sandoz | P4 | Resolved | Fixed | team |
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.
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.
- backported by
-
JDK-8136039 Optimize EnumMap.equals
-
- Resolved
-