7173919: Minor optimization of hashing methods
Summary: several minor optimizations to hashing methods used by hash map classes
Reviewed-by: dholmes
diff --git a/src/share/classes/java/util/HashMap.java b/src/share/classes/java/util/HashMap.java
index 2191b98..4687905 100644
--- a/src/share/classes/java/util/HashMap.java
+++ b/src/share/classes/java/util/HashMap.java
@@ -288,12 +288,11 @@
* 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
diff --git a/src/share/classes/java/util/Hashtable.java b/src/share/classes/java/util/Hashtable.java
index 593c171..f82c300 100644
--- a/src/share/classes/java/util/Hashtable.java
+++ b/src/share/classes/java/util/Hashtable.java
@@ -194,19 +194,17 @@
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 @@
*/
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;
diff --git a/src/share/classes/java/util/WeakHashMap.java b/src/share/classes/java/util/WeakHashMap.java
index ed9e1ad..26e22aa 100644
--- a/src/share/classes/java/util/WeakHashMap.java
+++ b/src/share/classes/java/util/WeakHashMap.java
@@ -295,13 +295,11 @@
* 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
diff --git a/src/share/classes/java/util/concurrent/ConcurrentHashMap.java b/src/share/classes/java/util/concurrent/ConcurrentHashMap.java
index deddc7f..66ae516 100644
--- a/src/share/classes/java/util/concurrent/ConcurrentHashMap.java
+++ b/src/share/classes/java/util/concurrent/ConcurrentHashMap.java
@@ -269,13 +269,11 @@
* 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.