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

[lworld] Weird interaction between OSR and value class instances

XMLWordPrintable

      The reproducer below and its output indirectly show that OSR and value class instances interact unexpectedly.
      The 1 billion iterations loop in method `add()` takes too long during the first couple of iterations of the loop in `main()`.

      ```
      public value class MyNumber {

          private long d0;

          private MyNumber(long d0) {
              this.d0 = d0;
          }

          public MyNumber add(long v) {
              return new MyNumber(d0 + v);
          }

          @Override
          public String toString() {
              return "[" + d0 + "]";
          }

          public static void main(String[] args) {
              for (int i = 0; i < 10; ++i) {
                  add();
              }
          }

          private static void add() {
              MyNumber dec = new MyNumber(123);
              long begin = System.nanoTime();
              for (int i = 0; i < 1_000_000_000; ++i) {
                  dec = dec.add(i);
              }
              System.out.println((System.nanoTime() - begin) / 1_000_000);
              System.out.println(dec);
          }

      }
      ```

      The timings below (Apple MacBook M1 Pro/3.2 GHz/32 GiB) show that later executions of the loop in add() are on par with pure `long` arithmetic, which is the expected outcome.

      ```
      1347
      [499999999500000123]
      1358
      [499999999500000123]
      332
      [499999999500000123]
      332
      [499999999500000123]
      332
      [499999999500000123]
      333
      [499999999500000123]
      330
      [499999999500000123]
      331
      [499999999500000123]
      331
      [499999999500000123]
      332
      [499999999500000123]
      ```

            Unassigned Unassigned
            rgiulietti Raffaello Giulietti
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: