user2758938
user2758938

Reputation: 63

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?

Upvotes: 5

Views: 256

Answers (3)

Peter Lawrey
Peter Lawrey

Reputation: 533850

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.

Upvotes: 3

Mike Strobel
Mike Strobel

Reputation: 25623

The only compilers that I have spent a great deal of time with are javac (which, as others have pointed out, does very little in terms of eager optimization) and the Eclipse compiler.

While writing a Java decompiler, I have observed a few (often frustrating) differences in how Eclipse compiles code, but not many. Some of them could be considered optimizations. Among them:

  1. The Eclipse compiler appears to perform at least some duplicate code analysis. If two (or more?) blocks of code both branch to separate but equivalent blocks of code, the equivalent target blocks may be flattened into a single block with multiple entry jumps. I have never seen javac perform this type of optimization; the equivalent blocks would always be emitted. All of the examples I can recall happened to occur in switch statements. This optimization reduces the method size (and therefore class file size), which may improve load and verification time. It may even result in improved performance in interpreted mode (particularly if the interpreter in question performs inlining), but I imagine such an improvement would be slight. I doubt it would make a difference once the method has been JIT compiled. It also makes decompilation more difficult (grrr).

  2. Basic blocks are often emitted in a completely different order from javac. This may simply be a side effect of the compiler's internal design, or it may be that the compiler is trying to optimize the code layout to reduce the number of jumps. This is the sort of optimization I would normally leave to the JIT, and that philosophy seems to work fine for javac.

Upvotes: 0

Rahul
Rahul

Reputation: 45080

No, they do not use the same compiler. I can't comment much about the optimizations and stuffs, but here's an example how the compilers are different in their working.

public class Test {
    public static void main(String[] args) {
        int x = 1L;  // <- this cannot compile
    }
}

If you use the standard java compiler, it'll throw an compilation error and the class file won't be created.

But if you use the eclipse compiler for java ECJ, it'll not only throw the same compilation error, but will also create a class file(YES, a class file for an uncompilable code, which makes ECJ, I wouldn't say wrong, but a bit tricky), which looks something like this.

public static void main(String[] paramArrayOfString)
{
    throw new Error("Unresolved compilation problem: \n\tType mismatch: cannot convert from long to int.\n");
}

Having said that, this is just between 2 compilers. Other compilers may have their own way of working.

P.S: I took this example from here.

Upvotes: 4

Related Questions