The GWT.create 2014 conference – Day 1 : Compiler Deep Dive

Compiler Deep Dive by Ray Cromwell (Google)

Overall architecture
GWT SDK is composed by 3 major pieces. The first one is the compiler tool (compiler and dev mode). The second one is the library (the user, core libs, Java util etc). the third one is runtime part (bootstrap, nocache.js etc.). This talk is about the first part, GWT compiler. The GWT compiler has 3 very distinctive faces: Precompiler, Compile permutation and Link. Precompiler get all java sources and transform them into intermediate representation. Compile permutation runs optimizations. The link gathers the permutation and binds them up so that the application can be executed.

Precompile
Precompile is a pretty complicated part. It reads the arguments; load the gwt xml file, which tells the compiler where the sources are located. Then basically it invokes the eclipse JDT compiler. The interest thing in precompile is that the whole compile procedure runs in a loop. Every time the procedure ends, it will check if something has changed. If yes, that means new Java sources are created, the procedure need to be run one more time, until no more source code is produced.

Compile permutation

GWT Java AST
Java AST, the abstract syntax tree, is something very similar with Java reflection. However, GWT goes deeper. It looks into the methods and the statements. For each piece of code, there will be a piece of statements that represents the Java code. Ray gave an example how the Java class is translated :

class Foo {
int x;
void bar() {
x = 2 + 5;
}
}

This is what the AST like before the optimization. Every operation in Java is translated to a JBinaryExpression.

GWT Java AST

Optimization
There are two kinds of optimizations, the local optimization looks just inside a statement or a method at a time, the global optimization looks at the relation between classes or methods.

Local optimization
When optimization runs, the JVisitor and JModVisitor are used to collect information. In fact every JBinaryExpression has a callback. It allows the compile to decide if there is something to optimize.

For example : x = 2 + 5 operations is optimized to x = 7.

Every JNode has the knowledge of the original code where it came from. So when one JNode is replaced, these informations will be copied to the new node. There are two special nodes in the AST, JNullType and JNotNullType. Some class could be wrapped by JNullType to optimize the access of a reference.

Global optimization

  • Pruner is a very important optimization. It will pass through the whole program from the entry point onModuleLoad and mark all used methods. The others not used methods will be removed.
  • Finalizer makes All non-overriden methods and classes final and all non-mutated fields made final. It will help the later optimizations.
  • MakeCallStatic makes some methods static for the later optimizations.
  • TypeTightener is the most expensive one but the most important one as well. It examines all the generate class to see if a tighter class could be used so that more optimizations can be done.
  • MethodCallTigntener bases on TypeTightener, which bound a method to the tighter class’s method. For example List x = new ArrayList() is rewritten to ArrayList x. Then the x.add() method will be bounded to ArrayList.add().
  • MethodInliner inlines small methods just like the example before.

Ray showed us a complete optimization example that makes the whole procedure very clear. Compiler keeps running these optimizations in a loop until a fix point that no change is made.

Normalization
Normalizer runs after the optimization step to make JavaScript can execute Java semantic. For example with CatchBlockNormalizer, the multi catch block in Java will be replaced by a check on the class of  the execption in one single catch block. As JavaScript doesn’t have Long, all the long operations will be replaced by methods in LongLib. Etc.

JavaScript AST
The JavaScript AST in GWT is pretty much identical with the Java AST. The reason why GWT has two separate AST is that, first of all JavaScript doesn’t have type, so some additional information must be added; secondly is JavaScript can’t be all represented is Java, the separate AST allows to do some optimization that is not possible in Java. Such as Boolean true can be replaced by not zero in JavaScript and that can reduce the code size.JavaScript has the different conception of scoping than Java. There are actually three ways to introduce a scope: the global scope, function scope, and catch block scope. So when the JS AST is build, in parallel a scope will be build. Ray showed also an example how a Java class is laid out in JavaScript. One interest thing is that static initialization code in Java will be represented in JavaScript like:

function clinit() {
clinit = nullMethod;
// class initialization code
}

We can see that the first thing in this method is turn itself to a NullMethod. That means, this method will only executed once because the next time when JavaScript try to run this method, it will ignore it because it’s a Null Method.

CodeSplitting
People are familiar with code splitting. The result of a little investigation of the public showed that more than a half use CodeSplitting. The problem of the CodeSplitting is the share libraries as ray talked in the keynote.

Overlay types
Different from Java class that generates lots of codes in JavaScript, Overlay type doesn’t add extra codes. Overlay doesn’t have any prototype or any constructor. The methods in an overlay type are generated to a static method, that the reason why overlay type must be final.

Conclusion
Compiler is for most of us a mystery part of GWT SDK. This session from Ray Cromwell leaded us to go through the compiler to have a general idea about how it works. This introduction is a good chance for all that doesn’t have the courage to look inside the compiler himself. What I regretted a lot is that he didn’t give any word about the new generation of the compiler like the incremental and the fast compile. As we always expect to have more information about the future in such a conference than to learn about something existed that compilation time make lots of developers suffer.

 

Share
Zhe SUN
Zhe SUN

1311

Leave a Reply

Your email address will not be published. Required fields are marked *