javaoptimizationbytecode

Is there difference in compilers - java


Is there any differences in code optimization done by same versions of:

If there is what code would demonstrate different optimizations? Or are they using same compiler? If there is no known optimization differences then where could I find resources on how to test compilers for different optimizations?


Solution

  • Is there any differences in code optimization done by same versions of: Oracle Java compiler Apache Java compiler IBM Java compiler OpenJDK Java compiler.

    While compiler can be very different, the javac does almost no optimisations. The main optimisation is constant inlining and this is specified in the JLS and thus standard (except for any bugs)

    If there is what code would demonstrate different optimizations?

    You can do this.

    final String w = "world";
    String a = "hello " + w;
    String b = "hello world";
    String c = w;
    String d = "hello " + c;
    System.out.prinlnt(a == b); // these are the same String
    System.out.prinlnt(c == b); // these are NOT the same String
    

    In the first case, the constant was inlined and the String concatenated at compile time. In the second case the concatenation was performed at runtime and a new String created.

    Or are they using same compiler?

    No, but 99% of optimisations are performed at runtime by the JIT so these are the same for a given version of JVM.

    If there is no known optimization differences then where could I find resources on how to test compilers for different optimizations?

    I would be surprised if there is one as this doesn't sound very useful. The problem is that the JIT optimises prebuilt templates of byte code and if you attempt to optimise the byte code you can end up confusing the JIT and having slower code, i.e. there is no way to evaluate an optimisation without considering the JVM it will be run on.