8245677: Optimize lookups in empty HashMaps
Reviewed-by: jlaskey, redestad
This commit is contained in:
parent
e04e052889
commit
742d35e08a
@ -555,20 +555,19 @@ public class HashMap<K,V> extends AbstractMap<K,V>
|
|||||||
*/
|
*/
|
||||||
public V get(Object key) {
|
public V get(Object key) {
|
||||||
Node<K,V> e;
|
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.
|
* Implements Map.get and related methods.
|
||||||
*
|
*
|
||||||
* @param hash hash for key
|
|
||||||
* @param key the key
|
* @param key the key
|
||||||
* @return the node, or null if none
|
* @return the node, or null if none
|
||||||
*/
|
*/
|
||||||
final Node<K,V> getNode(int hash, Object key) {
|
final Node<K,V> getNode(Object key) {
|
||||||
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
|
Node<K,V>[] tab; Node<K,V> first, e; int n, hash; K k;
|
||||||
if ((tab = table) != null && (n = tab.length) > 0 &&
|
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
|
if (first.hash == hash && // always check first node
|
||||||
((k = first.key) == key || (key != null && key.equals(k))))
|
((k = first.key) == key || (key != null && key.equals(k))))
|
||||||
return first;
|
return first;
|
||||||
@ -594,7 +593,7 @@ public class HashMap<K,V> extends AbstractMap<K,V>
|
|||||||
* key.
|
* key.
|
||||||
*/
|
*/
|
||||||
public boolean containsKey(Object 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;
|
return false;
|
||||||
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
|
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
|
||||||
Object key = e.getKey();
|
Object key = e.getKey();
|
||||||
Node<K,V> candidate = getNode(hash(key), key);
|
Node<K,V> candidate = getNode(key);
|
||||||
return candidate != null && candidate.equals(e);
|
return candidate != null && candidate.equals(e);
|
||||||
}
|
}
|
||||||
public final boolean remove(Object o) {
|
public final boolean remove(Object o) {
|
||||||
@ -1141,7 +1140,7 @@ public class HashMap<K,V> extends AbstractMap<K,V>
|
|||||||
@Override
|
@Override
|
||||||
public V getOrDefault(Object key, V defaultValue) {
|
public V getOrDefault(Object key, V defaultValue) {
|
||||||
Node<K,V> e;
|
Node<K,V> e;
|
||||||
return (e = getNode(hash(key), key)) == null ? defaultValue : e.value;
|
return (e = getNode(key)) == null ? defaultValue : e.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -1157,7 +1156,7 @@ public class HashMap<K,V> extends AbstractMap<K,V>
|
|||||||
@Override
|
@Override
|
||||||
public boolean replace(K key, V oldValue, V newValue) {
|
public boolean replace(K key, V oldValue, V newValue) {
|
||||||
Node<K,V> e; V v;
|
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)))) {
|
((v = e.value) == oldValue || (v != null && v.equals(oldValue)))) {
|
||||||
e.value = newValue;
|
e.value = newValue;
|
||||||
afterNodeAccess(e);
|
afterNodeAccess(e);
|
||||||
@ -1169,7 +1168,7 @@ public class HashMap<K,V> extends AbstractMap<K,V>
|
|||||||
@Override
|
@Override
|
||||||
public V replace(K key, V value) {
|
public V replace(K key, V value) {
|
||||||
Node<K,V> e;
|
Node<K,V> e;
|
||||||
if ((e = getNode(hash(key), key)) != null) {
|
if ((e = getNode(key)) != null) {
|
||||||
V oldValue = e.value;
|
V oldValue = e.value;
|
||||||
e.value = value;
|
e.value = value;
|
||||||
afterNodeAccess(e);
|
afterNodeAccess(e);
|
||||||
@ -1260,8 +1259,7 @@ public class HashMap<K,V> extends AbstractMap<K,V>
|
|||||||
if (remappingFunction == null)
|
if (remappingFunction == null)
|
||||||
throw new NullPointerException();
|
throw new NullPointerException();
|
||||||
Node<K,V> e; V oldValue;
|
Node<K,V> e; V oldValue;
|
||||||
int hash = hash(key);
|
if ((e = getNode(key)) != null &&
|
||||||
if ((e = getNode(hash, key)) != null &&
|
|
||||||
(oldValue = e.value) != null) {
|
(oldValue = e.value) != null) {
|
||||||
int mc = modCount;
|
int mc = modCount;
|
||||||
V v = remappingFunction.apply(key, oldValue);
|
V v = remappingFunction.apply(key, oldValue);
|
||||||
@ -1271,8 +1269,10 @@ public class HashMap<K,V> extends AbstractMap<K,V>
|
|||||||
afterNodeAccess(e);
|
afterNodeAccess(e);
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
|
int hash = hash(key);
|
||||||
removeNode(hash, key, null, false, true);
|
removeNode(hash, key, null, false, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -438,7 +438,7 @@ public class LinkedHashMap<K,V>
|
|||||||
*/
|
*/
|
||||||
public V get(Object key) {
|
public V get(Object key) {
|
||||||
Node<K,V> e;
|
Node<K,V> e;
|
||||||
if ((e = getNode(hash(key), key)) == null)
|
if ((e = getNode(key)) == null)
|
||||||
return null;
|
return null;
|
||||||
if (accessOrder)
|
if (accessOrder)
|
||||||
afterNodeAccess(e);
|
afterNodeAccess(e);
|
||||||
@ -450,7 +450,7 @@ public class LinkedHashMap<K,V>
|
|||||||
*/
|
*/
|
||||||
public V getOrDefault(Object key, V defaultValue) {
|
public V getOrDefault(Object key, V defaultValue) {
|
||||||
Node<K,V> e;
|
Node<K,V> e;
|
||||||
if ((e = getNode(hash(key), key)) == null)
|
if ((e = getNode(key)) == null)
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
if (accessOrder)
|
if (accessOrder)
|
||||||
afterNodeAccess(e);
|
afterNodeAccess(e);
|
||||||
@ -685,7 +685,7 @@ public class LinkedHashMap<K,V>
|
|||||||
return false;
|
return false;
|
||||||
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
|
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
|
||||||
Object key = e.getKey();
|
Object key = e.getKey();
|
||||||
Node<K,V> candidate = getNode(hash(key), key);
|
Node<K,V> candidate = getNode(key);
|
||||||
return candidate != null && candidate.equals(e);
|
return candidate != null && candidate.equals(e);
|
||||||
}
|
}
|
||||||
public final boolean remove(Object o) {
|
public final boolean remove(Object o) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user