JDK-6365149 : implicity optimise variable declaration (javac or hotspot)
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-12-19
  • Updated: 2010-08-19
  • Resolved: 2008-01-10
Related Reports
Duplicate :  
Description
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 ----------

Comments
EVALUATION The switch to SSA in the front end along with earlier phi simplification gives the desired effect. in 1.6 the performance of each of these variants is exactly the same as you would expect.
10-01-2008

EVALUATION While the client compiler does have the performance characteristics indicated by the submitter, the server compiler correctly detects the loops in this microbenchmark as being useless work and completely optimizes them away: testLL1: 968 ms testLL2: 1 ms testLL3: 0 ms testAL1: 0 ms testAL2: 1 ms testAL3: 0 ms testLL1: 1029 ms testLL2: 0 ms testLL3: 0 ms testAL1: 0 ms testAL2: 0 ms testAL3: 0 ms testLL1: 2 ms testLL2: 0 ms testLL3: 0 ms testAL1: 0 ms testAL2: 0 ms testAL3: 0 ms testLL1: 1 ms testLL2: 0 ms testLL3: 0 ms testAL1: 0 ms testAL2: 0 ms testAL3: 0 ms testLL1: 0 ms testLL2: 1 ms testLL3: 0 ms testAL1: 0 ms testAL2: 0 ms testAL3: 0 ms testLL1: 0 ms testLL2: 0 ms testLL3: 1 ms testAL1: 0 ms testAL2: 0 ms testAL3: 0 ms testLL1: 0 ms testLL2: 0 ms testLL3: 1 ms testAL1: 0 ms testAL2: 0 ms testAL3: 0 ms testLL1: 0 ms testLL2: 0 ms testLL3: 0 ms testAL1: 1 ms testAL2: 0 ms testAL3: 0 ms testLL1: 0 ms testLL2: 0 ms testLL3: 0 ms testAL1: 1 ms testAL2: 0 ms testAL3: 0 ms testLL1: 0 ms testLL2: 0 ms testLL3: 0 ms testAL1: 0 ms testAL2: 1 ms testAL3: 0 ms While the generated code quality could be improved by using e.g. dynamic type profiling information and better type flow analysis in the client compiler's front end, the best strategy under the current circumstances is to couple the client and server compilers together into a tiered system, which is intended for a future release.
03-01-2006

EVALUATION The Java compiler, javac, is not an optimizing compiler. Reassigning to HotSpot.
22-12-2005