Friday, March 7, 2008

GWT:The Road To 1.5, Part 1

The next version of Google Web Toolkit is almost upon us, GWT 1.5, but don't take the minor version bump as an indication of how much it's improved, GWT 1.5 has so many awesome improvements, it would be more proper to call it GWT 2.0. That's one reason why it's been almost a year since the last release.

To celebrate the release, I will be writing a series of brief articles on each of the many improvements of 1.5, hopefully with sample code demonstrations.

Zero Overhead Javascript Interop
One of the cool things about GWT has always been the JSNI concept, or 'Javascript Native Interface', that neatly parallels JNI in ordinary Java, except the 'native' implementation is Javascript.

One of the most common uses of JSNI is to produce wrappers for third party Javascript libraries like Scriptaculous, Dojo, ExtJS, etc. Unfortunately, these wrappers are somewhat expensive prior to 1.5

Common Wrap Patterns
There are two common ways people wrap third party Javascript libraries: Encapsulation of JavaScriptObject ('JSO'), or extension of JavaScriptObject. Prior to 1.5, subclassing of JSO worked, but was not officially supported.

Regardless of which technique is chosen, the wrappers work by providing access to Javascript object properties via JSNI getters and setters, and methods via JSNI methods that delegate to Javascript methods.

Unfortunately, delegation getter methods or other instance methods, were not optimized away or inlined by the GWT compiler. Let's look at an example.


public class TestWrapperEncapsulation {
private final JavaScriptObject jso;
public TestWrapperEncapsulation(JavaScriptObject jso) {
this.jso = jso;
}
public native String getFoo() /*-{
return this.@TestWrapperEncapsulation::jso.foo;
}-*/;
public static native JavaScriptObject makeObject() /*-{
return {foo: 'Hello World'}
}-*/;
public static TestWrapperEncapsulation create() {
return new TestWrapperEncapsulation(makeObject());
}
}


Given the above wrapper class, if you were to write:


Window.alert(TestWrapperEncapsulation.create().getFoo());


You would not end up with the desired optimal JavaScriptCode, e.g.

$wnd.alert(TestWrapperEncapsulation.create().jso.foo)


rather, this


$alert(this$static, $getFoo(create()));

function $alert(this$static, foo){
$wnd.alert(foo);
}

function $TestWrapperEncapsulation(this$static, jso){
this$static.jso = jso;
return this$static;
}

function $getFoo(this$static){
return this$static.jso.foo;
}

function create(){
return $TestWrapperEncapsulation(new TestWrapperEncapsulation(), makeObject());
}

function makeObject(){
return {'foo':'Hello World'};
}


Now let's take a look at GWT1.5's output:

$wnd.alert($TestWrapperEncapsulation(new TestWrapperEncapsulation(), {foo:'Hello World'}).jso.foo);

function $TestWrapperEncapsulation(this$static, jso){
this$static.jso = jso;
return this$static;
}


That's a massive reduction, but still not good enough. There is in fact, no reason to have a wrapper encapsulate the JSO. Instead, with GWT 1.5, we can subclass the JSO and pretend that our Java methods exist on the underlying JSO.


alert(TestWrapper.makeWrapper().getFoo());

public class TestWrapper extends JavaScriptObject {
protected TestWrapper() { }
public final native String getFoo() /*-{
return this.foo;
}-*/;

public static native TestWrapper makeWrapper() /*-{
return { foo: "Hello World" }
}-*/;
}

and what does the 1.5 compiler produce?

$wnd.alert({foo:'Hello World'}.foo);


Perfect! Absolutely zero overhead.

2 comments:

Ray Cromwell said...

Opps, the SyntaxHightlighter CSS doesn't appear to load right on FF, I'll fix later, and tweak the color scheme to better match the blog skin.

-Ray

Anonymous said...

(法新社倫敦四日電) 英國情色大亨芮孟a片的公司昨天說,芮孟av日前成人影片av女優世,享壽八十二歲;這位身價上億的房地產日本av開發商,部落格a片經在倫成人av推出第一場脫衣舞表演。

成人網站
芮孟的財產估計av女優達六億五千萬英鎊成人影片(台a片av女優情色近四成人百億),由於他名下事業大多分布在倫敦夜生色情a片色情區蘇活區sex,因此擁有「蘇成人網站情色之王」的稱號。
部落格

他的公司「保羅芮成人影片孟集團」旗下發a片行多種情色雜誌,包括「Raavdvdzzle」、情色電影「男性世界」以及「Mayfair」。色情影片


芮孟本名傑福瑞.安東尼.奎恩,父親色情為搬運承包商。芮孟av成人光碟五歲離開學校,矢言要在表演事業留名,起先表演讀av心術,後來成為巡迴歌舞雜耍表演的製作人。


許多評a片下載論家認為,他把情色情色電影表演帶進主流社會成人電影,一九五九年主成人網站持破情色視訊天荒的脫衣舞表演,後來更靠著在蘇活部落格區與倫敦色情西區開發房地產賺得大筆財富。

a片下載
有人形成人電影容芮孟是英國的海夫納,地位AV片等同美國的「花花公子」創辦人海夫納。