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

C2 optimization causing longer delay in loops

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      generic

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_162-ea"
      Java(TM) SE Runtime Environment (build 1.8.0_162-ea-b01)
      Java HotSpot(TM) 64-Bit Server VM (build 25.162-b01, mixed mode)

      FULL OS VERSION :
      Microsoft Windows [Version 10.0.16299.19]

      A DESCRIPTION OF THE PROBLEM :
      The attached code calls a function 1000000 times and does this in a loop of 5. The first time through the outer loop, the inner loop executes in < 60ms. But all subsequent runs (2-5), take almost 160ms. This is confusing. I thought once the code is JIT'd, it will run fast but in this case, it actually is slower. Not sure what the issue is. But it will good for the runtime team to check out if there is a weird pattern that causes this odd behavior.


      class CsTest {
        private int caught = 0;
        private boolean x;
        public final boolean f() { x = false; return !g() || x; }
        public final boolean g() { return h(); }
        public final boolean h() { if ((caught++ & 1) == 1) {x = true;return true; } return false; }

        public static void main(String[] args) {
          CsTest t = new CsTest();
          for (int j = 0; j < 5; j++) {
            long start = System.currentTimeMillis();
            for (int i = 0; i < 100000000; i++) t.f();
            System.err.println("Time taken: " + (System.currentTimeMillis() - start) + "ms.");
          }
        }
      }

      THE PROBLEM WAS REPRODUCIBLE WITH -Xint FLAG: No

      THE PROBLEM WAS REPRODUCIBLE WITH -server FLAG: Did not try

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Just compile and run the attached class!

      class CsTest {
        private int caught = 0;
        private boolean x;
        public final boolean f() { x = false; return !g() || x; }
        public final boolean g() { return h(); }
        public final boolean h() { if ((caught++ & 1) == 1) {x = true;return true; } return false; }

        public static void main(String[] args) {
          CsTest t = new CsTest();
          for (int j = 0; j < 5; j++) {
            long start = System.currentTimeMillis();
            for (int i = 0; i < 100000000; i++) t.f();
            System.err.println("Time taken: " + (System.currentTimeMillis() - start) + "ms.");
          }
        }
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      Expect the numbers printed (time taken) in lines 2-5 to be <= the one on line one.

      Actual:

      Time taken: 62ms.
      Time taken: 160ms.
      Time taken: 156ms.
      Time taken: 155ms.
      Time taken: 162ms.
      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      Just compile and run the attached class!

      class CsTest {
        private int caught = 0;
        private boolean x;
        public final boolean f() { x = false; return !g() || x; }
        public final boolean g() { return h(); }
        public final boolean h() { if ((caught++ & 1) == 1) {x = true;return true; } return false; }

        public static void main(String[] args) {
          CsTest t = new CsTest();
          for (int j = 0; j < 5; j++) {
            long start = System.currentTimeMillis();
            for (int i = 0; i < 100000000; i++) t.f();
            System.err.println("Time taken: " + (System.currentTimeMillis() - start) + "ms.");
          }
        }
      }
      ---------- END SOURCE ----------

        Attachments

          Activity

            People

            • Assignee:
              thartmann Tobias Hartmann
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: