提交 a00f0039 编写于 作者: M mduigou

7173919: Minor optimization of hashing methods

Summary: several minor optimizations to hashing methods used by hash map classes
Reviewed-by: dholmes
上级 8a2efcef
...@@ -288,12 +288,11 @@ public class HashMap<K,V> ...@@ -288,12 +288,11 @@ public class HashMap<K,V>
* in lower bits. * in lower bits.
*/ */
final int hash(Object k) { final int hash(Object k) {
int h = hashSeed;
if (k instanceof String) { 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 // This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded // constant multiples at each bit position have a bounded
......
...@@ -194,12 +194,11 @@ public class Hashtable<K,V> ...@@ -194,12 +194,11 @@ public class Hashtable<K,V>
transient final int hashSeed = sun.misc.Hashing.randomHashSeed(this); transient final int hashSeed = sun.misc.Hashing.randomHashSeed(this);
private int hash(Object k) { private int hash(Object k) {
int h = hashSeed;
if (k instanceof String) { if (k instanceof String) {
return ((String)k).hash32(); return ((String)k).hash32();
} else { }
h ^= k.hashCode();
int h = hashSeed ^ k.hashCode();
// This function ensures that hashCodes that differ only by // This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded // constant multiples at each bit position have a bounded
...@@ -207,7 +206,6 @@ public class Hashtable<K,V> ...@@ -207,7 +206,6 @@ public class Hashtable<K,V>
h ^= (h >>> 20) ^ (h >>> 12); h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4); return h ^ (h >>> 7) ^ (h >>> 4);
} }
}
/** /**
* Constructs a new, empty hashtable with the specified initial * Constructs a new, empty hashtable with the specified initial
...@@ -1015,7 +1013,7 @@ public class Hashtable<K,V> ...@@ -1015,7 +1013,7 @@ public class Hashtable<K,V>
*/ */
private static class Entry<K,V> implements Map.Entry<K,V> { private static class Entry<K,V> implements Map.Entry<K,V> {
final int hash; final int hash;
K key; final K key;
V value; V value;
Entry<K,V> next; Entry<K,V> next;
......
...@@ -295,13 +295,11 @@ public class WeakHashMap<K,V> ...@@ -295,13 +295,11 @@ public class WeakHashMap<K,V>
* otherwise encounter collisions for hashCodes that do not differ * otherwise encounter collisions for hashCodes that do not differ
* in lower bits. * in lower bits.
*/ */
int hash(Object k) { final int hash(Object k) {
int h = hashSeed;
if (k instanceof String) { if (k instanceof String) {
return ((String) k).hash32(); return ((String) k).hash32();
} else {
h ^= k.hashCode();
} }
int h = hashSeed ^ k.hashCode();
// This function ensures that hashCodes that differ only by // This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded // constant multiples at each bit position have a bounded
......
...@@ -269,13 +269,11 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> ...@@ -269,13 +269,11 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
* differ in lower or upper bits. * differ in lower or upper bits.
*/ */
private int hash(Object k) { private int hash(Object k) {
int h = hashSeed;
if (k instanceof String) { if (k instanceof String) {
return ((String) k).hash32(); return ((String) k).hash32();
} }
h ^= k.hashCode(); int h = hashSeed ^ k.hashCode();
// Spread bits to regularize both segment and index locations, // Spread bits to regularize both segment and index locations,
// using variant of single-word Wang/Jenkins hash. // 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.
先完成此消息的编辑!
想要评论请 注册