JDK-6182955 : Enum.hashCode() could be seven times faster
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 5.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-10-21
  • Updated: 2010-05-10
  • Resolved: 2006-02-04
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b71Fixed
Related Reports
Relates :  
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
Enum.hashCode() call System.identityHashCode() that
is seven times slower than Object.hashCode().

Note that I don't know why and perhaps, System.identityHashCode()
may be optimized.

JUSTIFICATION :
it's improve performance

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
public final int hashCode() {
    return super.hashCode();
}
ACTUAL -
public final int hashCode() {
    return System.identityHashCode(this);
}

---------- BEGIN SOURCE ----------
It's just a micro benchmark than test  super.hashCode()
and System.identityHashCode(this)

/**
 * @author remi
 *
 */
public class MyEnumTest {
  static class Zuper {
    public final int hashCode() {
      return super.hashCode();
    }
  }
  static class Identity {
    public final int hashCode() {
      return System.identityHashCode(this);
    }
  }
  static long test(Object o) {
    long time=System.nanoTime();
    for(int i=0;i<1000000;i++)
      o.hashCode();
    return System.nanoTime()-time;
  }
  public static void main(String[] args) {
    System.out.println("super "+test(new Zuper()));
    System.out.println("identity "+test(new Identity()));
  }
}

---------- END SOURCE ----------
###@###.### 10/21/04 23:26 GMT

Comments
EVALUATION I can verify the behavior of the micro-benchmark. I will investigate why Enum.hashCode() is implemented as it currently is. ###@###.### 10/22/04 00:04 GMT The method System.identityHashCode was assumed to be faster. Apparently, this is wrong. ###@###.### 10/22/04 02:09 GMT
22-10-2004