7173919: Minor optimization of hashing methods

Several minor optimizations to hashing methods used by hash map classes

Reviewed-by: dholmes
This commit is contained in:
Mike Duigou 2012-06-13 16:48:30 -07:00
parent c580cfee80
commit 4824bf600d
4 changed files with 14 additions and 21 deletions

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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.