title: Why was the default hashCode generation in jvm switched to xor-shift in JDK 8?
body: <p>I am learning JVM code to understand Java deeper. In <a href="http://hg.openjdk.java.net/jdk/jdk13/file/9c250a7600e1/src/hotspot/share/runtime/synchronizer.cpp" rel="nofollow noreferrer"><code>synchronizer.cpp</code></a> (in <a href="http://hg.openjdk.java.net/jdk/jdk13/file/9c250a7600e1/src/hotspot/share/runtime/synchronizer.cpp#l641" rel="nofollow noreferrer">the <code>get_next_hash</code> method</a>) there is a comment that says:</p>
  
  <blockquote>
    <p>// Marsaglia's xor-shift scheme with thread-specific state<br>
       // This is probably the best overall implementation -- we'll<br>
       // likely make this the default in future releases.</p>
  </blockquote>
  
  <p>This is found in the in else branch when variable <code>hashcode</code> is not any of (0,1,2,3,4). this variable can be set via the JVM option "-XX:hashcode=n".</p>
  
  <p>I wrote some code to test those hash algorithms: </p>
  
  <pre><code>public static void main(String[] args) {
      long now = System.currentTimeMillis();
      RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
      List&lt;String&gt; arguments = runtimeMxBean.getInputArguments();
      for(String s:arguments){
          System.out.println(s);
      }
  
      HashMap&lt;Integer,Object&gt; h = new HashMap&lt;&gt;();
  
      ArrayList&lt;Object&gt; arrayList = new ArrayList&lt;&gt;();
  
      for(int i=0;i&lt;2000000;i++){
          Object o = new Object();
          if(h.containsKey(o.hashCode())){
              arrayList.add(o);
              continue;
          }
          h.put(o.hashCode(),o);
      }
      long currentTimeMillis = System.currentTimeMillis();
      System.err.println("hashcode collision："+arrayList.size());
      System.err.println(" used time "+(currentTimeMillis - now));
  
  }
  </code></pre>
  
  <p>Before I ran the test, I expected I will get the fewest collisions when I set "-XX:hashcode=5", but that's not the case:</p>
  
  <pre><code>| n | algorithm      |collisions|
  |---|----------------|----------|
  | 0 | rondom         |        0 |
  | 1 | addr-XOR-SHIFT |        0 |
  | 2 | invarible-one  |  1999999 |
  | 3 | autoincrease   |        0 |
  | 4 | addr           |    23511 |
  | 5 | xor-shift      |      962 |
  </code></pre>
  
  <p>Then I set times to 20000000 and addr-XOR-SHIFT is still 0. My question is: is it better then xor-shift? Why does jdk-8 make "-XX:hashcode=5" the default?</p>