提交 bb92dc2d 编写于 作者: M mduigou

7173919: Minor optimization of hashing methods

Summary: several minor optimizations to hashing methods used by hash map classes
Reviewed-by: dholmes
上级 27beb6ef
......@@ -288,12 +288,11 @@ public class HashMap<K,V>
* in lower bits.
*/
final int hash(Object k) {
int h = hashSeed;
if (k instanceof String) {
return ((String)k).hash32();
return ((String) k).hash32();
}
h ^= k.hashCode();
int h = hashSeed ^ k.hashCode();
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
......
......@@ -194,19 +194,17 @@ public class Hashtable<K,V>
transient final int hashSeed = sun.misc.Hashing.randomHashSeed(this);
private int hash(Object k) {
int h = hashSeed;
if (k instanceof String) {
return ((String)k).hash32();
} else {
h ^= k.hashCode();
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
int h = hashSeed ^ k.hashCode();
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
/**
......@@ -1015,7 +1013,7 @@ public class Hashtable<K,V>
*/
private static class Entry<K,V> implements Map.Entry<K,V> {
final int hash;
K key;
final K key;
V value;
Entry<K,V> next;
......
......@@ -295,13 +295,11 @@ public class WeakHashMap<K,V>
* otherwise encounter collisions for hashCodes that do not differ
* in lower bits.
*/
int hash(Object k) {
int h = hashSeed;
final int hash(Object k) {
if (k instanceof String) {
return ((String) k).hash32();
} else {
h ^= k.hashCode();
}
int h = hashSeed ^ k.hashCode();
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
......
......@@ -269,13 +269,11 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
* differ in lower or upper bits.
*/
private int hash(Object k) {
int h = hashSeed;
if (k instanceof String) {
return ((String) k).hash32();
}
h ^= k.hashCode();
int h = hashSeed ^ k.hashCode();
// Spread bits to regularize both segment and index locations,
// using variant of single-word Wang/Jenkins hash.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册