Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8238382

[lworld] System::identityHashCode must be computable for inline types

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • repo-valhalla
    • repo-valhalla
    • hotspot
    • generic
    • generic

      From http://cr.openjdk.java.net/~briangoetz/valhalla/sov/02-object-model.html :

      System::identityHashCode. The main use of identityHashCode is in the implementation of data structures such as IdentityHashMap, as a filter in front of ==. We can totalize identityHashCode in the same way we totalize equality – deriving a hash from the hash of all the fields.

      As of LW2, attempts to compute identityHashCode on value instances were disallowed by javac compiler (where it is statically discernible that the operand is an instance of value type)

      JDK-8237071 is fixed so that this operation is no longer disallowed at compile time.

      However ATM, it appears that System.identityHashCode computes a result that seems to ignore the fields of the value instance as demonstrated by this test case:

      (I get the same code for all X instances. All Y instances appear to compute the same result that differs from X instances id hash code)

      final inline class Y {
          int y1;
          int y2;

          Y(int y1, int y2) {
              this.y1 = y1;
              this.y2 = y2;
          }

          Y() {
              this(1, 2);
          }
      }

      public final inline class X {

          int x;

          X(int x) {
              this.x = x;
          }

          X() {
              this(42);
          }
         
          int id() {
              return System.identityHashCode(this);
          }

          public static void main(String [] args) {

              X[] xa = { new X(), new X(), new X(10), new X(10), X.default, X.default };

              for (X x : xa) {
                  System.out.println("X instance = " + x);
                  System.out.println("X hashCode = " + x.hashCode());
                  System.out.println("X IDhashCode = " + System.identityHashCode(x));
              }
              System.out.println("X IDhashCode = " + System.identityHashCode(new Y()));
              System.out.println("X IDhashCode = " + System.identityHashCode(new Y(20, 30)));
       
          }
      }

            dsimms David Simms
            sadayapalam Srikanth Adayapalam (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: