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.

3 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

Unknown said...

情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣,情趣,情趣,情趣,情趣,情趣,情趣用品,情趣用品,情趣,情趣,A片,A片,A片,A片,A片,A片,情趣用品,A片,情趣用品,A片,情趣用品,a片,情趣用品,視訊聊天室,聊天室,視訊,ut聊天室,聊天室,視訊聊天室,成人電影,

A片,A片,AV女優,色情,成人,做愛,情色,AIO,視訊聊天室,SEX,聊天室,自拍,AV,情色,成人,情色,aio,sex,成人,情色,色情,情色電影,色情網站,av女優,av,自拍,成人,視訊聊天室,視訊交友網,AV女優,成人,聊天室,ut聊天室,av女優

免費A片,美女視訊,情色交友,免費AV,色情網站,辣妹視訊,美女交友,色情影片,成人影片,成人網站,H漫,18成人,成人圖片,成人漫畫,情色網,日本A片,免費A片下載,性愛

色情A片,A片下載,色情遊戲,色情影片,色情聊天室,情色電影,免費視訊,免費視訊聊天,免費視訊聊天室,一葉情貼圖片區,情色視訊,免費成人影片,視訊交友,視訊聊天,言情小說,愛情小說,AV片,A漫,av dvd,情色論壇,視訊美女,AV成人網,情色文學,成人交友,成人電影,成人貼圖,成人小說,成人文章,成人圖片區,成人遊戲,愛情公寓,情色貼圖,成人論壇,色情


免費A片,日本A片,A片下載,線上A片,成人電影,嘟嘟成人網,成人貼圖,成人交友,成人圖片,18成人,成人小說,成人圖片區,微風成人區,成人文章,成人影城,情色貼圖,色情聊天室,情色視訊,情色文學,色情小說,情色小說,臺灣情色網,色情遊戲,嘟嘟情人色網,麗的色遊戲,情色論壇,一葉情貼圖片區,做愛,性愛,美女視訊,辣妹視訊,免費視訊聊天,美女交友,做愛影片

Anonymous said...

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

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

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


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


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

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