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

implicity optimise variable declaration (javac or hotspot)

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 5.0
    • hotspot
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      when a declaration of a variable is made there are times when the compiler or runtime knows a more specific types of the variable then the declaration


      JUSTIFICATION :

      If the code uses the more specific declaration then hotspot can optimes the code in some cases

      as can be seen from the results below

      testLL1: 190 ms testLL2: 30 ms testLL3: 200 ms testAL1: 191 ms testAL2: 30 ms testAL3: 190 ms
      testLL1: 200 ms testLL2: 30 ms testLL3: 211 ms testAL1: 190 ms testAL2: 40 ms testAL3: 190 ms
      testLL1: 191 ms testLL2: 30 ms testLL3: 190 ms testAL1: 200 ms testAL2: 30 ms testAL3: 191 ms
      testLL1: 220 ms testLL2: 40 ms testLL3: 190 ms testAL1: 190 ms testAL2: 30 ms testAL3: 191 ms
      testLL1: 190 ms testLL2: 30 ms testLL3: 230 ms testAL1: 201 ms testAL2: 30 ms testAL3: 210 ms
      testLL1: 200 ms testLL2: 30 ms testLL3: 201 ms testAL1: 190 ms testAL2: 40 ms testAL3: 190 ms
      testLL1: 251 ms testLL2: 60 ms testLL3: 260 ms testAL1: 190 ms testAL2: 30 ms testAL3: 211 ms
      testLL1: 210 ms testLL2: 40 ms testLL3: 250 ms testAL1: 191 ms testAL2: 30 ms testAL3: 200 ms
      testLL1: 230 ms testLL2: 40 ms testLL3: 191 ms testAL1: 180 ms testAL2: 40 ms testAL3: 180 ms
      testLL1: 191 ms testLL2: 30 ms testLL3: 190 ms testAL1: 190 ms testAL2: 30 ms testAL3: 190 ms

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      in the example javac or hotspot could 'rewrite' the line
      final List x = new LinkedList()
      to
      final LinkedList x = new LinkedList();

      to enable the direct jumps to the methods as opposed to the virtual method calls

      ---------- BEGIN SOURCE ----------

      import java.util.ArrayList;
      import java.util.LinkedList;
      import java.util.List;
      import java.util.logging.Logger;

      /**
       *
       * @author <a href="mailto:###@###.###">Mike Skells</a> - <a href="http://www.validsoft.com">ValidSoft</a>
       */
      public class test10 {
          
          private final static Class loggingClass = test10.class;
          private final static boolean debug = loggingClass.desiredAssertionStatus();
          private final static Logger logger = Logger.getLogger(loggingClass.getName());
          public static void main(String ... args) {
              new test10().run();
          }
          private final static int count = 10000000;
          
          void run() {
              for (int i = 0; i < 10; i++) {
                  testLL1();
                  testLL2();
                  testLL3();
                  testAL1();
                  testAL2();
                  testAL3();
              }
              long time = System.currentTimeMillis();
              for (int i = 0; i < 10; i++) {
                  testLL1();
                  time = timer(time,"testLL1");
                  testLL2();
                  time = timer(time,"testLL2");
                  testLL3();
                  time = timer(time,"testLL3");
                  testAL1();
                  time = timer(time,"testAL1");
                  testAL2();
                  time = timer(time,"testAL2");
                  testAL3();
                  time = timer(time,"testAL3");
                  System.out.println();
              }
          }
          
          long timer(long start, String msg) {
              long end = System.currentTimeMillis();
              System.out.print(msg+": "+(end-start)+" ms ");
              return end;
          }
          
          void testLL1() {
              List x = new LinkedList();
              for (int i = 0; i < count; i++) {
                  int ignore = x.size();
              }
          }
          void testLL2() {
              LinkedList x = new LinkedList();
              for (int i = 0; i < count; i++) {
                  int ignore = x.size();
              }
          }
          void testLL3() {
              final List x = new LinkedList();
              for (int i = 0; i < count; i++) {
                  int ignore = x.size();
              }
          }
          void testAL1() {
              List x = new ArrayList();
              for (int i = 0; i < count; i++) {
                  int ignore = x.size();
              }
          }
          void testAL2() {
              ArrayList x = new ArrayList();
              for (int i = 0; i < count; i++) {
                  int ignore = x.size();
              }
          }
          void testAL3() {
              final List x = new ArrayList();
              for (int i = 0; i < count; i++) {
                  int ignore = x.size();
              }
          }
        }

      ---------- END SOURCE ----------

            Unassigned Unassigned
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: