Sunday, November 20, 2011

Speeding up GWT compilation

When you develop a GWT application you can easily observe that it is compiled for a pretty long time.

This post has a Russian version.

For example, build that small application by executing command "mvn clean install":

[INFO] Scanning for projects...
...                                                                       
[INFO] ------------------------------------------------------------------------
[INFO] Building hellogwt 1.0
[INFO] ------------------------------------------------------------------------
...
[INFO] --- gwt-maven-plugin:2.4.0:compile (default) @ hellogwt ---
[INFO] auto discovered modules [com.hellogwt.HelloGWT]
[INFO] Compiling module com.hellogwt.HelloGWT
[INFO]    Compiling 6 permutations
[INFO]       Compiling permutation 0...
[INFO]       Process output
[INFO]          Compiling
[INFO]             Compiling permutation 1...
[INFO]       Compiling permutation 2...
[INFO]       Compiling permutation 3...
[INFO]          Compiling
[INFO]             Compiling permutation 4...
[INFO]       Compiling permutation 5...
[INFO]    Compile of permutations succeeded
[INFO] Linking into D:\Work\hellogwt\target\hellogwt-1.0\hellogwt
[INFO]    Link succeeded
[INFO]    Compilation succeeded -- 30.684s
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 36.295s
[INFO] Finished at: Sat Nov 19 01:40:03 EET 2011
[INFO] Final Memory: 17M/309M
[INFO] ------------------------------------------------------------------------

30.684 seconds. And that application only contains several services and one main class HelloGWT.

Let's try to reduce compilation time:

1) It is better to use implementations (ArrayList) instead of interfaces (List) as arguments and return types of GWT RPC services. Otherwise we will have unnecessary compilation to support all interface implementations. HelloGWT class contains refreshGreetingTable() method that uses List interface:
...
private void refreshGreetingsTable() {
        greetingService.getGreetings(new AsyncCallback<List<Greeting>>() {
            public void onFailure(Throwable throwable) {
                Window.alert("ERROR: Cannot load greetings!");
            }

            public void onSuccess(List<Greeting> greetings) {
                fillGreetingsTable(greetings);
            }
        });
    }
...

Change List to its implementation - ArrayList:
...
private void refreshGreetingsTable() {
        greetingService.getGreetings(new AsyncCallback<ArrayList<Greeting>>() {
            public void onFailure(Throwable throwable) {
                Window.alert("ERROR: Cannot load greetings!");
            }

            public void onSuccess(ArrayList<Greeting> greetings) {
                fillGreetingsTable(greetings);
            }
        });
    }
...

Of course don't forget to make appropriate changes in interfaces and classes that contain refreshGreetingTable() because of changed method signature.

2) It is possible to disable code optimization during compilation using "draftCompile" parameter.

Execute command "mvn clean install -Dgwt.draftCompile=true":
[INFO] Scanning for projects...
...
[INFO] ------------------------------------------------------------------------
[INFO] Building hellogwt 1.0
[INFO] ------------------------------------------------------------------------
...
[INFO] --- gwt-maven-plugin:2.4.0:compile (default) @ hellogwt ---
[INFO] auto discovered modules [com.hellogwt.HelloGWT]
[INFO] Compiling module com.hellogwt.HelloGWT
[INFO]    Compiling 6 permutations
[INFO]       Compiling permutation 0...
[INFO]       Process output
[INFO]          Compiling
[INFO]             Compiling permutation 1...
[INFO]       Compiling permutation 2...
[INFO]          Compiling
[INFO]             Compiling permutation 3...
[INFO]       Compiling permutation 4...
[INFO]       Compiling permutation 5...
[INFO]    Compile of permutations succeeded
[INFO] Linking into D:\Work\hellogwt\target\hellogwt-1.0\hellogwt
[INFO]    Link succeeded
[INFO]    Compilation succeeded -- 25.326s
... 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 31.143s
[INFO] Finished at: Sat Nov 19 01:48:38 EET 2011
[INFO] Final Memory: 16M/309M
[INFO] ------------------------------------------------------------------------

25.326 seconds. It's much better.

3) GWT application is compiled for all supported browser types by default. We can compile it for only one. It can be set with "user.agent" property in HelloGWT.gwt.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.6.2//EN"
        "http://google-web-toolkit.googlecode.com/svn/tags/1.6.2/distro-source/core/src/gwt-module.dtd">
<module rename-to='hellogwt'>
    <!-- Inherit the core Web Toolkit stuff.                        -->
    <inherits name='com.google.gwt.user.User'/>

    <!-- Inherit the default GWT style sheet.  You can change       -->
    <!-- the theme of your GWT application by uncommenting          -->
    <!-- any one of the following lines.                            -->
    <inherits name='com.google.gwt.user.theme.standard.Standard'/>
    <!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
    <!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/>     -->

    <!-- Other module inherits                                      -->

    <!-- Specify the app entry point class.                         -->
    <entry-point class='com.hellogwt.client.HelloGWT'/>

    <set-property name="user.agent" value="safari"/>

    <source path='client'/>
    <source path='shared'/>
</module>

All possible "user.agent" property values are listed there.

Execute command "mvn clean install -Dgwt.draftCompile=true":
[INFO] Scanning for projects...
...                                                                     
[INFO] ------------------------------------------------------------------------
[INFO] Building hellogwt 1.0
[INFO] ------------------------------------------------------------------------
...
[INFO] --- gwt-maven-plugin:2.4.0:compile (default) @ hellogwt ---
[INFO] auto discovered modules [com.hellogwt.HelloGWT]
[INFO] Compiling module com.hellogwt.HelloGWT
[INFO]    Compiling 1 permutation
[INFO]       Compiling permutation 0...
[INFO]    Compile of permutations succeeded
[INFO] Linking into D:\Work\hellogwt\target\hellogwt-1.0\hellogwt
[INFO]    Link succeeded
[INFO]    Compilation succeeded -- 19.606s
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 25.433s
[INFO] Finished at: Sat Nov 19 01:42:30 EET 2011
[INFO] Final Memory: 16M/330M
[INFO] ------------------------------------------------------------------------

19.606 seconds. It is 36% less than default compilation time - 30.684 seconds. Great difference!

Also, you can discover more about effective GWT application development there and there.

2 comments:

  1. Thanks a lot Alex! you saved my time. Actually my projects was 18 permutation totally. This blog saved my precious time.

    Regards,
    vidhya

    ReplyDelete