8245677: Optimize lookups in empty HashMaps

Reviewed-by: jlaskey, redestad
This commit is contained in:
Christoph Dreis 2020-05-25 22:21:45 +02:00 committed by Claes Redestad
parent e04e052889
commit 742d35e08a
2 changed files with 16 additions and 16 deletions

View File

@ -555,20 +555,19 @@ public class HashMap<K,V> extends AbstractMap<K,V>
*/
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
return (e = getNode(key)) == null ? null : e.value;
}
/**
* Implements Map.get and related methods.
*
* @param hash hash for key
* @param key the key
* @return the node, or null if none
*/
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
final Node<K,V> getNode(Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n, hash; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
(first = tab[(n - 1) & (hash = hash(key))]) != null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
@ -594,7 +593,7 @@ public class HashMap<K,V> extends AbstractMap<K,V>
* key.
*/
public boolean containsKey(Object key) {
return getNode(hash(key), key) != null;
return getNode(key) != null;
}
/**
@ -1105,7 +1104,7 @@ public class HashMap<K,V> extends AbstractMap<K,V>
return false;
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
Object key = e.getKey();
Node<K,V> candidate = getNode(hash(key), key);
Node<K,V> candidate = getNode(key);
return candidate != null && candidate.equals(e);
}
public final boolean remove(Object o) {
@ -1141,7 +1140,7 @@ public class HashMap<K,V> extends AbstractMap<K,V>
@Override
public V getOrDefault(Object key, V defaultValue) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? defaultValue : e.value;
return (e = getNode(key)) == null ? defaultValue : e.value;
}
@Override
@ -1157,7 +1156,7 @@ public class HashMap<K,V> extends AbstractMap<K,V>
@Override
public boolean replace(K key, V oldValue, V newValue) {
Node<K,V> e; V v;
if ((e = getNode(hash(key), key)) != null &&
if ((e = getNode(key)) != null &&
((v = e.value) == oldValue || (v != null && v.equals(oldValue)))) {
e.value = newValue;
afterNodeAccess(e);
@ -1169,7 +1168,7 @@ public class HashMap<K,V> extends AbstractMap<K,V>
@Override
public V replace(K key, V value) {
Node<K,V> e;
if ((e = getNode(hash(key), key)) != null) {
if ((e = getNode(key)) != null) {
V oldValue = e.value;
e.value = value;
afterNodeAccess(e);
@ -1260,8 +1259,7 @@ public class HashMap<K,V> extends AbstractMap<K,V>
if (remappingFunction == null)
throw new NullPointerException();
Node<K,V> e; V oldValue;
int hash = hash(key);
if ((e = getNode(hash, key)) != null &&
if ((e = getNode(key)) != null &&
(oldValue = e.value) != null) {
int mc = modCount;
V v = remappingFunction.apply(key, oldValue);
@ -1271,8 +1269,10 @@ public class HashMap<K,V> extends AbstractMap<K,V>
afterNodeAccess(e);
return v;
}
else
else {
int hash = hash(key);
removeNode(hash, key, null, false, true);
}
}
return null;
}

View File

@ -438,7 +438,7 @@ public class LinkedHashMap<K,V>
*/
public V get(Object key) {
Node<K,V> e;
if ((e = getNode(hash(key), key)) == null)
if ((e = getNode(key)) == null)
return null;
if (accessOrder)
afterNodeAccess(e);
@ -450,7 +450,7 @@ public class LinkedHashMap<K,V>
*/
public V getOrDefault(Object key, V defaultValue) {
Node<K,V> e;
if ((e = getNode(hash(key), key)) == null)
if ((e = getNode(key)) == null)
return defaultValue;
if (accessOrder)
afterNodeAccess(e);
@ -685,7 +685,7 @@ public class LinkedHashMap<K,V>
return false;
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
Object key = e.getKey();
Node<K,V> candidate = getNode(hash(key), key);
Node<K,V> candidate = getNode(key);
return candidate != null && candidate.equals(e);
}
public final boolean remove(Object o) {