7173919: Minor optimization of hashing methods
Summary: several minor optimizations to hashing methods used by hash map classes
Reviewed-by: dholmes
--- a/jdk/src/share/classes/java/util/HashMap.java Thu Jun 14 12:13:54 2012 +0100
+++ b/jdk/src/share/classes/java/util/HashMap.java Wed Jun 13 16:48:30 2012 -0700
@@ -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
--- a/jdk/src/share/classes/java/util/Hashtable.java Thu Jun 14 12:13:54 2012 +0100
+++ b/jdk/src/share/classes/java/util/Hashtable.java Wed Jun 13 16:48:30 2012 -0700
@@ -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();
+ }
+
+ 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);
- }
+ // 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;
--- a/jdk/src/share/classes/java/util/WeakHashMap.java Thu Jun 14 12:13:54 2012 +0100
+++ b/jdk/src/share/classes/java/util/WeakHashMap.java Wed Jun 13 16:48:30 2012 -0700
@@ -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
--- a/jdk/src/share/classes/java/util/concurrent/ConcurrentHashMap.java Thu Jun 14 12:13:54 2012 +0100
+++ b/jdk/src/share/classes/java/util/concurrent/ConcurrentHashMap.java Wed Jun 13 16:48:30 2012 -0700
@@ -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.