<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2515037436118935802</id><updated>2025-06-15T23:43:16.673-07:00</updated><category term="gwt"/><category term="google web toolkit"/><category term="gwt demystified"/><category term="generators"/><category term="timepedia"/><category term="chronoscope"/><category term="deferred binding"/><category term="google webtoolkit"/><category term="accordion"/><category term="bridge methods"/><category term="chronoscope gwt visualization iphone gesture touch"/><category term="clippedimage"/><category term="data mining"/><category term="everett"/><category term="fontbook"/><category term="google developer day"/><category term="gwt exporter"/><category term="renderer"/><category term="rotated text"/><category term="training"/><title type='text'>Timefire</title><subtitle type='html'>&quot;Time is the school in which we learn,&#xa;Time is the fire in which we burn.&quot;</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Timepedia</name><uri>http://www.blogger.com/profile/01408958797259838599</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>77</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-9138200342195645772</id><published>2010-04-01T12:10:00.000-07:00</published><updated>2010-04-01T14:12:12.955-07:00</updated><title type='text'>GwtQuake: Taking the Web to the Next Level</title><content type='html'>Back in November of 2009, Joel Webber and I were at a GWT Summit focused on improving the Web and UI latency. Someone was giving a presentation on WebGL, and I saw Joel sitting in the back of the room looking seriously distracted. When I approached him, he said &quot;Did you see this?&quot; and swung his notebook around to show Jake2 --  Quake2 ported to Java running as a Java WebStart Application.  I had seen this project years ago, but never knew it had gotten this far.&lt;br /&gt;&lt;br /&gt;Right then and there, I knew what he was thinking, I almost didn&#39;t need to ask -- he was suggesting that we port Jake2 to the Web using GWT!&lt;br /&gt;&lt;object width=&quot;640&quot; height=&quot;385&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/fyfu4OwjUEI&amp;hl=en_US&amp;fs=1&amp;&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/fyfu4OwjUEI&amp;hl=en_US&amp;fs=1&amp;&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;640&quot; height=&quot;385&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;The Path to the Web&lt;/h3&gt;&lt;br /&gt;The idea that you could use GWT to compile something like Jake2 to Javascript seems straightforward, since GWT after all, compiles Java to Javascript, however there is still a lot of work massaging the code to compile, as well as mapping I/O to the appropriate Web APIs.&lt;br /&gt;&lt;br /&gt;As you may know, GWT only implements a subset of the Java JRE, and Jake2 relies on many Java classes that GWT just doesn&#39;t have:&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;The LWJGL library for 3D&lt;/li&gt;&lt;br /&gt;&lt;li&gt;and java.nio.Buffer classes&lt;/li&gt;&lt;br /&gt;&lt;li&gt;java.net.* for networking&lt;/li&gt;&lt;br /&gt;&lt;li&gt;AWT for keyboard stuff&lt;/li&gt;&lt;br /&gt;&lt;li&gt;File system APIs for loading data&lt;/li&gt;&lt;br /&gt;&lt;li&gt;OpenAL for audio&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;More importantly, it relies on a synchronous API paradigm for doing I/O, and as we know, Javascript is built around event driven I/O.&lt;br /&gt;&lt;h3&gt;Mapping I/O&lt;/h3&gt;&lt;br /&gt;Joel reorganized Jake2 I/O system around event driven pumping to load files. The first thing we did was get the level and model files loading, and rendering using a 2D &amp;lt;canvas&gt; wireframe renderer (no WebGL). After that, Stefan Haustein contributed new JRE classes to implement Java NIO Buffers around WebGL&#39;s new typed javascript arrays, as well as a GL renderer based around WebGL.&lt;br /&gt;&lt;br /&gt;Joel amazingly got multiplayer up and running on WebSockets and we had great fun play testing deathmatches between Atlanta, Mountain View, and even Sydney.&lt;br /&gt;&lt;br /&gt;I went ahead and converted all of the sound to MP3 and implemented an initial sound renderer just by using the DOM Audio element. Later, I refactored the OpenAL code into a base class and provided an implementation of the OpenAL Inverse Distance Clamped sound model that adjusted volume gain of sounds based on 3D position, still, using nothing more than the audio.volume property.&lt;br /&gt;&lt;br /&gt;Stefan added an implementation of RandomAccessFile based on LocalStorage to save game files, and an implementation of video playback using the &amp;lt;video&gt; tag.&lt;br /&gt;&lt;br /&gt;Most of the work was completed by January by part time contributions, but has been tweaked since then. Stefan, for example, went ahead and ported the entire single player game (no server needed) as static content.  &lt;br /&gt;&lt;br /&gt;A more detailed post-mortem will be presented during one of my Google I/O sessions, be sure to attend!&lt;br /&gt;&lt;h3&gt;Is it slow?&lt;/h3&gt;&lt;br /&gt;I thought it would be, honestly, I never thought we would get this far, but it turns out that playable framerates are achievable. On WebKit/Chrome I can get 20-25fps on a MacBook, and on my Mac Pro desktop, I get about 45fps. Joel reports that a Linux notebook gets up to 60fps.&lt;br /&gt;&lt;h3&gt;What this means for the Web&lt;/h3&gt;&lt;br /&gt;For years, people have assumed the browser was a poor platform for this kind of thing, and that you&#39;d need something like Flash, Silverlight, JavaFX, or native code. While it is true that you should not expect the browser to rival triple-A titles like Far Cry or Call of Duty in the browser, there is no reason why lots of casual games that used to be implemented in Flash, or are now implemented in Objective-C on the iPhone/iPad can&#39;t be done using similar techniques we&#39;ve used.&lt;br /&gt;&lt;br /&gt;Moreover, because it&#39;s the Web, all you need to do to invite someone to your game, to share it, is to send a link. You could Tweet a link directly to your game, which when clicked, would make someone join the game in progress. No installs necessary. While this can be done in Flash, it just feels &quot;natural&quot; when it&#39;s in the browser. &lt;br /&gt;&lt;br /&gt;I hope that this port encourages some people to become even bolder and crazier in the types of Web Apps they&#39;re trying to build, because if Quake2 is possible in Javascript using browser APIs, then even more amazing applications are waiting for you to develop, so get started!&lt;br /&gt;&lt;br /&gt;The source code for our port is at: &lt;a href=&quot;http://code.google.com/p/quake2-gwt-port&quot;&gt;http://code.google.com/p/quake2-gwt-port&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;-Ray</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/9138200342195645772/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/9138200342195645772' title='19 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/9138200342195645772'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/9138200342195645772'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2010/04/gwtquake-taking-web-to-next-level.html' title='GwtQuake: Taking the Web to the Next Level'/><author><name>Ray Cromwell</name><uri>http://www.blogger.com/profile/05070235196940096193</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>19</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-2017433081392011528</id><published>2009-12-08T16:51:00.000-08:00</published><updated>2009-12-09T02:33:37.565-08:00</updated><title type='text'>GWT 2.0: So good it&#39;s ridiculous</title><content type='html'>GWT 2.0 was released today, and even though I&#39;ve been using it for months and contributing to it, I didn&#39;t realize the significance of this release until I saw the Google Campfire event, because I&#39;ve been exposed to each new feature incrementally, rather than all at once. If you take a step back, look at what has been accomplished since 1.7:&lt;br /&gt;&lt;dl&gt;&lt;br /&gt;&lt;dt&gt;Development Mode&lt;br /&gt;&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;Who else but Google could build a plugin that runs on 3 different browsers on 3 different operating systems, deeply integrated with the Javascript VMs on IE, WebKit, and Firefox,  that runs Java in a hybrid mode, where Java code runs in any JVM, and Javascript code is injected and ran in the browser as Javascript over a network connection? Do not underestimate the difficulty of doing this, especially when considering that Javascript can hold references to Java and vice-versa, causing problems for accurate garbage collection. Development Mode is an amazing productivity tool, made possible by some really awesome technology under the hood. Let&#39;s get a w00t! for the engineering that went into this.&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;UI Binder&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;Following the GWT mantra &quot;Why do at runtime what you can do at compile time?&quot; UIBinder is an extensible declarative language for constructing user interfaces, injecting those constructs into your Java code, and optimizing and bundling resources like stylesheets, images, and localized text.  While it may seem like a throwback to use XML/XHTML in an era when HTML5 tagsoup and JSON are emerging as favorites, you have to consider what has been bought in exchange. UiBinder constructs HTML at compile time, which means it minimizes the number of DOM operations needed to get your UI up and running. It treats stylesheets as an intelligence resource, obfuscating and compressing them just as the GWT compiler does for JS, as well as providing local scope for CSS classes so that you can compose stylesheets written by different parties together without worrying about conflicts.&lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;Google Eclipse Plugin&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;Technically, it&#39;s separate from GWT, but I&#39;ll list it here because of its tight integration with GWT. It provides intelligent integration between your Java source, JSNI methods, and UiBinder files. Again, this product alone would be a major event unto itself, but it&#39;s almost under-the-radar in Google&#39;s Campfire event.&lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;Code Splitting&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;The value that whole-program compilation for JS brings to the table is no better demonstrated than it is here, by the ability of the GWT compiler to statically compute transitive callgraphs and dependencies from any callsite you desire, and split off that chunk of code for deferred loading. Ask yourself, if I handed you a hand-written large JS application like Maps, or GMail, or Y! Mail, how long would it take you to fully modularize all of the libraries used, get all of the dependencies exactly right (if you need feature F, load library F, but F needs G and H, so load libraries G and H). You can point to popular JS libraries using complex JS module loaders with dependency management, but keep in mind, that is all done by hand, on very mature libraries. GWT Code Splitting brings this functionality to mere mortals in just a few lines of code.&lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;Constraint-Based Layout&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;Many modern JS libraries have punted on making CSS work for general compose-able widgets, and rely on JS to perform layout calculations for widgets. This works well, but has the downside of laggy performance, as the browser rendering engines are optimized for layout calculations, and often performing JS layout can cause excessive browser layouts. You&#39;ll see this in split panels or scroll panes sometimes as the divider or scrollbar lagging the mouse position.  Joel Webber of the GWT team has finally cracked the code to make CSS based layouts work in a predictable fashion, and as a result, you can now compose arbitrary layouts in GWT with predictability, a productivity enhancement unto itself, even if some flexibility is lost, end-user performance and development speed are gained.&lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;ClientBundle&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;Few people realize that GWT has been doing automated image sprites for a long time. ImageBundle, introduced in GWT 1.4, allowed GWT users to simply refer to images on disk, and the compiler would pack all of those images into a single file, transforming references to the images automatically into sprites. Then with 1.5/1.6, FooBundle, or ImmutableResourceBundle was introduced to the GWT Incubator. FooBundle/IRB took spriting to the next level by allowing all of your resources to be bundled into one or more files, or even inlined in your compiled JS as a gigantic data URI. GWT 2.0 introduces IRB into the core SDK, now called ClientBundle, it has been enhanced and polished. Simply put, ClientBundle allows GWT developers to reduce the number of HTTP requests for resources to just 1 or 2 which are &lt;b&gt;cached forever&lt;/b&gt;, pack images optimally, optimize and minify CSS, and much more. Did you know that ClientBundle works with internationalization? That GWT ClientBundle can automatically flip images for Right-To-Left locales like Arabic, and flip CSS rules as well? Crazy awesome!&lt;/dd&gt; &lt;br /&gt;&lt;dt&gt;Optimized for Compression&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;I&#39;m some what tooting my own horn on this feature, since I contributed it, but GWT 2.0 produces obfuscated Javascript in a way that no other tool on the planet does. In addition to renaming pretty much every variable in your entire program in a few symbols as possible, it actually re-orders code to increase gzip compression ratios. And that&#39;s just the beginning. The ability to just keep plugging in optimizations to this toolset is unbounded. Automated PNG optimization in the future? Check. New Deflate algorithms? Check. The list goes on.&lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;HtmlUnit for Testability&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;You want testing? GWT&#39;s got you covered. Java tests? Check. Selenium tests? Check. Web-Mode test in real browser? Yep. But what&#39;s really new, is that GWT can run tests in a simulated browser environment thanks to HtmlUnit. This has two immediate benefits: 1) You can run your tests anywhere and not have to worry about prepping a test environment or platform specific details and 2) Tests can startup much quicker because they don&#39;t need to use a real browser. Again, it would seem like an almost irrelevent footprint if it weren&#39;t for the fact that a lot of effort is going into making this work correctly.&lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;Speed Tracer, &#39;nuff said!&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;1. It&#39;s awesome. 2. It&#39;s actually written in GWT. 3. It enables easy collaboration and sharing of traces. 4. It has a plugin mechanism for analyzing traces and providing automated warnings and suggestions like YSlow!.  5. It&#39;s awesome. 6. It&#39;s awesome!&lt;br /&gt;&lt;/dl&gt;&lt;br /&gt;&lt;br /&gt;But wait, there&#39;s more! These are just the main bullet points, there&#39;s lots of other changes under the hood that are also important, let&#39;s look at a few:&lt;br /&gt;&lt;br /&gt;&lt;dl&gt;&lt;br /&gt;&lt;dt&gt;Story of Your Compile, Compile Reports&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;Think of it as Compile-Time SpeedTracer. SOYC gives you lots of detail about what each part of your app is contributing to the output in terms of size, and what the interdependencies are. This provides valuable feedback as to where to look to reduce bloat, or split-off code better. This would be the equivalent of a JS tool that looked at your jQuery application, and told you exactly how many bytes were used by each method in each jQuery plugin you&#39;re using, and which modules and functions pulled in the others. Think about it.&lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;Better stack traces, and stack trace emulation for IE6&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;The GWT team simply isn&#39;t content with the status quo. When it doesn&#39;t exist, invent it. Internet Explorer does not provide proper Javascript stack frame traces. This means when something goes wrong in your application, it can be difficult to get a detailed report about what call sequence triggered it. GWT 2.0 contains the ability to switch on stack-trace emulation, which causes the compiler to insert line number, file, and callstack recording into every program method. When an error occurs in a GWT application compiled for IE6, you can actually reverse obfuscated emulated stack trace output back into real Java stack traces that tell you file and line number. Com&#39;on, you&#39;re not excited yet?&lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;Schedule Finally&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;Javascript is an event driven programming language. When the Javascript interpreter runs, it is the context of an event pumped through an event queue that enters the interpreter/VM through some handler callback, or default entry point. GWT 2.0 recognizes this fact by becoming the gatekeeper to every entry/exit of the application code, and by doing this, it can perform quite a few useful things. One is, it can implement the idea of an uncaught exception handler and enforce it, so that any exception that causes the JS interpreter to exit/yield can be diverted to a custom handler of your choice. More importantly, GWT can take control of how code is scheduled for execution, beyond setTimeout()/setInterval(). GWT 2.0 introduces the concept of Schedule Finally? What is it? Simply this: Defer execution of a piece of code to run after all other code in the current execution context is run, but before the next event is pumped. This is used, for example, by the GWT style injector, so that CSS style injection can be deferred to after the current event is handled, but run before the next is. This can allow efficient batching of operations that mutate the DOM, among other things. &lt;/dd&gt;&lt;br /&gt;&lt;/dl&gt;&lt;br /&gt;&lt;br /&gt;I could go on, tons of compiler optimizations, Directly Evalable RPC, Conditional Deferred Binding Properties, HTML Standards Mode Support.  One could make the argument, that almost everything in this list can be done, or has been done, elsewhere, or in parts of other tools. But that&#39;s why GWT is so beneficial. It unifies where other approaches fragment. Sure, you could combine your own code splitting, with your own handrolled JS loader, with someone else&#39;s image sprite tool, with another guy&#39;s test framework, and someone else&#39;s minifier, and this guy&#39;s trace analyzer, and that guy&#39;s widget toolset. And if you think you could achieve the same outcome, you&#39;d be absolutely right.&lt;br /&gt;&lt;br /&gt;GWT isn&#39;t about doing what isn&#39;t possible elsewhere. It&#39;s about making you productive doing it. It isn&#39;t about extolling the beauties of Java, it&#39;s about pragmatically recognizing the leverage you get from the existing toolchain, IDEs, test harnesses, and types. It isn&#39;t about hiding the DOM, CSS, and the browser from you. It&#39;s about allowing you to write your abstractions yourself more productively, just like you&#39;d do in hand-coded JS anyway.  Someday, there may even be the ability for GWT to compile other languages to Java, or even process JS with type annotations. &lt;br /&gt;&lt;br /&gt;GWT makes productive, large scale web development possible, and enables next-gen applications to be built reliably.&lt;br /&gt;&lt;br /&gt;-Ray</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/2017433081392011528/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/2017433081392011528' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/2017433081392011528'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/2017433081392011528'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2009/12/gwt-20-so-good-its-ridiculous.html' title='GWT 2.0: So good it&#39;s ridiculous'/><author><name>Ray Cromwell</name><uri>http://www.blogger.com/profile/05070235196940096193</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-7815352793788506172</id><published>2009-12-08T15:56:00.000-08:00</published><updated>2009-12-08T16:16:08.714-08:00</updated><title type='text'>GWT Exporter 2.09 released</title><content type='html'>Improvements since last version:&lt;br /&gt;&lt;dl&gt;&lt;br /&gt;&lt;dt&gt; Support for runtime method overloading. &lt;br /&gt;&lt;dd&gt; Methods on the same class with identical names can be exported as a single Javascript method using run-time type-detection and dispatch. You pay a price in the form of a dispatch table encoded as a JSON object, but only for methods with overloads.&lt;br /&gt;&lt;dt&gt; ExporterUtil::exportAll()&lt;br /&gt;&lt;dd&gt;Feeling lazy? exportAll() will export every single exportable type in your classpath so that you don&#39;t have to invoke GWT.create() manually.&lt;br /&gt;&lt;dt&gt; ExporterUtil::exportAllAsync()&lt;br /&gt;&lt;dd&gt; All exports compiled to a separate JS fragment, loaded asynchronously, and invokes window.onexport() when done.&lt;br /&gt;&lt;dt&gt; Exporter.export() deprecrated&lt;br /&gt;&lt;dd&gt; Now simply invoking GWT.create(SomeClass.class) is sufficient for export.&lt;br /&gt;&lt;dt&gt; Supports GWT 2.0 $entry function&lt;br /&gt;&lt;dd&gt; $entry preserves Java exception handler behavior as well as the correct event-loop entry/exit, like running scheduleFinally commands on exit.&lt;br /&gt;&lt;dt&gt;&lt;b&gt;Requires GWT 2.0 RC2 or higher&lt;/b&gt;&lt;br /&gt;&lt;dd&gt;Because $entry and code-splitting are now integrated features.&lt;br /&gt;&lt;/dl&gt;&lt;br /&gt;&lt;h3&gt;Structural Typing&lt;/h3&gt;&lt;br /&gt;I&#39;m also about 90% done implementing &lt;a href=&quot;http://timepedia.blogspot.com/2009/03/structural-typing-for-gwt-and.html&quot;&gt;Structural Types&lt;/a&gt; for exports, which allows idiomatic interoperability between JS and exported APIs. Structural Typing will be available in an upcoming 2.10 release real soon now.&lt;br /&gt;&lt;h3&gt;The future&lt;/h3&gt;&lt;br /&gt;GWT Exporter was originally written for some dire needs I had, and the codebase is now over 2 years old. The latest additions have been difficult, as the codebase is getting messier. The 2.10 release will be the last release, afterwards, I will use the knowledge gained from the library to work on a rewrite for GWT core that may or may not make it into a future SDK distribution. &lt;br /&gt;&lt;br /&gt;The goals for the future releases will be (1) Never pay for what you don&#39;t use, that is, exports should only bloat code if they absolutely must. 2) Provide flexible way to customize output for various idiomatic Javascript styles. You should be able to generate APIs that look like they were designed by hand. 3) Look at ways to process JSDocs and provide additional ways to automatically bridge JS and Java objects. 4) Improve error checking, diagnostic messages, and quality of auto generated JS docs.&lt;br /&gt;&lt;br /&gt;GWT Exporter is available at &lt;a href=&quot;http://code.google.com/p/gwt-exporter&quot;&gt;http://code.google.com/p/gwt-exporter&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/7815352793788506172/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/7815352793788506172' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/7815352793788506172'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/7815352793788506172'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2009/12/gwt-exporter-209-released.html' title='GWT Exporter 2.09 released'/><author><name>Ray Cromwell</name><uri>http://www.blogger.com/profile/05070235196940096193</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-6236516474721179439</id><published>2009-11-19T16:39:00.001-08:00</published><updated>2009-11-19T18:04:45.617-08:00</updated><title type='text'>The Traveling Salesman Problem and Javascript Compression</title><content type='html'>In my last post, &lt;a href=&quot;http://timepedia.blogspot.com/2009/08/on-reducing-size-of-compressed.html&quot;&gt;On Reducing the Size of Compressed Javascript&lt;/a&gt;, I covered a technique I had been researching on re-ordering Javascript code to enhance the efficiency of the gzip/deflate compressors. The basic approach is to come up with some way of measuring the distance between two functions, and then trying to group functions together by shortest distance.&lt;br /&gt;&lt;br /&gt;A key question is, what&#39;s a good metric?  I knew that the ideal metric would take into account how the LZ77 algorithm works, but for expediency, I went with a reasonable and well known string distance: the levenshtein distance.&lt;br /&gt;&lt;h2&gt;GZip vs 7-zip&lt;/h2&gt;&lt;br /&gt;Another finding from my previous experiments was that 7-zip sports a much superior deflate implementation, it can compress a given input using the same algorithm as gzip with non-trivial size improvements. The problem is, 7-zip is implemented in C++, and I wanted to write a GWT Linker to automatically produce super-compressed Javascript.&lt;br /&gt;&lt;br /&gt;In digging around for a Java 7-zip deflate implementation, I ran into this paper on &lt;a href=&quot;http://www.paul.sladen.org/projects/http://www.paul.sladen.org/projects/compression/&quot;&gt;Paul Sladen&#39;s page.&lt;/a&gt; titled:  &lt;a href=&quot;http://cis.poly.edu/tr/tr-cis-2004-02.pdf&quot;&gt;Compressing File Collections with a TSP-Based Approach&lt;/a&gt;. This paper published in 2004 details a similar technique of enhancing TAR compression of a collection of files by re-ordering the TAR to maximize compression. More on that later, but the paper itself has an obscure reference to something called the &lt;span style=&quot;font-weight:bold;&quot;&gt;LZ Distance&lt;/span&gt; metric. Tracking down that paper &lt;a href=&quot;http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.41.348&quot;&gt;&quot;G. Cormode, M. Paterson, S. Sahinalp, and U. Vishkin. Communication complexity of doc- ument exchange. In Proc. of the ACM–SIAM Symp. on Discrete Algorithms, January 2000.&quot;&lt;/a&gt; leads us to the definition of LZ Distance.&lt;br /&gt;&lt;h2&gt;LZ Distance&lt;/h2&gt;&lt;br /&gt;The LZ Distance between two strings &lt;tt&gt;x&lt;/tt&gt; and &lt;tt&gt;y&lt;/tt&gt; denoted by &lt;tt&gt;LZDIST(x,y)&lt;/tt&gt; is as follows:&lt;br /&gt;&lt;blockquote&gt;The minimum number of single characters or substrings of &lt;tt&gt;y&lt;/tt&gt; or of the partially built string, which are required to build &lt;tt&gt;x&lt;/tt&gt; from left to right.&lt;/blockquote&gt;&lt;br /&gt;As an example, consider &lt;tt&gt;x=aabcdefgabc&lt;/tt&gt; and &lt;tt&gt;y=aaaaaaaaaaaaaaaa&lt;/tt&gt;.  We try to parse x in terms of y, and keep a running partially built string called &#39;p&#39;.  Here are the steps:&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt; Prefix &#39;aa&#39; of x is contained in y, so p = (aa), x = bcdefgabc&lt;br /&gt;&lt;li&gt; No prefix of x found in y or p, so just add &#39;b&#39;  p = (aa)(b), x = cdefgabc&lt;br /&gt;&lt;li&gt; No prefix of x found in y or p ,so just add &#39;c&#39;, p = (aa)(b)(c), x = defgabc&lt;br /&gt;&lt;li&gt; No prefix of x found in y or p ,so just add &#39;d&#39;, p = (aa)(b)(c)(d), x = efgabc&lt;br /&gt;&lt;li&gt; No prefix of x found in y or p ,so just add &#39;e&#39;, p = (aa)(b)(c)(d)(e), x = fgabc&lt;br /&gt;&lt;li&gt; No prefix of x found in y or p ,so just add &#39;f&#39;, p = (aa)(b)(c)(d)(e)(f), x = gabc&lt;br /&gt;&lt;li&gt; No prefix of x found in y or p ,so just add &#39;g&#39;, p = (aa)(b)(c)(d)(e)(f)(g), x = abc&lt;br /&gt;&lt;li&gt; &#39;abc&#39; is found at index &#39;1&#39; in p, so p = (aa)(b)(c)(d)(e)(f)(g)(abc)&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;Here, the LZDIST(x,y) = 8. But what of LZDIST(y, x)?&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt; Prefix &#39;aa&#39; of y is contained in x, so p = (aa), y = aaaaaaaaaaaaaa&lt;br /&gt;&lt;li&gt; Prefix &#39;aa&#39; of y is contained in x, so  p = (aa)(aa), y = aaaaaaaaaaaa&lt;br /&gt;&lt;li&gt; Prefix &#39;aaaa&#39; of y is contained in p, p = (aa)((aa)(aaaa) y = aaaaaaaa&lt;br /&gt;&lt;li&gt; Prefix &#39;aaaaaaaa&#39; of y is contained in p, so p = (aa)(aa)(aaaa)(aaaaaaaa)&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;LZDIST(y,x) = 4. Interesting, but what does this buy us?&lt;br /&gt;&lt;h2&gt;LZDIST and GZIP&lt;/h2&gt;&lt;br /&gt;Recall that GZIP&#39;s deflate encoder uses an algorithm called LZ77 which looks at the current input tokens, and searches backwards for previous substrings that match the current input string, replacing them with back references. What LZDIST(x,y) gives us, is a measure of how many back references  for X can be found in Y, that is, how X can be compressed by references to substrings in Y or in partially built decompression outputs.&lt;br /&gt;&lt;br /&gt;Now you can see why this might help improve code re-ordering. If we can order Javascript functions in order to maximize back-references, we might be able to squeeze a little more out of gzip.&lt;br /&gt;&lt;h2&gt;Clustering with LZDIST&lt;/h2&gt;&lt;br /&gt;Remember the earlier paper&#39;s title had the acronym TSP? This paper discusses how re-ordering can be viewed as an optimal tour on a graph which means that solving it is as hard as the Traveling Salesman Problem - NP-Complete. Uh oh, that sounds like trouble, but that&#39;s only NP Complete for the optimal solution. A good estimate on the true solution might also produce good results, and this paper shows that a greedy tour heuristic produces up to a 10% benefit in compression.&lt;br /&gt;&lt;br /&gt;For expediency reasons, I decided not to implement a TSP model at this point, but went with simple pair-based greedy approach detailed in my previous blog post, substituting LZDIST for Levenshtein Distance. I also implemented an LZ-like greedy algorithm where I keep a running string which is the concatenation of all of the functions which have been added to the output, and use LZDIST to find the next closest function to append within a 32k window. This mimics how a gzip compressor might see the input. &lt;br /&gt;&lt;h2&gt;Results for GWT Showcase Application&lt;/h2&gt;&lt;br /&gt;&lt;table border=&quot;1&quot;&gt;&lt;br /&gt;&lt;tr&gt;&lt;th&gt;Technique&lt;/th&gt;&lt;th&gt;Size&lt;/th&gt;&lt;th&gt;Ratio&lt;/th&gt;&lt;th&gt;Relative Improvement (vs previous row)&lt;/th&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;th&gt;No Compression&lt;/th&gt;&lt;td align=&quot;right&quot;&gt;196778&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;100%&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;th&gt;gzip -9&lt;/th&gt;&lt;td align=&quot;right&quot;&gt;64401&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;32.7%&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;32.7%&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;th&gt;7zip&lt;/th&gt;&lt;td align=&quot;right&quot;&gt;61500&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;31.5%&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;4.6%&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;th&gt;gzip -9 + levensthein&lt;/th&gt;&lt;td align=&quot;right&quot;&gt;61409&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;31.2%&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;4.7%&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;th&gt;7zip + levenshtein&lt;/th&gt;&lt;td align=&quot;right&quot;&gt;58536&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;29.7%&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;4.7%&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;th&gt;7zip + lzdist&lt;/th&gt;&lt;td align=&quot;right&quot;&gt;57923&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;29.4%&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;1.1%&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;th&gt;LZMA&lt;/th&gt;&lt;td align=&quot;right&quot;&gt;51423&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;26.1%&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;11.3%&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;/table&gt;&lt;br /&gt;Looking at relative compression, levenshtein vs lzdist, the difference 57923/58536 = 1.1% which seems worthwhile to me. In a large script, this can save 1-2 kilobytes. I threw in LZMA which boasts 12% better compression in this instance than DEFLATE. &lt;br /&gt;&lt;h2&gt;Notes on implementation&lt;/h2&gt;&lt;br /&gt;The LZDIST function is expensive to compute in a naive fashion. It is similar to the &lt;a href=&quot;http://en.wikipedia.org/wiki/Longest_common_substring&quot;&gt;longest common substring problem&lt;/a&gt;.  The brute force version can be on the order of O(n^3), while a dynamic programming version can run in O(n^2). To make this run efficiently and not irritate your users, you need to use a Generalized Suffix Tree approach. Compressors like 7-zip are already using Suffix trees to implement the LZ77 search algorithm, so those of you looking for hints to do an efficient implementation should take a look at the LZMA SDK (both C++ and Java versions are available)&lt;br /&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;br /&gt;Utilizing clustering compression techniques, LZ distance, and 7-zip, it is possible to reduce the GWT Showcase application from 64401 bytes to 57923 bytes, a savings of about 10.1%.&lt;br /&gt;&lt;br /&gt;-Ray</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/6236516474721179439/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/6236516474721179439' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/6236516474721179439'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/6236516474721179439'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2009/11/traveling-salesman-problem-and.html' title='The Traveling Salesman Problem and Javascript Compression'/><author><name>Ray Cromwell</name><uri>http://www.blogger.com/profile/05070235196940096193</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-3371243308417222097</id><published>2009-08-17T20:13:00.000-07:00</published><updated>2009-08-18T12:24:30.092-07:00</updated><title type='text'>On Reducing the Size of Compressed Javascript (by up to 20%)</title><content type='html'>Recently, I&#39;ve been studying ways of reducing the download size of Javascript applications produced by Google Web Toolkit, while preserving or improving startup time. There are a number of ways to do this, the first of which is to transform Javascript code into a form that is naturally more succinct, while eliminating unused code, or deferring the load of some code until later.&lt;br /&gt;&lt;br /&gt;Unfortunately, there is a tradeoff inherent in some of the fancier ways of making JS more succinct that use run-time code generation. This is put to good use in libraries like &lt;i&gt;jQuery&lt;/i&gt;. For a large application, like Google Wave, this would increase startup time.  Another technique is to teach a compiler even better optimizations for reducing redundant code, work which is ongoing in Google Web Toolkit.&lt;br /&gt;&lt;br /&gt;Other attempts to reduce JS size have looked at ways to remove extraneous whitespace, shorten identifiers, and tokenize/pack JS statements, and while the former two are win-win one-way transforms, the latter has the trade-off requirement of being a reversible transform, meaning that it has to be decoded by an additional JS stage after load, a step that hurts startup performance.&lt;br /&gt;&lt;br /&gt;Instead, I was drawn to a reversible transform the browser already includes support for: gzip compression,  and decided to ask the question: what effect does the large-scale structure of the JS output code have on the &lt;i&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/DEFLATE_(algorithm)&quot;&gt;DEFLATE&lt;/a&gt;&lt;/i&gt; algorithm of GZIP which is used to serve up compressed script? The answer it turns out, is substantial.&lt;br /&gt;&lt;h2&gt;Reversible Transforms&lt;/h2&gt;&lt;br /&gt;If we didn&#39;t care about startup performance, we could spend all the time in the world unpacking JS code by including a custom tailored compressor, perhaps the &lt;a href=&quot;http://en.wikipedia.org/wiki/Lzma&quot;&gt;LZMA (Lempel-Ziv Markov Chaining)&lt;/a&gt; algorithm or &lt;a href=&quot;http://en.wikipedia.org/wiki/Prediction_by_partial_matching&quot;&gt;PPM (Prediction by Partial Matching)&lt;/a&gt;, but unfortunately, these algorithms would be very slow to run in Javascript. &lt;br /&gt;&lt;br /&gt;That leaves us with the built in GZIP compression that most web browsers include support for. The question is, can we improve compression while remaining compatible with the browser&#39;s decoder? There is an existing example of improving GZIP by injecting a reversible transform: bzip2 and the &lt;a href=&quot;http://en.wikipedia.org/wiki/Burrows-Wheeler_transform&quot;&gt;Burrows-Wheeler Transform&lt;/a&gt;. &lt;br /&gt;&lt;br /&gt;Which brings up an interesting idea, can we do something like BWT&#39;s sort but for Javascript, in a way that doesn&#39;t require an extra pass to &#39;undo&#39; the sorting?&lt;br /&gt;&lt;h2&gt;Deflate: A digression&lt;/h2&gt;&lt;br /&gt;Before answering that question, it would be helpful to look at some of the restrictions of the &lt;i&gt;deflate&lt;/i&gt; algorithm, and how code ordering could affect the outcome. &lt;br /&gt;&lt;br /&gt;The deflate algorithm is a combination of two compression algorithms: &lt;a href=&quot;http://en.wikipedia.org/wiki/LZ77_and_LZ78&quot;&gt;LZ77&lt;/a&gt; and &lt;a href=&quot;http://en.wikipedia.org/wiki/Huffman_coding&quot;&gt;Huffman coding&lt;/a&gt;. Huffman coding is a variable length code technique where symbols are replaced with codes based on their frequency of occurrence. So for example, the most common letter in the English alphabet is the letter &lt;tt&gt;e&lt;/tt&gt;, so replacing &lt;tt&gt;e&lt;/tt&gt; with a short code, but giving &lt;tt&gt;q&lt;/tt&gt;, the least frequent character, a longer code, leads to shorter text overall.  Huffman encoding is order independent, so that &#39;eq&#39; is the same length as &#39;qe&#39;. &lt;br /&gt;&lt;br /&gt;LZ77 on the other hand, is a sliding window compression algorithm based on replacing strings with backwards references to previous strings in the input. For example, the string &quot;this is a test&quot; contains the substring &#39;is&#39; repeated twice in a row, separated by a space, so that the second occurance of &#39;is&#39; can be replaced with a length (2 characters, and a backwards distance (-3 positions),  called the length-distance pair. The compressor typically scans backwards in the input within a certain window (e.g. 8,192 characters or 32,768 characters) looking for matches and then encoding them as length-distance pairs. The compressor has some freedom as to how hard it will search for a match before giving up (something I&#39;ll get to later).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;One important effect of the sliding window limit is that if two Javascript functions with common substrings are separated by more than this distance, they cannot be matched.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;But DEFLATE has another trick up its sleeve. It encodes the output of the LZ77 algorithm using Huffman encoding, but uses one Huffman tree for the character literals and length codes, and another Huffman tree for the backwards distances. &lt;br /&gt;&lt;br /&gt;&lt;b&gt;Which suggests another potential gain is to try and arrange for the backwards distances to be both small, and frequently the same, so as it produce shorter Huffman codes.&lt;/b&gt;&lt;br /&gt;&lt;h2&gt;A sort with no undo&lt;/h2&gt;&lt;br /&gt;Thus far, intuition would tell us that if we could rearrange the input in order to bring more closely matched text closer together, we might be able to push up compression ratios, but how to do this without something to reverse the sort? Fortunately, unlike BWT, we are not working on plain text, but machine readable program code. We already know of something that rearranges code and moves it around, it&#39;s called a compiler!&lt;br /&gt;&lt;br /&gt;We would not want a one-way text compression sort which say, brings Hamlet&#39;s prologue, climax, and epilogue together and randomly rearranges the rest, but your browser has no problem running your Javascript code if function &lt;tt&gt;foo&lt;/tt&gt; is declared after &lt;tt&gt;bar&lt;/tt&gt;, or before it. Thus, as long as two statements do not have order-dependencies, we can arrange them freely, in fact, all top-level function declarations can be rearranged arbitrarily.&lt;br /&gt;&lt;h2&gt;Sort by Clustering&lt;/h2&gt;&lt;br /&gt;So, we&#39;ve finally arrived at the point in which we have to devise our sort algorithm. What we want to do is, for any function &lt;tt&gt;Foo&lt;/tt&gt; ensure that the best-match for this function in the whole program appears closest within the sliding window that the compressor will use. But bringing two functions that are most similar together in a greedy algorithm fashion won&#39;t necessarily produce the optimal result, as it is possible that by moving function &lt;tt&gt;Bar&lt;/tt&gt; closer to function &lt;tt&gt;Foo&lt;/tt&gt;, you&#39;ve moved it away from lots of other functions that were good matches as well.  As an example, consider these strings:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&quot;Hello World&quot;&lt;br /&gt;&quot;Hello World is a common test string used.&quot;&lt;br /&gt;...several thousand strings later including the phrase &quot;hello world&quot; ...&lt;br /&gt;&quot;Common test strings used are metasyntactic variables like Foo and Bar.&quot;&lt;br /&gt;&quot;Variables like Foo and Bar are very common.&quot;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;If we selectively moved the second string close to the first, we might prevent it from matching the other good matches later, especially if the window fills up.&lt;br /&gt;&lt;br /&gt;One idea I started to think about was to repurpose &lt;i&gt;Document Clustering&lt;/i&gt; techniques towards code. Document Clustering is commonly used in information retrieval systems to find related documents. Typically, a document is encoded using some technique to measure word importance, such as representing each word by its &lt;i&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Tf-idf&quot;&gt;term frequency inverse document frequency&lt;/a&gt;&lt;/i&gt;. Then, any two documents can be compared by some distance metric, for example, taking the tf-idf weightings of terms as a vector in N-space and computing the cosine between them.&lt;br /&gt;&lt;br /&gt;In this case, we&#39;d let each function be a separate document, and the entire program be like the corpus of documents. We&#39;d then choose some encoding to weigh Javascript grammar nodes by importance in a way that would produce good LZ77 matches, and then proceed in a bottom-up clustering fashion. First, we&#39;d construct all the pairs of functions which match best. Pick a function, pair it with its best match, call that Cluster 1. Pick another function, pair it with its best match, call that Cluster 2, and so on. After this procedure is done, pick a Cluster, and find its nearest Cluster (according to some metric) and pair them up in a Cluster of 4 functions. After that&#39;s done, pair up 4-Clusters into Clusters of 8, and so on, until the final cluster encompasses the whole program.&lt;br /&gt;&lt;h2&gt;What&#39;s a good metric?&lt;/h2&gt;&lt;br /&gt;Ideally, a good metric for comparing two functions would take into account the way the GZIP compressor searches for matches and encodes them. It is an interesting theoretical question, but for practical implementation purposes, I needed something that performs reasonably well, now. One algorithm that is pretty good at finding strings of rough similarity, even in the presence of noise, is the dynamic programming edit-distance algorithm. It&#39;s deployed widely in one variant or another in the bioinformatics industry for gene sequence alignment (Smith-Waterman, HMMR, etc), but the version commonly used for general CS work is the &lt;a href=&quot;http://en.wikipedia.org/wiki/Levenshtein_distance&quot;&gt;Levenshtein Distance&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Results&lt;/h2&gt;&lt;br /&gt;Taking Levenshtein Distance as my metric, I produced a greedy variant as a patch to the GWT compiler. The greedy variant does not implement bottom-up clustering, but instead, sorts all functions by length first (suggested by GWT team member Lex Spoon), and then performs a linear scan over the sorted functions, picking the best match each time to the previous output. The source code is &lt;a href=&quot;http://code.google.com/p/google-web-toolkit/source/detail?r=5972&quot;&gt;here&lt;/a&gt;. Remarkably, even this simple algorithm produces nice gains? How much? Well, if you do nothing else, it produces a 5-7% gain in GWT&#39;s Showcase application when compressed with gzip -9. But there&#39;s more that we can do!&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Optimizing GWT&#39;s Obfuscation&lt;/h2&gt;&lt;br /&gt;When the GWT compiler is executed in obfuscated mode, it renames every single Javascript identifier in the whole program except for foreign Javascript. Ideally, you want the shortest id possible. Up until recently, GWT limited the first character of an identifier to an alphabet of 32-characters, and for strings of length 2 or more, it used base-64 characters. However, due to a clever patch by GWT community member Andriasyan (&lt;a href=&quot;http://code.google.com/p/google-web-toolkit/issues/detail?id=2448&quot;&gt;Issue #2448&lt;/a&gt;), the first character can actually be chosen from a base-54 alphabet. This has the effect of shrinking output size by up to 1.75% prior to compression.&lt;br /&gt;&lt;br /&gt;We&#39;re not done yet! The GWT compiler has other tricks up its sleeve. It performs the renaming from the bottom-most scopes upwards, letting each scope reuse variable identifiers as they become free. However, it unfortunately did not insure that identifiers were picked in a stable order. Thus, a function of 3 variables could be declared as &lt;tt&gt;function foo(a,b,c){&lt;/tt&gt; or as &lt;tt&gt;function foo(b,c,a){&lt;/tt&gt;. Obviously, this would lead to suboptimal compression since every function of 3 variables should have the same suffix &lt;tt&gt; (a,b,c){&lt;/tt&gt;. The effect of making obfuscated identifier allocation have a stable sort order combined with the base-54 patch produces an incredible gain of 10.5% when compressed with gzip -9.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Choosing a different GZIP implementation&lt;/h2&gt;&lt;br /&gt;The deflate algorithm actually gives some leeway to the compressor implementor in terms of how matches are found. ZLIB&#39;s implementation on which GZIP is based is actually not the best implementation, although it might be the best &lt;i&gt;patent unencumbered one.&lt;/i&gt; Rather, the inventor of the &lt;b&gt;LZMA&lt;/b&gt; algorithm has his own DEFLATE implementation in his &lt;b&gt;7-zip&lt;/b&gt; utility, which produces 4% better output than gzip by my estimates.&lt;br /&gt;&lt;br /&gt;Combining base-54/base-64 obfuscated identifier encoding, stable sort-order for identifier allocation, my greedy clustering-by-edit-distance sort algorithm, and 7-zip as a gzip-compatible compressor, yields an incredible 21% reduction of the Showcase application. On a large 500k Javascript application, this means an additional 100k bandwidth is saved, with no performance penalty!&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;br /&gt;A general purpose technique (&lt;b&gt;Cromwell Clustering Transform?  (CCT) :)&lt;/b&gt;) for compilers to rearrange code for compression efficiency (vs say, cache locality) has been presented, which achieves non-trivial compression efficiency gains in Javascript output from the GWT compiler. Some of these techniques can also be applied to hand-written Javascript as well and included in 3rd party JS minification utilities.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Addendum&lt;/h2&gt;&lt;br /&gt;From reading a description of the algorithm, it may be hard to visualize. Here is sample output from GWT&#39;s Showcase application:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;function lU(a){gU();while(bU){bU.a.a.a.p=false;$wnd.alert(s5b+a+t5b);bU=bU.b}cU=null}&lt;br /&gt;function vR(a){qR();while(lR){lR.a.b.a.p=false;$wnd.alert(s5b+a+t5b);lR=lR.b}mR=null}&lt;br /&gt;function QR(a){LR();while(GR){GR.a.b.a.p=false;$wnd.alert(s5b+a+t5b);GR=GR.b}HR=null}&lt;br /&gt;function mS(a){hS();while(cS){cS.a.b.a.p=false;$wnd.alert(s5b+a+t5b);cS=cS.b}dS=null}&lt;br /&gt;function KS(a){FS();while(AS){AS.a.b.a.p=false;$wnd.alert(s5b+a+t5b);AS=AS.b}BS=null}&lt;br /&gt;function gT(a){bT();while(YS){YS.a.b.a.p=false;$wnd.alert(s5b+a+t5b);YS=YS.b}ZS=null}&lt;br /&gt;function PT(a){KT();while(FT){FT.a.b.a.p=false;$wnd.alert(s5b+a+t5b);FT=FT.b}GT=null}&lt;br /&gt;function JU(a){EU();while(zU){zU.a.b.a.p=false;$wnd.alert(s5b+a+t5b);zU=zU.b}AU=null}&lt;br /&gt;function fV(a){aV();while(XU){XU.a.a.a.p=false;$wnd.alert(s5b+a+t5b);XU=XU.b}YU=null}&lt;br /&gt;function DV(a){yV();while(tV){tV.a.a.a.p=false;$wnd.alert(s5b+a+t5b);tV=tV.b}uV=null}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;For any function, note that the one immediately following it contains large numbers of common substrings of length 3 or greater.</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/3371243308417222097/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/3371243308417222097' title='17 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/3371243308417222097'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/3371243308417222097'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2009/08/on-reducing-size-of-compressed.html' title='On Reducing the Size of Compressed Javascript (by up to 20%)'/><author><name>Ray Cromwell</name><uri>http://www.blogger.com/profile/05070235196940096193</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>17</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-8673348949122587554</id><published>2009-05-14T12:49:00.001-07:00</published><updated>2009-05-14T13:02:02.864-07:00</updated><title type='text'>Gwt Query and Timefire @ Google I/O</title><content type='html'>If you&#39;re attending &lt;a href=&quot;http://code.google.com/events/io/&quot;&gt;Google I/O&lt;/a&gt; this year (and you should, it&#39;s not to late to &lt;a href=&quot;https://io2009.event-projects.com/&quot;&gt;Register&lt;/a&gt;, $50 for Academia!) I&#39;m doing two sessions. The first:&lt;br /&gt;&lt;a href=&quot;http://code.google.com/events/io/sessions.html&quot;&gt;Progressively Enhance Ajax Applications with Google Web Toolkit and GQuery&lt;/a&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;br /&gt;Google Web Toolkit&lt;br /&gt;Ray Cromwell&lt;br /&gt;Thu 1:15p - 2:15p&lt;br /&gt;Don&#39;t throw away your Web 1.0 websites just yet. In this session, you&#39;ll learn how to take your existing websites, and layer AJAX on top of them using GQuery, a jQuery style library for GWT. Learn how GQuery benefits performance over existing solutions, increases productivity, and reduces defects by leveraging the GWT tool chain.&lt;br /&gt;&lt;/i&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;The second:&lt;br /&gt;&lt;a href=&quot;http://code.google.com/events/io/sessions.html&quot;&gt;Building Applications With Google APIs&lt;br /&gt;&lt;/a&gt;&lt;blockquote&gt;&lt;i&gt;&lt;br /&gt;Google Web Toolkit&lt;br /&gt;Ray Cromwell&lt;br /&gt;Thu 3:45p - 4:45p&lt;br /&gt;Google offers a wide variety of APIs in many domains that together form a complete platform, from authentication and authorization, cloud computing, and social networking, to visualization, mobile computing, and Google Web Toolkit. In this talk, we will walk though a complex application that integrates many APIs together, how each can solve a different need in your application, how you can share code between GWT, Android, and App Engine, and how you can monetize your application with Google Checkout.&lt;br /&gt;&lt;/blockquote&gt;&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;In the first presentation, I&#39;ll detail the revamped and relaunched GWT Query library, a jQuery clone for GWT featuring fast implementation, terse syntax, type safety, and a few other tricks. If you&#39;ve ever wanted to use GWT, but didn&#39;t want to program in a Desktop Widget metaphor, this talk is for you.&lt;br /&gt;&lt;br /&gt;The second presentation sounds stale (I couldn&#39;t write more exciting copy, honest!) but it is infact, the story of Timefire, a software-as-a-service visualization/analytics platform we are building that leverages a number of Google APIs, from AppEngine, to GWT, to Android, OpenSocial, Checkout, and beyond. If you&#39;ve got a need, Google has an API for it, and in my opinion, what they have is looking more and more like a complete platform, from client to cloud.&lt;br /&gt;&lt;br /&gt;So if this sounds interesting, mark Thursday on your calendars, and come see the amazing things you can do with Google Web Toolkit.&lt;br /&gt;&lt;br /&gt;-Ray</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/8673348949122587554/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/8673348949122587554' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/8673348949122587554'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/8673348949122587554'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2009/05/gwt-query-and-timefire-google-io.html' title='Gwt Query and Timefire @ Google I/O'/><author><name>Ray Cromwell</name><uri>http://www.blogger.com/profile/05070235196940096193</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-7023805243463162600</id><published>2009-04-24T13:43:00.000-07:00</published><updated>2009-04-24T14:20:00.293-07:00</updated><title type='text'>GWT RPC over arbitrary transports: Uber RPC</title><content type='html'>A frequent request that pops up in the GWT groups is how to run GWT RPC over non-XHR transport mechanisms, like script-tag injection, cross-domain POSTs, or OpenSocial Gadget&#39;s &lt;code&gt;makeRequest()&lt;/code&gt;. &lt;br /&gt;&lt;br /&gt;There are a few ways to do this, including patching GWT itself, but the path of least resistance would be a module that you could inherit which would provide this functionality, without compromising or changing your client code in any way. &lt;br /&gt;&lt;h2&gt;Step 1: Overwrite GWT ServiceInterfaceProxyGenerator&lt;/h2&gt;&lt;br /&gt;In order to handle alternate transport mechanisms, we need to take over generation of the RPC client stub that is created by the builtin generators, to do this, we add the following entries to a module file:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush:xml&quot;&gt;&lt;br /&gt; &lt;generate-with&lt;br /&gt;            class=&quot;com.google.gwt.user.rebind.rpc.UberServiceInterfaceProxyGenerator&quot;&gt;&lt;br /&gt;        &lt;when-type-assignable&lt;br /&gt;                class=&quot;com.google.gwt.user.client.rpc.RemoteService&quot;/&gt;&lt;br /&gt;    &lt;/generate-with&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This tells GWT to invoke our &lt;code&gt;UberServiceIntefaceProxyGenerator&lt;/code&gt; whenever someone calls &lt;code&gt;GWT.create(RemoteService.class)&lt;/code&gt;. There are some package-protected methods we need access to, so we arrange for our class to be in the &lt;code&gt;com.google.gwt.user.rebind.rpc&lt;/code&gt; package. Next, we want to modify the generator to allow substitution of arbitrary client stub superclasses.&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush:xml&quot;&gt;&lt;br /&gt;&amp;lt;define-property name=&quot;gwt.rpc.proxySuperclass&quot;&lt;br /&gt;                     values=&quot;org_timepedia_uberrpc_client_RpcServiceProxy&quot;/&gt;&lt;br /&gt;&amp;lt;set-property name=&quot;gwt.rpc.proxySuperclass&quot;&lt;br /&gt;                  value=&quot;org_timepedia_uberrpc_client_RpcServiceProxy&quot;/&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This construct is used to pass a compile time parameter to the generator as to which class will be used as a client stub, &lt;code&gt;org.timepedia.uberrpc.client.RpcServiceProxy&lt;/code&gt;.  Here is our proxy generator source, which exists merely to redirect to &lt;code&gt;UberProxyCreator&lt;/code&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;public class UberServiceInterfaceProxyGenerator extends Generator {&lt;br /&gt;&lt;br /&gt;  @Override&lt;br /&gt;  public String generate(TreeLogger logger, GeneratorContext ctx,&lt;br /&gt;      String requestedClass) throws UnableToCompleteException {&lt;br /&gt;&lt;br /&gt;    logger.log(TreeLogger.WARN, &quot;Running UberProxyCreator&quot;, null);&lt;br /&gt;    &lt;br /&gt;    TypeOracle typeOracle = ctx.getTypeOracle();&lt;br /&gt;    assert (typeOracle != null);&lt;br /&gt;&lt;br /&gt;    JClassType remoteService = typeOracle.findType(requestedClass);&lt;br /&gt;    if (remoteService == null) {&lt;br /&gt;      logger.log(TreeLogger.ERROR, &quot;Unable to find metadata for type &#39;&quot;&lt;br /&gt;          + requestedClass + &quot;&#39;&quot;, null);&lt;br /&gt;      throw new UnableToCompleteException();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    if (remoteService.isInterface() == null) {&lt;br /&gt;      logger.log(TreeLogger.ERROR, remoteService.getQualifiedSourceName()&lt;br /&gt;          + &quot; is not an interface&quot;, null);&lt;br /&gt;      throw new UnableToCompleteException();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    UberProxyCreator proxyCreator = new UberProxyCreator(remoteService);&lt;br /&gt;&lt;br /&gt;    TreeLogger proxyLogger = logger.branch(TreeLogger.DEBUG,&lt;br /&gt;        &quot;Generating client proxy for remote service interface &#39;&quot;&lt;br /&gt;            + remoteService.getQualifiedSourceName() + &quot;&#39;&quot;, null);&lt;br /&gt;&lt;br /&gt;    return proxyCreator.create(proxyLogger, ctx);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h2&gt;Step 2; Override the superclass of the generated client proxy stub&lt;/h2&gt;&lt;br /&gt;This source was mostly copied unchanged from the original, except for the line which calls &lt;code&gt;UberProxyCreator&lt;/code&gt;. The bulk of the work is done there. Again, I copied the source, but made just a few changes to the routine which creates the &lt;code&gt;SourceWriter&lt;/code&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;private SourceWriter getSourceWriter(TreeLogger logger, GeneratorContext ctx,&lt;br /&gt;      JClassType serviceAsync) {&lt;br /&gt;    JPackage serviceIntfPkg = serviceAsync.getPackage();&lt;br /&gt;    String packageName = serviceIntfPkg == null ? &quot;&quot; : serviceIntfPkg.getName();&lt;br /&gt;    PrintWriter printWriter = ctx&lt;br /&gt;        .tryCreate(logger, packageName, getProxySimpleName());&lt;br /&gt;    if (printWriter == null) {&lt;br /&gt;      return null;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    ClassSourceFileComposerFactory composerFactory&lt;br /&gt;        = new ClassSourceFileComposerFactory(packageName, getProxySimpleName());&lt;br /&gt;&lt;br /&gt;    String[] imports = new String[]{RemoteServiceProxy.class.getCanonicalName(),&lt;br /&gt;        ClientSerializationStreamWriter.class.getCanonicalName(),&lt;br /&gt;        GWT.class.getCanonicalName(), ResponseReader.class.getCanonicalName(),&lt;br /&gt;        SerializationException.class.getCanonicalName()};&lt;br /&gt;    for (String imp : imports) {&lt;br /&gt;      composerFactory.addImport(imp);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    String rpcSuper = null;&lt;br /&gt;    try {&lt;br /&gt;      // retrieve user-defined superclass from module file&lt;br /&gt;      rpcSuper = ctx.getPropertyOracle()&lt;br /&gt;          .getPropertyValue(logger, &quot;gwt.rpc.proxySuperclass&quot;);&lt;br /&gt;      if (rpcSuper != null) {&lt;br /&gt;        rpcSuper = rpcSuper.replaceAll(&quot;_&quot;, &quot;.&quot;);&lt;br /&gt;      }&lt;br /&gt;    } catch (Exception e) {&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // allow defining a custom superclass to customize the RPC implementation&lt;br /&gt;    composerFactory.setSuperclass(rpcSuper);&lt;br /&gt;    composerFactory.addImplementedInterface(&lt;br /&gt;        serviceAsync.getErasedType().getQualifiedSourceName());&lt;br /&gt;&lt;br /&gt;    composerFactory.addImplementedInterface(&lt;br /&gt;        serviceAsync.getErasedType().getQualifiedSourceName());&lt;br /&gt;&lt;br /&gt;    return composerFactory.createSourceWriter(ctx, printWriter);&lt;br /&gt;  }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h2&gt;Step 3: Write your own Proxy&lt;/h2&gt;&lt;br /&gt;This is where you come in, since you have to decide how you&#39;re going to transport the RPC payload, such as putting it as a URL GET parameter, a POST parameter, or using an OpenSocial container. Here is some skeleton code showing you how to override the &lt;code&gt;doInvoke&lt;/code&gt; method. This example is pseudo-code for how you&#39;d do it using &lt;code&gt;gadgets.io.makeRequest()&lt;/code&gt; in an OpenSocial container.&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush:java&quot;&gt;&lt;br /&gt;public class RpcServiceProxy extends RemoteServiceProxy {&lt;br /&gt;&lt;br /&gt;  protected GadgetRpcServiceProxy(String moduleBaseURL,&lt;br /&gt;      String remoteServiceRelativePath, String serializationPolicyName,&lt;br /&gt;      Serializer serializer) {&lt;br /&gt;    super(moduleBaseURL, remoteServiceRelativePath, serializationPolicyName,&lt;br /&gt;        serializer);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  static boolean isReturnValue(String encodedResponse) {&lt;br /&gt;     return encodedResponse.startsWith(&quot;//OK&quot;);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   /**&lt;br /&gt;    * Return &lt;code&gt;true&lt;/code&gt; if the encoded response contains a checked&lt;br /&gt;    * exception that was thrown by the method invocation.&lt;br /&gt;    * &lt;br /&gt;    * @param encodedResponse&lt;br /&gt;    * @return &lt;code&gt;true&lt;/code&gt; if the encoded response contains a checked&lt;br /&gt;    *         exception that was thrown by the method invocation&lt;br /&gt;    */&lt;br /&gt;   static boolean isThrownException(String encodedResponse) {&lt;br /&gt;     return encodedResponse.startsWith(&quot;//EX&quot;);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;  public static final String RPC_PAYLOAD_PARAM=&quot;rpcpayload&quot;;&lt;br /&gt;  &lt;br /&gt;  @Override&lt;br /&gt;  protected &lt;T&gt; Request doInvoke(&lt;br /&gt;      final RequestCallbackAdapter.ResponseReader responseReader, String methodName, int invocationCount,&lt;br /&gt;      String requestData, final AsyncCallback&lt;T&gt; tAsyncCallback) {&lt;br /&gt;&lt;br /&gt;    try {&lt;br /&gt;      makeRequest(getServiceEntryPoint(), &quot;text/x-gwt-rpc; charset=utf-8&quot;, requestData, new AsyncCallback&lt;String&gt;() {&lt;br /&gt;        public void onFailure(Throwable throwable) {&lt;br /&gt;           tAsyncCallback.onFailure(new InvocationException(&quot;Unable to initiate the asynchronous service invocation -- check the network connection&quot;));&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public void onSuccess(String encodedResponse) {&lt;br /&gt;          try {&lt;br /&gt;            if(isReturnValue(encodedResponse)) {&lt;br /&gt;              tAsyncCallback.onSuccess((T) responseReader.read(createStreamReader(encodedResponse)));&lt;br /&gt;            }&lt;br /&gt;            else if(isThrownException(encodedResponse)) {&lt;br /&gt;              tAsyncCallback.onFailure((Throwable)responseReader.read(createStreamReader(encodedResponse)));&lt;br /&gt;              &lt;br /&gt;            }&lt;br /&gt;            else {&lt;br /&gt;              tAsyncCallback.onFailure(new InvocationException(&quot;Unknown return value type&quot;));&lt;br /&gt;            }&lt;br /&gt;          } catch (SerializationException e) {&lt;br /&gt;            tAsyncCallback.onFailure(new InvocationException(&quot;Failure deserializing object &quot;+e));&lt;br /&gt;          }&lt;br /&gt;        }&lt;br /&gt;      });&lt;br /&gt;    } catch (Exception ex) {&lt;br /&gt;      InvocationException iex = new InvocationException(&lt;br /&gt;          &quot;Unable to initiate the asynchronous service invocation -- check the network connection&quot;,&lt;br /&gt;          ex);&lt;br /&gt;      tAsyncCallback.onFailure(iex);&lt;br /&gt;    } finally {&lt;br /&gt;      if (RemoteServiceProxy.isStatsAvailable()&lt;br /&gt;          &amp;&amp; RemoteServiceProxy.stats(RemoteServiceProxy.bytesStat(methodName,&lt;br /&gt;          invocationCount, requestData.length(), &quot;requestSent&quot;))) {&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;    return null;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  private native void makeRequest(String serviceEntryPoint, String contentType,&lt;br /&gt;      String requestData, AsyncCallback tAsyncCallback) /*-{&lt;br /&gt;      var params = {};&lt;br /&gt;      params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;&lt;br /&gt;      &lt;br /&gt;      params[gadgets.io.RequestParameters.AUTHORIZATION]=gadgets.io.AuthorizationType.SIGNED;&lt;br /&gt;      params[gadgets.io.RequestParameters.METHOD]=gadgets.io.MethodType.GET;&lt;br /&gt;      &lt;br /&gt;      gadgets.io.makeRequest(serviceEntryPoint+&quot;?&quot;+@org.timepedia.uberrpc.client.UberRpcServiceProxy::RPC_PAYLOAD_PARAM+&quot;=&quot;+encodeURIComponent(requestData), function(resp) { &lt;br /&gt;          if(resp.errors &amp;&amp; resp.errors.length &gt; 0) {&lt;br /&gt;            tAsyncCallback.@com.google.gwt.user.client.rpc.AsyncCallback::onFailure(Ljava/lang/Throwable;)(null)&lt;br /&gt;          }&lt;br /&gt;          else {&lt;br /&gt;          tAsyncCallback.@com.google.gwt.user.client.rpc.AsyncCallback::onSuccess(Ljava/lang/Object;)(resp.text);&lt;br /&gt;          }&lt;br /&gt;      }, params);&lt;br /&gt;  }-*/;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h2&gt;Step 4: Modify RemoteServiceServlet&lt;/h2&gt;&lt;br /&gt;The last step is to modify RemoteServiceServlet so that it understands the new transport formats you&#39;ve devised. Here&#39;s an example of one that would handle the incoming OpenSocial makeRequest(). This one handles GET or POST requests with the incoming payload as a form parameter.&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush:java&quot;&gt;&lt;br /&gt;public class GadgetServiceServlet extends RemoteServiceServlet {&lt;br /&gt;&lt;br /&gt;  @Override&lt;br /&gt;  protected void doGet(HttpServletRequest httpServletRequest,&lt;br /&gt;      HttpServletResponse httpServletResponse)&lt;br /&gt;      throws ServletException, IOException {&lt;br /&gt;    doPost(httpServletRequest, httpServletResponse);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  @Override&lt;br /&gt;  protected String readContent(HttpServletRequest httpServletRequest)&lt;br /&gt;      throws ServletException, IOException {&lt;br /&gt;    String str = httpServletRequest.getMethod().equals(&quot;POST&quot;) ? RPCServletUtils&lt;br /&gt;        .readContentAsUtf8(httpServletRequest, false) : httpServletRequest&lt;br /&gt;        .getParameter(RpcServiceProxy.RPC_PAYLOAD_PARAM);&lt;br /&gt;    String ustr = URLDecoder.decode(str);&lt;br /&gt;    return ustr;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;To use, you&#39;d just subclass this servlet.&lt;br /&gt;&lt;h2&gt;The Sky&#39;s the Limit&lt;/h2&gt;&lt;br /&gt;Once you get to this point, you can start imagining all of the cool things you can do. Cross-domain POSTs using the &lt;a href=&quot;http://development.lombardi.com/?p=611&quot;&gt;window.name&lt;/a&gt; trick. OAuth signed RPC requests, verified in the servlet, done by OpenSocial containers. FaceBook integration. RPC over JSON, Protocol Buffers, Thrift. I&#39;m trying to cobble together some of this stuff for a future UberRPC module, but due to talks I&#39;m giving at the upcoming Google I/O, I&#39;m a little too swamped to make them release worthy at this point.&lt;br /&gt;&lt;br /&gt;Much credit goes to &lt;b&gt;Alex Epshteyn&lt;/b&gt; for his original proposal on the GWT Contributors list, which I picked up (over an inferior method I had been using to make Gadgets work), and integrated into a more graceful override of the default RPC behavior.&lt;br /&gt;&lt;br /&gt;If you&#39;re going to Google I/O, I will be doing two talks this year. One on Progressive Enhancement using GWT and a new library I&#39;ve written, and a second on Building an Application on Google&#39;s Open Stack, which is a walkthrough of a sophisticated GWT app which leverages about a dozen Google APIs.&lt;br /&gt;&lt;br /&gt;-Ray</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/7023805243463162600/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/7023805243463162600' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/7023805243463162600'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/7023805243463162600'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2009/04/gwt-rpc-over-arbitrary-transports-uber.html' title='GWT RPC over arbitrary transports: Uber RPC'/><author><name>Ray Cromwell</name><uri>http://www.blogger.com/profile/05070235196940096193</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-1759163326631528806</id><published>2009-04-07T19:43:00.000-07:00</published><updated>2009-04-07T19:45:33.652-07:00</updated><title type='text'>Small update to GWT Exporter</title><content type='html'>I recently discovered a pretty critical bug in GWT Exporter that can cause an infinite loop doing export. This was fixed in version 2.06 which available in the trunk and maven repository.&lt;br /&gt;&lt;br /&gt;-Ray</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/1759163326631528806/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/1759163326631528806' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/1759163326631528806'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/1759163326631528806'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2009/04/small-update-to-gwt-exporter.html' title='Small update to GWT Exporter'/><author><name>Ray Cromwell</name><uri>http://www.blogger.com/profile/05070235196940096193</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-309724405496464279</id><published>2009-04-07T17:54:00.000-07:00</published><updated>2009-04-07T23:07:45.440-07:00</updated><title type='text'>Google AppEngine and GWT now a marriage made in heaven</title><content type='html'>The announcement that Google AppEngine now supports Java is incredible news. Not just because it opens the doors to running arbitrary JVM languages like Scala, JRuby, PHP, etc on AppEngine, but because of the ability to wire up Java on the client, and Java on the server, through Google Web Toolkit. You can use all of your familiar Java tools for editing, debugging, testing, and packaging. &lt;br /&gt;&lt;br /&gt;With the new system, you can write a POJO, add JPA or JDO annotations, and write server-side logic to persist these POJOs in either a RDBMS like MySQL, or in BigTable/AppEngine. Moreover, you can export your DAO or logic interfaces through GWT RPC, and call them directly from the client, seamlessly, and painlessly.&lt;br /&gt;&lt;h2&gt;Almost Painlessly&lt;/h2&gt;&lt;br /&gt;The one hitch you&#39;ll encounter as a GWT developer is trying to serialize or deserialize persistence capable types. This is nothing new for GWT developers who have tried this with Hibernate before, and there are workarounds such as Hibernate4GWT. This problem occurs because the persistence classes are enhanced with an extra field to hold state which enables them to work when detached from the persistence context.  GWT RPC computes its own CRC based on the fields of a class in order to ensure compatibility between server and client and the extra field causes problems.&lt;br /&gt;&lt;br /&gt;In general, when it comes to sending serialized ORM POJOs down the wire, I think it&#39;s a risky practice, because you&#39;re likely to pull in a lot more of the reachable object tree than you bargained for unless you&#39;re careful. A better approach might be to use DTOs based on ProtocolBuffers.&lt;br /&gt;&lt;br /&gt;However, it is sometimes nice to do it if your POJOs are relatively flat and you want to rapidly prototype. If your insist on using your ORM&#39;ed POJOs over RPC, there is a trick to making it work.&lt;br /&gt;&lt;h2&gt;Making JDO/JPA enhanced classes work over GWT RPC&lt;/h2&gt;&lt;br /&gt;The first step to making things work is to disable detachable objects and tag your class as serializable.&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = &quot;false&quot;)&lt;br /&gt;public class MyPojo implements Serializable {&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This does two things. First, it tells the persistence engine that you&#39;ll be managing object identity, usually through a primary key, and secondly, when your POJO is accessed outside of a transaction/session, you want it to be &lt;b&gt;transient&lt;/b&gt; not &lt;b&gt;detached&lt;/b&gt;. A detached object remembers where it came from, so that after modifications, it can be reattached  and merged back into the datastore. A transient object forgets that it once came from the datastore, and thus if you try to repersist it, you&#39;ll end up inserting a copy.&lt;br /&gt;&lt;br /&gt;This has a major downside in terms of ease of use, but it does prevent the enhancer from injecting hidden fields into your class to manage detached state, and it is these hidden fields which break GWT RPC compatibility.&lt;br /&gt;&lt;h2&gt;But I don&#39;t want copies!&lt;/h2&gt;&lt;br /&gt;In using transient objects, you&#39;ll break a desired design pattern, which is to fetch an object through RPC, modify its properties, and send the same object back through RPC to be merged into the datastore. A quick and dirty work around is to use reflection to lookup an attached object on the server, copy all of the persistent fields from the transient object, and then merge the persistent object. Here&#39;s a prototype class that does this (but not recursively, so it doesn&#39;t handle anything but primitive fields):&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;public class PersistenceHelper {&lt;br /&gt;  public static &lt;T&gt; Object findPrimaryKey(T tInstance) {&lt;br /&gt;    if (tInstance == null) {&lt;br /&gt;      return null;&lt;br /&gt;    }&lt;br /&gt;    for (Field l : tInstance.getClass().getDeclaredFields()) {&lt;br /&gt;      if (l.getAnnotation(PrimaryKey.class) != null&lt;br /&gt;          || l.getAnnotation(Id.class) != null) {&lt;br /&gt;        l.setAccessible(true);&lt;br /&gt;        try {&lt;br /&gt;          return l.get(tInstance);&lt;br /&gt;        } catch (IllegalArgumentException e) {&lt;br /&gt;          e.printStackTrace();&lt;br /&gt;          return null;&lt;br /&gt;        } catch (IllegalAccessException e) {&lt;br /&gt;          e.printStackTrace();&lt;br /&gt;          return null;&lt;br /&gt;        }&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;    return new IllegalArgumentException(&lt;br /&gt;        &quot;Class &quot; + tInstance.getClass().getName()&lt;br /&gt;            + &quot; does not have a method called getId()&quot;);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public static &lt;T&gt; void copyPersistentFields(Object entity, T tInstance)&lt;br /&gt;      throws IllegalAccessException, NoSuchMethodException,&lt;br /&gt;      InvocationTargetException {&lt;br /&gt;    for (Method f : tInstance.getClass().getMethods()) {&lt;br /&gt;      if (f.getName().startsWith(&quot;set&quot;) &amp;&amp; Character&lt;br /&gt;          .isUpperCase(f.getName().charAt(3))) {&lt;br /&gt;       f.setAccessible(true);&lt;br /&gt;        Method getter = tInstance.getClass()&lt;br /&gt;            .getMethod(&quot;get&quot; + f.getName().substring(3));&lt;br /&gt;        getter.setAccessible(true);&lt;br /&gt;        f.invoke(entity, getter.invoke(tInstance));&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The way you&#39;d typically use this is as follows:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;public &lt;T&gt; T mergeTransient(T tInstance) {&lt;br /&gt;    EntityManager e = em.get();&lt;br /&gt;    if(e.contains(tInstance)) {&lt;br /&gt;      e.persist(tInstance);&lt;br /&gt;      return tInstance;&lt;br /&gt;    } else {&lt;br /&gt;      Object primaryKey = PersistenceHelper.findPrimaryKey(tInstance);&lt;br /&gt;      if(primaryKey != null) {&lt;br /&gt;        Object entity = e.find(tInstance.getClass(), primaryKey);&lt;br /&gt;        if(entity == null) {&lt;br /&gt;          e.persist(tInstance);&lt;br /&gt;          return tInstance;&lt;br /&gt;        }&lt;br /&gt;        else {&lt;br /&gt;          try {&lt;br /&gt;            PersistenceHelper.copyPersistentFields(entity, tInstance);&lt;br /&gt;          } catch (IllegalAccessException e1) {&lt;br /&gt;            e1.printStackTrace();&lt;br /&gt;            throw new IllegalArgumentException(&quot;Can&#39;t copy fields from transient class to persistent class.&quot;);&lt;br /&gt;          } catch (NoSuchMethodException e1) {&lt;br /&gt;            throw new IllegalArgumentException(&quot;Can&#39;t copy fields from transient class to persistent class.&quot;);&lt;br /&gt;          } catch (InvocationTargetException e1) {&lt;br /&gt;            throw new IllegalArgumentException(&quot;Can&#39;t copy fields from transient class to persistent class.&quot;);&lt;br /&gt;          }&lt;br /&gt;          e.persist(entity);&lt;br /&gt;          return (T) entity;&lt;br /&gt;        }&lt;br /&gt;      } else {&lt;br /&gt;        // primary key may be null, assume insert&lt;br /&gt;        e.persist(tInstance);&lt;br /&gt;        return tInstance;&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h2&gt;Less than ideal&lt;/h2&gt;&lt;br /&gt;After experimenting with this pattern, I&#39;ve come to the conclusion that although it works, I don&#39;t feel warm and cozy serializing instances out of the datastore, I like to have full control over what I&#39;m sending down to the client so I can optimize for size and speed. However, I don&#39;t want to write boilerplate sychronization code for DTOs. In a later article, I&#39;ll detail a pattern for using ProtocolBuffers with GWT and a DSL for terse/concise manipulation of them.&lt;br /&gt;&lt;h2&gt;It&#39;s still awesome&lt;/h2&gt;&lt;br /&gt;Even though there are some issue surrounding using RPC seamlessly with the Datastore ORM later, it completely trumps the time saved not having to do ANY configuration AT ALL to deploy an application. Words cannot describe how much of a time saver this is. No messing around with apt-get. No editing Apache configs. No Setting up log rotation and archiving. No dealing with backups. No bother with figuring out the right way to shard your db for your expected growth. No need to harden your own machines and firewalls. No need to provision anything but the application ID. &lt;br /&gt;&lt;br /&gt;To be sure, there are still things you can&#39;t do on AppEngine. I can&#39;t run JNI. I can&#39;t launch threads. I can&#39;t use Java2D/JAI/JavaSound. And probably most relevant, I can&#39;t host long-running comet sessions.  And if you really really need to do those, you can rent a server somewhere to do it.&lt;br /&gt;&lt;br /&gt;However, the majority of applications don&#39;t use these capabilies, and for these developers, AppEngine is an epic win.</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/309724405496464279/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/309724405496464279' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/309724405496464279'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/309724405496464279'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2009/04/google-appengine-and-gwt-now-marriage.html' title='Google AppEngine and GWT now a marriage made in heaven'/><author><name>Ray Cromwell</name><uri>http://www.blogger.com/profile/05070235196940096193</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-9197365961575606779</id><published>2009-04-02T15:28:00.001-07:00</published><updated>2009-04-02T16:30:59.648-07:00</updated><title type='text'>GWT&#39;s type system is more powerful than Java</title><content type='html'>This may come as a shock to some people. Isn&#39;t GWT just Java syntax you say? Yes, it is, and it does not extend the Java grammar in anyway. Yet, it is nonetheless true that GWT is more powerful than Java.&lt;br /&gt;&lt;h2&gt;The Overlay Type&lt;/h2&gt;&lt;br /&gt;The reason why GWT is more powerful is because it is actually a unification of two type systems, Java and Javascript. And while it seems that these are relatively walled off from one another, there is a bridge that unites the two, and that is the &lt;a href=&quot;http://code.google.com/p/google-web-toolkit/wiki/OverlayTypes&quot;&gt;GWT Overlay Type&lt;/a&gt; system.&lt;br /&gt;&lt;br /&gt;The overlay system in essence, permits the &#39;overlay&#39; or attachment of Java types to Javascript objects. And since you can pass Java objects into Javascript and back, this means you can in fact, overlay types on Java as well. &lt;br /&gt;&lt;h2&gt;Categories and Extension Methods&lt;/h2&gt;&lt;br /&gt;Some languages have a facility called &lt;a href=&quot;http://docs.codehaus.org/display/GROOVY/Groovy+Categories&quot;&gt;Categories&lt;/a&gt; or &lt;a href=&quot;http://en.wikipedia.org/wiki/Extension_method&quot;&gt;Extension Methods&lt;/a&gt;, modern examples include Objective-C, Groovy, and C#. A category allows you to pretend as if an existing reference implements additional methods, when it actually doesn&#39;t.&lt;br /&gt;&lt;br /&gt;In reality, they are syntax sugar for invocation of static utility functions. That is:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;SomeType x = new SomeType();&lt;br /&gt;SomeTypeUtils.doSomething(x);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;becomes&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;SomeType x = new SomeType();&lt;br /&gt;x.doSomething();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Transparently, behind the scenes, the compiler rewrites the invocation of &lt;code&gt;x.doSomething()&lt;/code&gt; into &lt;code&gt;SomeTypeUtils.doSomething(x)&lt;/code&gt;. GWT&#39;s JavaScriptObject overlays are essentially Categories, as the compiler transparently rewrites methods that appear to exist on an Overlay into static method calls on the underlying JavaScriptObject. This is one reason why JSO methods have to be effectively final, as there is no polymorphism allowed.&lt;br /&gt;&lt;br /&gt;This all sounds very interesting and theoretical, but what&#39;s the practical benefit?&lt;br /&gt;&lt;h2&gt;Type-Safe Enumerations with Zero Overhead&lt;/h2&gt;&lt;br /&gt;Let&#39;s say you want to write a method that can set the display property of an element to one of the legal values, and only the legal values. Traditional approaches would include using a Java enum and writing a setter method that accepts only this type:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;enum DisplayType {&lt;br /&gt;   BLOCK, INLINE, TABLE, NONE;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void setDisplay(Element e, DisplayType t) {&lt;br /&gt;   e.getStyle().setProperty(&quot;display&quot;, t.name().toLowerCase());&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Unfortunately, this will generate a lot of bloat, since each enum value is a class, the class must be initialized by a static initializer, and the ultimate CSS property value string has to be obtained through method calls that might not inline because of polymorphic dispatch.&lt;br /&gt;&lt;br /&gt;Think about what we want here. Don&#39;t we really just want to create a subclass of &lt;code&gt;java.lang.String&lt;/code&gt;, create a bunch of this String subclass constants, and write a method to accept that type? Unfortunately, java.lang.String is final. You can&#39;t subclass it in Java. &lt;br /&gt;&lt;br /&gt;&lt;b&gt;But you can in GWT, and I&#39;ll show you how!&lt;/b&gt;&lt;br /&gt;&lt;h2&gt;Turn a String into a JSO&lt;/h2&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;public class DisplayType extends JavaScriptObject {&lt;br /&gt;   protected DisplayType() {}&lt;br /&gt;   public native static DisplayType make(String str) /*-{&lt;br /&gt;      return str;&lt;br /&gt;   }-*/;&lt;br /&gt;&lt;br /&gt;   public native String value() /*-{&lt;br /&gt;      return this;&lt;br /&gt;   }-*/;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;For brevity, I left out the extra code to support Hosted Mode. In hosted mode, you must wrap the &#39;str&#39; argument in an array, e.g. &lt;code&gt;[str]&lt;/code&gt; and fetch it using &lt;code&gt;return this[0]&lt;/code&gt;, but that&#39;s not important. What this code is effectively doing is casting a &lt;code&gt;String&lt;/code&gt; into a &lt;code&gt;DisplayType&lt;/code&gt; and imposing an additional categorical method on this reference, which is &lt;code&gt;value()&lt;/code&gt;. Keep in mind, the &lt;code&gt;value()&lt;/code&gt; is never actually attached to the prototype of the underlying Javascript object.&lt;br /&gt;&lt;br /&gt;To use,&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;DisplayType NONE = DisplayType.make(&quot;none&quot;);&lt;br /&gt;DisplayType BLOCK = DisplayType.make(&quot;block&quot;);&lt;br /&gt;DisplayType INLINE = DisplayType.make(&quot;inline&quot;);&lt;br /&gt;DisplayType TABLE = DisplayType.make(&quot;TABLE&quot;);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now, what is the output of the GWT compiler when compiling &lt;code&gt;element.getStyle().setProperty(&quot;display&quot;, BLOCK.value())&lt;/code&gt;? Here it is:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: js&quot;&gt;&lt;br /&gt;element.style[&#39;display&#39;]=&#39;block&#39;;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;In fact, even calling the &lt;code&gt;setDisplay()&lt;/code&gt; method with various &lt;code&gt;DisplayType&lt;/code&gt; enums results in inlined assignments to the element.style property with no method calls!&lt;br /&gt;&lt;h2&gt;Adding methods to Numbers&lt;/h2&gt;&lt;br /&gt;In Groovy, Scala, and some languages, you can even add methods to primitive integers. Using GWT overlay types, you can even do this!&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt; public class Int extends JavaScriptObject {&lt;br /&gt;    protected Int() {}&lt;br /&gt;    public static native Int make(int x) /*-{&lt;br /&gt;      return x;&lt;br /&gt;    }-*/;&lt;br /&gt;    &lt;br /&gt;    final public native int value() /*-{&lt;br /&gt;      return this;&lt;br /&gt;    }-*/;&lt;br /&gt;    &lt;br /&gt;    final public Int square() {&lt;br /&gt;      return make(value() * value());&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt; int val = (int) Duration.currentTimeMillis();&lt;br /&gt; Int x = Int.make(val);&lt;br /&gt; Int sq = x.square();&lt;br /&gt; Window.alert(String.valueOf(sq.value()));&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;And what do you think the generated code looks like? Try this:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: js&quot;&gt;&lt;br /&gt;val = (new Date()).getTime();&lt;br /&gt;x = val;&lt;br /&gt;sq = x * x;&lt;br /&gt;$wnd.alert(&#39;&#39; + sq);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;There you have it. I successfully added a method to a primitive &#39;int&#39; called &lt;code&gt;square()&lt;/code&gt; without using a wrapper, and with no overhead whatsoever.  This opens the doors to implementing primitive &#39;wrappers&#39; for GWT for int, double, float, etc, that are not wrappers at all and have no overhead, which would be very useful in many circumstances where Integer, Double, Float,  in java.lang are too heavy.&lt;br /&gt;&lt;br /&gt;So, can we conclude from this that GWT is more powerful than Java? ;-)&lt;br /&gt;&lt;br /&gt;-Ray</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/9197365961575606779/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/9197365961575606779' title='14 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/9197365961575606779'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/9197365961575606779'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2009/04/gwts-type-system-is-more-powerful-than.html' title='GWT&#39;s type system is more powerful than Java'/><author><name>Ray Cromwell</name><uri>http://www.blogger.com/profile/05070235196940096193</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>14</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-3164273180936690104</id><published>2009-03-12T12:46:00.000-07:00</published><updated>2009-03-12T13:00:32.255-07:00</updated><title type='text'>Relaxing constraints on GWT.create()</title><content type='html'>Recently I&#39;ve been playing around with some GWT ideas that really cry out for a more liberal deferred binding system. Currently, GWT imposes the restriction that deferred binding can only happen through the GWT.create() method.  There&#39;s a couple of problems with this:&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Can&#39;t narrow type signature for custom library&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Can&#39;t create a method to decorate the returned result&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Can&#39;t parameterized or override the bindings at callee site&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;&lt;br /&gt;To illustrate this, I&#39;ve compiled some motivation examples.&lt;br /&gt;&lt;h1&gt;RPC requests signed by OAuth&lt;/h1&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;interface MyService extends RemoteService&amp;lt;MyServiceAsync&gt; { ... }&lt;br /&gt;// example call&lt;br /&gt;OAuth.withSignature(MyService.class).method1(arg1, arg2, asyncCallback);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;public class OAuth {&lt;br /&gt;  @GwtCreate  // method must be static, class parameter must be final&lt;br /&gt;  public static &amp;lt;S, T extends RemoteService&amp;lt;S&gt;&gt; S withSignature(final Class&amp;lt;T&gt; service) {&lt;br /&gt;        // GWT.create with non-literal ONLY allowed &lt;br /&gt;        // if enclosing method is @GwtCreate and &lt;br /&gt;       // variable statically resolvable to literal param&lt;br /&gt;        S async = GWT.create(service);  &lt;br /&gt;        ((ServiceDefTarget)async).setRequestBuilderCallback(new OAuthRequestBuilderSigner());&lt;br /&gt;        return async;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Currently today, it would look like this:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;MyServiceAsync foo = GWT.create(MyService.class);&lt;br /&gt;RequestBuilder rb = foo.method1(arg1, arg2, asyncCallback);&lt;br /&gt;OAuthRequestBuilderSigner.sign(rb);&lt;br /&gt;rb.send();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The tight coupling at the callsite also makes it difficult to swap out implementations easy (like using AuthSub, or one of the other 4 Google Friend Connect auth techniques).  An alternative is to make a separate subtype of each with a custom generator, e.g.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;MyServiceOAuth extends MyService, GWT.create(MyServiceOAuth.class)&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;I&#39;d argue that the above gets cumbersome with multiple services and multiple authentication types. I&#39;ve taken some liberties above by adding a type parameter to RemoteService to make the return type statically resolvable, as well as allowing a global callback mechanism on ServiceDefTarget for RequestBuilder override. Note that type-safety is ensured, one can&#39;t call this method with something that is not a RemoteService, and it will be flagged at &lt;b&gt;edit-time&lt;/b&gt; in your IDE.&lt;br /&gt;&lt;h1&gt;An EasyMock library for Hosted/Web Mode&lt;/h1&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;Subscriber mock = GMock.mock(Subscriber.class);&lt;br /&gt;publisher.add(subscriber);&lt;br /&gt;//...&lt;br /&gt;GMock.replay(mock);&lt;br /&gt;&lt;br /&gt;public class GMock {&lt;br /&gt;  // selectively override module binding rules ONLY for this method&lt;br /&gt;  @GwtCreate(generator=com.gmock.rebind.GMockGenerator.class)&lt;br /&gt;  public static &amp;lt;T&gt; T mock(Class&amp;lt;T&gt; toMock) {&lt;br /&gt;        return GWT.create(toMock);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In this method, the &lt;code&gt;mock()&lt;/code&gt; method acts like &lt;code&gt;GWT.create()&lt;/code&gt; except that it overrides the current binding rules, forcing the specified generator.&lt;br /&gt;&lt;h1&gt;GWT Exporter&lt;/h1&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;Exporter.export(Foo.class);&lt;br /&gt;&lt;br /&gt;public class Exporter {&lt;br /&gt;   @GwtCreate&lt;br /&gt;   public static &amp;lt;T extends Exportable&gt; void export(Class&amp;lt;T&gt; exportable) {&lt;br /&gt;       ExporterImpl ximpl = GWT.create(exportable);&lt;br /&gt;       ximpl.export();&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Note, the type safety, one can&#39;t try to Export a non-exportable class. This will be flagged at edit time in the IDE.&lt;br /&gt;&lt;h1&gt;GIN/Guice dependency injection&lt;/h1&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;Processor pimpl = Gin.inject(Processor.class, TestProcessorModule.class);&lt;br /&gt;&lt;br /&gt;public class Gin {&lt;br /&gt;  @GwtCreate(generator = com.google.gin.rebind.GInjectorGenerator)&lt;br /&gt;  public static &amp;lt;T, S extends AbstractGinModule&gt; &lt;br /&gt;      T inject(Class&amp;lt;T&gt; interf, @GwtCreateParam Class&amp;lt;S&gt; module) {&lt;br /&gt;         return GWT.create(interf, module);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This one is more controversial, but allows GWT.create() to be parameterized by literal metadata that is available to the generator. This is semantically equivalent to what we have today:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;@GinModule(TestProcessorModule.class)&lt;br /&gt;interface MyTestInjector extends GInjector {&lt;br /&gt;   Processor getProcessor();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;but without the need to actually write the interface.&lt;br /&gt;&lt;h1&gt;Combing compile-time and run-time parameters&lt;/h1&gt;&lt;br /&gt;Final example, mix-and-match both compile-time variables and run-time variables:&lt;br /&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;OAuth.withSignature(MyService.class, debugMode ? &quot;/debugService&quot; : &quot;/MyService&quot;).method1(arg1, arg2, asyncCallback);&lt;br /&gt;&lt;br /&gt;public class OAuth {&lt;br /&gt;  @GwtCreate  // method must be static, class parameter must be final&lt;br /&gt;  public static &amp;lt;S, T extends RemoteService&amp;lt;S&gt;&gt; &lt;br /&gt;        S withSignature(final Class&amp;lt;T&gt; service, String endPoint) {&lt;br /&gt;        // GWT.create with non-literal ONLY allowed if enclosing method is &lt;br /&gt;        // @GwtCreate and variable statically resolvable to literal param&lt;br /&gt;        S async = GWT.create(service);  &lt;br /&gt;        ((ServiceDefTarget)async).setRequestBuilderCallback(new OAuthRequestBuilderSigner());&lt;br /&gt;        ((ServiceDefTarget)async).setServiceEntryPoint(endPoint);&lt;br /&gt;        return async;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/3164273180936690104/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/3164273180936690104' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/3164273180936690104'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/3164273180936690104'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2009/03/relaxing-constraints-on-gwtcreate.html' title='Relaxing constraints on GWT.create()'/><author><name>Ray Cromwell</name><uri>http://www.blogger.com/profile/05070235196940096193</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-8367385920916423474</id><published>2009-03-06T15:34:00.000-08:00</published><updated>2009-03-06T15:36:51.932-08:00</updated><title type='text'>Spring cleaning and the blog</title><content type='html'>To try and improve readability and reach, I made a few changes today:&lt;br /&gt;&lt;br /&gt;1) Re-enabled SyntaxHighlighter for code snippets&lt;br /&gt;2) Widened the width of the main content to 600px (for code blocks)&lt;br /&gt;3) removed Twitter and added Google Friend Connect&lt;br /&gt;4) enabled CAPTCHA to reduce comment span&lt;br /&gt;5) tweak some colors so the subsections are easier to read.&lt;br /&gt;&lt;br /&gt;If you have any other advice for improving the Look and Feel, leave a comment.&lt;br /&gt;&lt;br /&gt;-Ray</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/8367385920916423474/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/8367385920916423474' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/8367385920916423474'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/8367385920916423474'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2009/03/spring-cleaning-and-blog.html' title='Spring cleaning and the blog'/><author><name>Ray Cromwell</name><uri>http://www.blogger.com/profile/05070235196940096193</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-596031860531424032</id><published>2009-03-05T14:21:00.000-08:00</published><updated>2009-03-06T15:22:18.507-08:00</updated><title type='text'>Structural Typing for GWT and Javascript</title><content type='html'>In a &lt;a href=&quot;http://timepedia.blogspot.com/2009/03/whats-next-for-gwt-exporter.html&quot;&gt;previous post&lt;/a&gt; I described a type of impedance mismatch between Javascript and Java idioms that makes the GWT Exporter still less than ideal for supporting Javascript programmers. A short illustrative (but contrived) example:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;public class Customer implements Exportable {&lt;br /&gt;   private String firstName, lastName;&lt;br /&gt;   @Export String getFirstName() { &lt;br /&gt;      return firstName; &lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;   @Export void setFirstName(String fn) {&lt;br /&gt;      firstName = fn;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   @Export String getLastName() { &lt;br /&gt;      return lastName; &lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;   @Export void setLastName(String ln) {&lt;br /&gt;      lastName = fn;&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Today, you may use this export in Javascript like this:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: js&quot;&gt;&lt;br /&gt;var cust = new Customer();&lt;br /&gt;cust.setFirstName(&quot;Ray&quot;);&lt;br /&gt;cust.setLastName(&quot;Cromwell&quot;);&lt;br /&gt;Processor.doSomething(cust);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The example is contrived because you could use a constructor, but with more complex objects with nested types, you wouldn&#39;t use a constructor, but either a builder pattern, or inject the types after construction. &lt;br /&gt;&lt;br /&gt;Javascript developers however don&#39;t work in the world of Javabean interfaces, they prefer easy construction of configuration/builder information via object literals:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: js&quot;&gt;&lt;br /&gt;Processor.doSomething({firstName: &quot;Ray&quot;, lastName: &quot;Cromwell&quot;})&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Moreover, when passing in say, a bind of event callbacks, they&#39;d prefer to write:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: js&quot;&gt;&lt;br /&gt;foo.addEvents({&lt;br /&gt;    click: function(e) { ... }&lt;br /&gt;    move: function(e) { ... }&lt;br /&gt;    drop: function(e) { ... }&lt;br /&gt;});&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The challenge is to seemlessly bridge this idiomatic mismatch between JS and Java GWT code without actually having to write much bridging code or adapters.&lt;br /&gt;&lt;h2&gt;Structural Typing&lt;/h2&gt;&lt;br /&gt;Java is a &lt;a href=&quot;http://en.wikipedia.org/wiki/Manifest_typing&quot;&gt;manifestly typed language&lt;/a&gt;. All types have to be declared, and type checking is done by explicit hierarchy. Javascript is a dynamically typed language, with essentially no type checking at all. Orthogonal to this is the concept of &lt;a href=&quot;http://en.wikipedia.org/wiki/Structural_type_system&quot;&gt;Structural Typing&lt;/a&gt;. Haskell, ML, and Scala are all examples of languages which support structural typing. Structural typing was also planned for Javascript 2 before it got killed.&lt;br /&gt;&lt;br /&gt;So what&#39;s a structural type? Recall the &lt;code&gt;Customer&lt;/code&gt; example from the previous section. It was a class with two fields, firstName, and lastName, both Strings. If Java supported a structural type system, I could declare a method in two ways:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;public void process(Customer cust) { ... }&lt;br /&gt;&lt;br /&gt;public void process({firstName: String, lastName: String} cust) { ... } &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;In this invented syntax, the &lt;code&gt;cust&lt;/code&gt; parameter to the second &lt;code&gt;process()&lt;/code&gt; function is an anonymous type, we don&#39;t know its real name. However, we are stating that as long as it consists of two fields named &lt;code&gt;&quot;firstName&quot;&lt;/code&gt;, and &lt;code&gt;&quot;lastName&quot;&lt;/code&gt;, and the types are both &lt;code&gt;String&lt;/code&gt;s, then we can access these fields and treat it like a Customer (although it may not be one)&lt;br /&gt;&lt;br /&gt;Hmm....I smell an idea...&lt;br /&gt;&lt;h2&gt;Structural Type Exports&lt;/h2&gt;&lt;br /&gt;What if I rewrite the Customer POJO class with a &lt;code&gt;@StructuralType&lt;/code&gt; annotation:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;@Export&lt;br /&gt;@StructuralType&lt;br /&gt;public class Customer implements Exportable { ... }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The dispatch code for an exported &lt;code&gt;process()&lt;/code&gt; function could then look like this:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: js&quot;&gt;&lt;br /&gt;$wnd.Processor.prototype.process = function(cust) {&lt;br /&gt;  if(cust instanceof $wnd.Customer &amp;&amp; isExportedInstance(cust)) {&lt;br /&gt;    // JSNI dispatch to @Processor::process(LCustomer;)(unwrap(cust));&lt;br /&gt;   }&lt;br /&gt;  else if(typeof(cust) == &#39;object&#39;) {&lt;br /&gt;      // cust is not an instance of the Customer POJO, but an object&lt;br /&gt;      if(cust.firstName != undefined &amp;&amp; cust.lastName != undefined) {&lt;br /&gt;          var scust = new Customer();&lt;br /&gt;          scust.setFirstName(cust.firstName);&lt;br /&gt;          scust.setLastName(cust.lastName); &lt;br /&gt;          @Processor::process(unwrap(scust));&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The GWT compiler would auto-inject this structural type check and initialization code simply by annotating a parameter or return type involved with &lt;code&gt;@StructuralType&lt;/code&gt;&lt;br /&gt;&lt;h2&gt;Refinements to the idea&lt;/h2&gt;&lt;br /&gt;Taking this a step further, one could override the expected type literal field names to be checked&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;@SType(&quot;fn&quot;)&lt;br /&gt;public void setFirstName(String firstName) { ... }&lt;br /&gt;@SType(&quot;ln&quot;)&lt;br /&gt;public void setLastName(String lastName) { ... }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;which would allow the object literal to be specified as &lt;code&gt;{fn: &quot;Ray&quot;,ln: &quot;Cromwell&quot;}&lt;/code&gt;. Another extension would allow partial matches to succeed with default values supplied:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;@Optional(&quot;Ray&quot;)&lt;br /&gt;public void setFirstName(String firstName) { ... }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;which would reduce the structural type to just a &lt;code&gt;lastName&lt;/code&gt; field for matching purposes, but would allow the specification of &lt;code&gt;firstName&lt;/code&gt; to be supplied and injected into the setter as &quot;Ray&quot; if it wasn&#39;t present.&lt;br /&gt;&lt;h2&gt;Structurally Typed Interfaces&lt;/h2&gt;&lt;br /&gt;GWT Exporter already supports closure conversion for single-method Java interfaces. That is, if the following interface:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;public interface MyCallback {&lt;br /&gt;     public void go(Object arg);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;occurs as a type parameter in an exported method, the Javascript developer may supply &lt;code&gt;function(arg) { ... }&lt;/code&gt; and the GWT generated bridge code will automatically convert this into a GWT object instance that implements the &lt;code&gt;MyCallback&lt;/code&gt; interface type. What it cannot support is the example given earlier:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: js&quot;&gt;&lt;br /&gt;foo.addEvents({&lt;br /&gt;    click: function(e) { ... }&lt;br /&gt;    move: function(e) { ... }&lt;br /&gt;    drop: function(e) { ... }&lt;br /&gt;});&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;which would represent something like:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;public interface MyEventHandler {&lt;br /&gt;    public void onClick(Event e);&lt;br /&gt;    public void onMove(Event e);&lt;br /&gt;    public void onDrop(Event e);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;However, using structural typing conventions, and an &lt;code&gt;@StructuralType&lt;/code&gt; annotation on an interface, we can auto-convert object literals containing multiple function closures into a Java interface for GWT.&lt;br /&gt;&lt;br /&gt;I am still working out the details of the full overload resolution algorithm for overloaded function types, but some version of this proposal will make it into GWT Exporter 3.0.&lt;br /&gt;&lt;br /&gt;-Ray</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/596031860531424032/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/596031860531424032' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/596031860531424032'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/596031860531424032'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2009/03/structural-typing-for-gwt-and.html' title='Structural Typing for GWT and Javascript'/><author><name>Ray Cromwell</name><uri>http://www.blogger.com/profile/05070235196940096193</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-873046768822963700</id><published>2009-03-05T14:09:00.000-08:00</published><updated>2009-03-06T15:29:37.152-08:00</updated><title type='text'>GWT Exporter 2.05 released</title><content type='html'>The release adds two new features:&lt;br /&gt;&lt;br /&gt;1) arrays are return types or parameters supported, that is:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;@Export public double[] getFoo() { }&lt;br /&gt;@Export public SomeExportable[] getBar() {}&lt;br /&gt;@Export public void setFoo(double[] foo) { }&lt;br /&gt;@Export public void setBar(SomeExportable[] bar) { }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;now works, in both Web mode and Hosted Mode. &lt;br /&gt;&lt;br /&gt;2) Export Overlays implemented. This means you can now Export classes you don&#39;t own.&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;@ExportPackage(&quot;gwt&quot;)&lt;br /&gt;public interface WindowExport extends ExportOverlay&amp;lt;Window&gt; {&lt;br /&gt;   @Export void alert(String s);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;will export the GWT Window class into the namespace $wnd.gwt.Window, and export the alert() method of this class.&lt;br /&gt;&lt;br /&gt;The JARs are downloadable from: &lt;a href=&quot;http://timefire-repository.googlecode.com/svn/mavenrepo/org/timepedia/exporter/gwtexporter/2.05/&quot;&gt;http://timefire-repository.googlecode.com/svn/mavenrepo/org/timepedia/exporter/gwtexporter/2.05/&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/873046768822963700/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/873046768822963700' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/873046768822963700'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/873046768822963700'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2009/03/gwt-exporter-205-released.html' title='GWT Exporter 2.05 released'/><author><name>Ray Cromwell</name><uri>http://www.blogger.com/profile/05070235196940096193</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-5669698379430808446</id><published>2009-03-03T14:26:00.000-08:00</published><updated>2009-03-06T15:30:44.264-08:00</updated><title type='text'>What&#39;s next for GWT Exporter?</title><content type='html'>I have learned over the past year that a lot of Chronoscope users like to use the Javascript API, but the current API is unfortunately, a small sliver of the full power the GWT API offers. In dealing with increasing demand for more exported functions, I have run into some basic limitations that GWT-Exporter has that make it less than ideal for exporting idiomatic Javascript APIs.&lt;br /&gt;&lt;h2&gt;Exporting Classes you don&#39;t control&lt;/h2&gt;&lt;br /&gt;Since Chronoscope made the switch to the new GWT Incubator/GWT 1.6 Event handling system, I have been unable to export event handler functions, because the &lt;code&gt;HandlerRegistration&lt;/code&gt; type returned by the GWT event system is not an interface, making it difficult to wrap.  In general, if you don&#39;t own a class, you can&#39;t add Exporter annotations to it, which is problematic because there are lots of types you don&#39;t control.&lt;br /&gt;&lt;br /&gt;A new mechanism will be added in an upcoming GWT Exporter release, I dub Export Overlays, which will work something like this:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;@ExportPackage(&quot;gwt&quot;)&lt;br /&gt;public interface WindowExport extends ExportOverlay&amp;lt;Window&gt; {&lt;br /&gt;    @Export void alert(String message);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;In the above code, we are exporting the GWT Window class in the Javascript package &quot;gwt&quot;, but only exposing the alert() method. This will result in an exported wrapper in $wnd.gwt.Window with  the method published on $wnd.gwt.Window.prototype.alert.&lt;br /&gt;&lt;h2&gt;Fine control over Javascript-&gt;Java type conversions&lt;/h2&gt;&lt;br /&gt;Javascript doesn&#39;t support overloaded method dispatch as a language feature, but most Javascript libraries do use heavily overloaded functions with variable arguments and variable argument types. Currently, GWT Exporter supports Javascript overloading by arity, but not by type, so two Java methods of the same name and identical arity cannot be exported without overriding the exported name.&lt;br /&gt;&lt;br /&gt;Some of this will be mitigated by adding in automatic type checking for handling overloaded dispatch, but more is needed. Consider the following Javascript API:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: js&quot;&gt;&lt;br /&gt;plot.addHandler({&lt;br /&gt;    onMove: function(event) {  }&lt;br /&gt;    onHover: function(event) { }&lt;br /&gt;    onFocus: function(event) { }&lt;br /&gt;});&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This is somewhat idiomatic Javascript, leveraging object literals to pass in multiple bindings. The current approach is to have 3 methods, addMoveHander, addHoverHandler, addFocusHandler, but this doesn&#39;t feel quite right to some Javascript programmers. Another problem is illustrated below:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;@Export&lt;br /&gt;public class Foo implements Exportable {&lt;br /&gt;   public void setFoo(Date date) { }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;A Javascript developer would naturally want to write:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: js&quot;&gt;&lt;br /&gt;var date = new Date();&lt;br /&gt;foo.setFoo(date);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;But this won&#39;t work, because there is no automatic conversion in GWT Exporter between JS Date and java.util.Date. I could add this (as was done with JS String and java.lang.String), but that still leaves open the problem of the inability to pass Javascript types from third party Javascript libraries into GWT methods.&lt;br /&gt;&lt;h2&gt;Type Converter injection&lt;/h2&gt;&lt;br /&gt;What I&#39;m currently looking at is a mechanism to declare Javascript fragments on Java methods which can type-convert arguments between the exported wrapper, before they invoke the underlying GWT method. Something like this:&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;brush: java&quot;&gt;&lt;br /&gt;@Export&lt;br /&gt;public class Foo implements Exportable {&lt;br /&gt;  public void setFoo(@Convert(JsType.DATE, &quot;new java.util.Date($_arg.getTime()$)&quot;) Date foo) { }   &lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The syntax is still subject to change of course, but the idea is that you supply an annotation on each param, using an enum (STRING, FUNCTION, ARRAY, OBJECT, etc) which corresponds to the return value of the &#39;typeof&#39; operator, or you use a special INSTANCE version which allows you to specify a match according to &#39;instanceof&#39;.  Next, you supply a Java expression), including inline JSNI code (here shown in between $ $). The GWT Exporter then arranges to generate code to check the type, and inject your fragment before calling the underlying method.&lt;br /&gt;&lt;br /&gt;Another possibility is to add JSNI methods to Export Overlays which allow arbitrary conversion code to be specified and injected into the wrapper prior to invocation of methods.&lt;br /&gt;&lt;br /&gt;By combining this with the Export Overlay concept, it might be possible to deal with the idiomatic JS Literal pattern, while keeping JSNI methods out of classes that may not want them, or that you do not control.&lt;br /&gt;&lt;h2&gt;Primitive arrays an oversight&lt;/h2&gt;&lt;br /&gt;Finally, GWT Exporter currently does not support export of parameters or return types which are native Java arrays, which is a hole in the export type system. This will be added in a near future release.&lt;br /&gt;&lt;br /&gt;-Ray</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/5669698379430808446/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/5669698379430808446' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/5669698379430808446'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/5669698379430808446'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2009/03/whats-next-for-gwt-exporter.html' title='What&#39;s next for GWT Exporter?'/><author><name>Ray Cromwell</name><uri>http://www.blogger.com/profile/05070235196940096193</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-197960992041688837</id><published>2009-02-19T21:54:00.000-08:00</published><updated>2009-02-20T13:44:22.687-08:00</updated><title type='text'>Small demo of Timelord</title><content type='html'>Timefire is building a set of social collaboration and visualization tools to help people understand data better, and part of this has been the development of Chronoscope/Timescope, but an even larger part of this effort has been the development of an end user application that permits users to play with data, annotate it, and share their activity with friends.&lt;br /&gt;&lt;br /&gt;Timelord is the codename of our application, which runs as an embeddable widget, a Google Gadget, a Spreadsheet Gadget, or as a native Android application. It leverages OpenSocial when possible, to allow users to share activities, but also integrates with other contact sources, like GData Contacts API, Android address book, etc. &lt;br /&gt;&lt;br /&gt;One of the more important features of Timelord is the ability for users to create their own &quot;mini-presentations&quot; which script tours of the data. These mini-presentations are simply a special comment syntax, like Wiki-text or BBCode, to instruct the chart to perform actions which would normally be done programmatically, like adding markers, or zooming. &lt;br /&gt;&lt;br /&gt;A cool feature, I think, is the ability to do 2-way synchronization between these mini-presentations and YouTube video, which is demonstrated in the screencast below, courtesy of our friend Al Gore, and the YouTube Player API.&lt;br /&gt;&lt;br /&gt;Take it away, Mr Gore (best if watched in HD):&lt;br /&gt;&lt;object width=&quot;480&quot; height=&quot;295&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/s5Y21t0u_Zw&amp;hl=en&amp;fs=1&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/s5Y21t0u_Zw&amp;hl=en&amp;fs=1&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;480&quot; height=&quot;295&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;http://www.youtube.com/watch?v=s5Y21t0u_Zw&amp;fmt=22&quot;&gt;Watch it in HD here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;UPDATE: YouTube seems to have disappeared my HD version, so there is an HD version mirrored at &lt;a href=&quot;http://vimeo.com/3292149&quot;&gt;http://vimeo.com/3292149&lt;/a&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;I don&#39;t think I have to mention that this is all done in GWT? :)&lt;br /&gt;&lt;br /&gt;-Ray</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/197960992041688837/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/197960992041688837' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/197960992041688837'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/197960992041688837'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2009/02/small-demo-of-timelord.html' title='Small demo of Timelord'/><author><name>Ray Cromwell</name><uri>http://www.blogger.com/profile/05070235196940096193</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-1483347796611558081</id><published>2009-01-30T17:44:00.000-08:00</published><updated>2009-01-30T17:49:25.469-08:00</updated><title type='text'>GWT Exporter now generates Javascript documentation</title><content type='html'>I added a rough implementation of a Javadoc Doclet (my first!) to the exporter trunk. It is an extension of the Standard Doclet, so it not only generates Javadoc for your GWT classes, but automatically emits a file called &quot;jsdoc.html&quot; that includes documentation for every @Export in your source base. This allows you to automatically produce documentation for Javascript programmers from GWT source. I use the Maven Javadoc Plugin to execute this, but you can use ant tasks or the command line as well.&lt;br /&gt;&lt;br /&gt;It&#39;s quite ugly at the moment, no CSS stylesheet, but I&#39;m working on a basic one. I&#39;m happing to take any CSS donations from those who want to check out the trunk and play with it.&lt;br /&gt;&lt;br /&gt;Code project is : &lt;a href=&quot;http://code.google.com/p/gwt-exporter/&quot;&gt;http://code.google.com/p/gwt-exporter/&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;-Ray</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/1483347796611558081/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/1483347796611558081' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/1483347796611558081'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/1483347796611558081'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2009/01/gwt-exporter-now-generates-javascript.html' title='GWT Exporter now generates Javascript documentation'/><author><name>Ray Cromwell</name><uri>http://www.blogger.com/profile/05070235196940096193</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-6769550557501351145</id><published>2009-01-27T13:52:00.000-08:00</published><updated>2009-01-27T13:57:02.954-08:00</updated><title type='text'>Screencast of Timescope, the commercial version of Chronoscope</title><content type='html'>Hi Folks,&lt;br /&gt;  I&#39;ve uploaded a &lt;a href=&quot;http://vimeo.com/2983154&quot;&gt;screencast of Timescope&lt;/a&gt;, which is the commercial version of Chronoscope, to Vimeo. This is a sneak peak at some of the features in the upcoming release of Timescope, as well as Chronoscope 1.0. The chief differences between Timescope and Chronoscope, are a more accurate multiresolution algorithm that preserves data better when zooming out, new marker and renderer types (CandleStick charts for example), new marker types like Image, Axis, and Callout markers, iPhone support, a native Android version, as well as a Chart Server.&lt;br /&gt;&lt;br /&gt;The screencast is at: &lt;a href=&quot;http://vimeo.com/2983154&quot;&gt;http://vimeo.com/2983154&lt;/a&gt;, I&#39;d embed it here, but it has to be watched in HD in order to see what&#39;s going on.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;-Ray</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/6769550557501351145/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/6769550557501351145' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/6769550557501351145'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/6769550557501351145'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2009/01/screencast-of-timescope-commercial.html' title='Screencast of Timescope, the commercial version of Chronoscope'/><author><name>Ray Cromwell</name><uri>http://www.blogger.com/profile/05070235196940096193</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-1406274645105716</id><published>2008-12-09T21:57:00.000-08:00</published><updated>2008-12-09T22:52:13.120-08:00</updated><title type='text'>The OpenWeb language for the next 100 years is...</title><content type='html'>Recently, there&#39;s been a lot of discussion about frameworks which translate or compile alternate programming languages to Javascript, John Resig&#39;s article being one of the more recent. At the heart of John&#39;s article is a lament that by abstracting people away from Javascript, they may not choose to learn the language. &lt;br /&gt;&lt;br /&gt;There is some merit to this, after all, many developers use programming languages which compile down to assembly, but never take the time to learn the CPU architecture. I&#39;m guilty of such, I grew up in the 80s hacking 6502 (VIC20/C64)  and later 68k/80x86 code, but after my computer got powerful enough to compile C, I pretty much didn&#39;t keep up with ASM never learned MIPS, SPARC, or SSE (although I did take the time to learn DX9 shader assembly).&lt;br /&gt;&lt;br /&gt;John also mentions that sometimes abstractions leak, and when they do, it may be hard to debug problems if you aren&#39;t familiar with the native platform. This too is a fair point, of course, the same is true with assembly, in that sometimes you do need to step into assembler code when a heisenbug appears.&lt;br /&gt;&lt;br /&gt;One lesson to take away from this is that programmers should strive for deep knowledge of the tools they are using, from how compilers work, to library implementation, and underlying execution environment. Is it really good practice for a Java developer to learn nothing about the internals of the JVM, and how Hotspot optimizations work, for example?&lt;br /&gt;&lt;br /&gt;But I&#39;d like to ask a more important question: What if future web applications aren&#39;t written in Javascript at all, would that really be a bad thing? What makes Javascript ideally suited for client side coding, which really means, DOM/CSS manipulation, event handling, and network I/O? You see, much of the Javascript community accepts that it is perfectly natural for server-side code to be written in any language: Java, Python, Perl, Ruby, PHP, C#, even Javascript. You do not see a huge push for a singular server-side language.  And the server-side has since developed into a very strong, diverse ecosystem, sprouting up robust VMs everywhere. And from there, robust library frameworks, like RoR, Django, etc.&lt;br /&gt;&lt;br /&gt;So why is it, that only a single language: Javascript, and it&#39;s various VM implementations, is treated as if it is the only True and Correct way to work on the client side? If Javascript VMs one day became more general purpose, accepting perhaps bytecode or some form of intermediate representation as input, and we ended up with Ruby, Scheme, Python, et al, running natively in the browser, would this be the end of the world?&lt;br /&gt;&lt;br /&gt;To listen to some Javascript evangelists, you would think so. The OpenWeb seems to mean that you have freedom and openness on the server, just don&#39;t dare write any non-Javascript on the client side, that&#39;s off limits!&lt;br /&gt;&lt;br /&gt;Unfortunately, we don&#39;t have a general purpose VM in the browser, like JVM, or CLR, or Parrot. Instead, we have Javascript, which is why all of these third party language translators are treating Javascript as if it were assembly or bytecode. But oh, the nerve of these translators! What an insult! How dare you compile some inferior language  or DSL down to basic Javascript, and not write beautiful and elegant human readable Javascript instead. &lt;br /&gt;&lt;br /&gt;On a more serious note, there appears to be a great deal of hypocrisy in some of the criticisms of third party frameworks as well. That is, it is impermissible to abstract away the Javascript language or try to &quot;hide&quot; the Web, that is, unless you are a Javascript framework which has nifty DSLs and Widgets which do exactly that -- like hiding the pain of getting CSS layouts right from newbies. &lt;br /&gt;&lt;br /&gt;The question for you is, do you believe Javascript is the next big programming language, the language that we should be programming the web with, 5 years, 10 years,  100 years from now? What I&#39;ve learned from sitting through 20+ years of internet/usenet language flamewars is that no language is perfect, people have different tastes, different aesthetics, and different languages fill different niches.  Emacs vs VI will never be resolved. Static vs Dynamic typing will never be resolved. And no one language can satisfy everyone.&lt;br /&gt;&lt;br /&gt;This is not to say that there aren&#39;t concerns about gluing code written in multiple languages together and making them interoperate. This is being done on .NET CLR and JVM today, so it is certainly possible in the future to do it in the browser. On the other hand, many people today already work with incompatible siloed Javascript frameworks, so maybe it&#39;s not really an issue if Ruby code can&#39;t reuse Python code on the client.&lt;br /&gt;&lt;br /&gt;In the end, I would say to Javascript ninjas, relax. There will be more than enough people writing Javascript to sustain a vibrant community. You should welcome polyglots on the client as well as the server. &lt;br /&gt;&lt;br /&gt;Let a thousand flowers bloom.&lt;br /&gt;-Ray</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/1406274645105716/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/1406274645105716' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/1406274645105716'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/1406274645105716'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2008/12/openweb-language-for-next-100-years-is.html' title='The OpenWeb language for the next 100 years is...'/><author><name>Timepedia</name><uri>http://www.blogger.com/profile/01408958797259838599</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-2649349363775616955</id><published>2008-12-08T14:41:00.000-08:00</published><updated>2008-12-08T15:39:51.455-08:00</updated><title type='text'>Worst anti-GWT marketing ever</title><content type='html'>There seems to be an increase in other frameworks attacking GWT, which is probably a indicator that Google is doing something right, but some of these attacks are downright silly, consider &lt;a rel=&quot;nofollow&quot; href=&quot;http://jaxcent.com&quot;&gt;Jaxcent.com&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;Unlike GWT, Jaxcent is a server-side framework. Instead of being compiled into JavaScript, the Java code directly runs on the server, and communicates with the client via a small JavaScript file.&lt;br /&gt;&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;There may be some concern that a server-side framework may be putting more burden on the server, compared to GWT. However, a GWT-like approach does require the server to maintain, manage and deliver multiple JavaScript files. &lt;b&gt;In real terms, that can be a significant server load.&lt;/b&gt; &lt;b&gt;In contrast,&lt;/b&gt; Jaxcent has a single small JavaScript file, that will be cached by normal browsers. &lt;b&gt;The actual load on the server is comparable to any server side pre-AJAX framework, such as servlets, JSP, ASP etc.&lt;/b&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;Leaving aside the fact that it is ridiculous to assert that serving up static GWT Javascript files, which can be deployed to a CDN, typically are less than 150k compressed, and cacheable forever so that they are never fetched more than once, represents a signifcant server load, it is even more ridiculous to minimize the burden of keeping all UI state on the server, and making network around trips every time the user takes an action.&lt;br /&gt;&lt;br /&gt;Instead of a Rich Internet Application, you end up with a Dumb Terminal Client, that evokes the days of TN3270 and mainframes.</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/2649349363775616955/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/2649349363775616955' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/2649349363775616955'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/2649349363775616955'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2008/12/worst-anti-gwt-marketing-ever.html' title='Worst anti-GWT marketing ever'/><author><name>Timepedia</name><uri>http://www.blogger.com/profile/01408958797259838599</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-1102534323612869929</id><published>2008-11-19T15:41:00.000-08:00</published><updated>2008-11-19T15:51:19.336-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="chronoscope gwt visualization iphone gesture touch"/><title type='text'>Chronoscope on iPhone</title><content type='html'>If it hasn&#39;t been clear from my videos, I don&#39;t film them with a script or much forethought as to what I&#39;m going to say or demo, which is one reason why I forgot to show off the iPhone version of Chronoscope. This time, it runs as Javascript in the Safari browser, but leverages iPhone gesture and touch events.  But will it always run as Javascript, or is there another possibility? Time will reveal all secrets.&lt;br /&gt;&lt;br /&gt;&lt;object width=&quot;425&quot; height=&quot;344&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/M946ifp84Ls&amp;hl=en&amp;fs=1&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/M946ifp84Ls&amp;hl=en&amp;fs=1&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;425&quot; height=&quot;344&quot;&gt;&lt;/embed&gt;&lt;/object&gt;</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/1102534323612869929/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/1102534323612869929' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/1102534323612869929'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/1102534323612869929'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2008/11/chronoscope-on-iphone.html' title='Chronoscope on iPhone'/><author><name>Timepedia</name><uri>http://www.blogger.com/profile/01408958797259838599</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-4429722888346809376</id><published>2008-11-18T17:17:00.000-08:00</published><updated>2008-11-18T17:28:58.647-08:00</updated><title type='text'>Chronoscope and GViz on Android</title><content type='html'>It&#39;s been a while since I&#39;ve updated the blog, with good reason as we&#39;ve been very busy recently. However, I wanted to share with you the fruit of some of that labor.&lt;br /&gt;&lt;br /&gt;Long time readers will recall a prototype demo I posted a long time ago of Chronoscope running on Android. The original version was mainly a proof of concept that it was possible to reuse 98% of your code base and deploy nearly identical functionality both in browser, in the server, and on the phone.&lt;br /&gt;&lt;br /&gt;What hasn&#39;t been shown however, is the production version, rewritten, with improved performance. Now dubbed Timescope, the new version integrates tightly with Android, as well as our ChartServer, and Google&#39;s Visualization API, to allow you to generate charts that render dynamically in web browsers, but launch native high performance applications on the phone.&lt;br /&gt;&lt;br /&gt;Here&#39;s a video demonstrating a GViz chart, rendered  as a static clickable image from our chart server and viewed in the Android Web browser. Click on the image launches a native Android application which allows full interactivity.&lt;br /&gt;&lt;br /&gt;All of the full functionality of Chronoscope is retained, scalable to immense streaming datasets, annotatable via a variety of markers, a browser-like history mechanism, bookmarkable links, and a very flexible graph style sheet system.&lt;br /&gt;&lt;br /&gt;&lt;object width=&quot;425&quot; height=&quot;344&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/qJkiiVE68u4&amp;amp;hl=en&amp;amp;fs=1&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/qJkiiVE68u4&amp;amp;hl=en&amp;amp;fs=1&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;425&quot; height=&quot;344&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;&lt;br /&gt;I&#39;ll be posting a fairly substantial update soon on all of the cool stuff we&#39;ve been working on recently, which is nearing public release.&lt;br /&gt;&lt;br /&gt;-Ray</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/4429722888346809376/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/4429722888346809376' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/4429722888346809376'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/4429722888346809376'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2008/11/chronoscope-and-gviz-on-android.html' title='Chronoscope and GViz on Android'/><author><name>Timepedia</name><uri>http://www.blogger.com/profile/01408958797259838599</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-8919280034348592156</id><published>2008-09-04T13:20:00.000-07:00</published><updated>2008-09-04T13:31:38.165-07:00</updated><title type='text'>Interesting observations about WebKit/Chrome and Chronoscope Benchmark</title><content type='html'>Playing around with the benchmark some more, I noticed some strange things. The more I clicked the &quot;Bench&quot; button, the faster Chrome got. After about 4 runs of the benchmark, it increased as much as 2x in performance. Is Chrome using HotSpot-like techniques to count method invocations for inlining?&lt;br /&gt;&lt;br /&gt;Secondly, the animation on WebKit/Chrome gets choppy. This isn&#39;t because the Javascript isn&#39;t drawing the frames. The benchmark draws 100 frames, each in a setTimeout call to avoid slow script warnings. Browsers appear to defer reflecting any mutations to the UI until the Javascript thread yields the CPU back. That is, you don&#39;t see the results of a canvas fill() or stroke() until the CPU is yielded.  (Opera seems to work differently, but offers a GameCanvas which lets you lock/unlock progressive updates)&lt;br /&gt;&lt;br /&gt;Now, I would normally expect that the draw results would be copied/blitted/reflected into the Browser&#39;s document as soon as the Javascript thread yields. But what if the update is scheduled in the same timer/event queue as other pending setTimeout calls? And what if the next frame&#39;s scheduled timeout is before the native canvas update?&lt;br /&gt;&lt;br /&gt;Then what would happen is that the Javascript code would get the CPU again, erase the canvas, and draw the next frame. This would account for the choppiness and disappearance of intermediate frames.&lt;br /&gt;&lt;br /&gt;But it could also count for the performance differential between TraceMonkey/FF3.1 and WebKit/Chrome, in the sense that TraceMonkey might &quot;pay the cost&quot; of reflecting every frame into the browser&#39;s window which gets counted by my timing code, whereas WebKit/Chrome might get a free ride by skipping the update and somehow avoid the cost by deferring it.&lt;br /&gt;&lt;br /&gt;I&#39;m not sure, and it would be interesting if people who are familiar with the internals of Gecko and WebKit could comment on how canvas updates are handled.&lt;br /&gt;&lt;br /&gt;In the meantime, I&#39;ll look at ways of ensuring that every frame is drawn, perhaps by increasing the intra-frame timeout interval.</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/8919280034348592156/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/8919280034348592156' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/8919280034348592156'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/8919280034348592156'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2008/09/interesting-observations-about.html' title='Interesting observations about WebKit/Chrome and Chronoscope Benchmark'/><author><name>Timepedia</name><uri>http://www.blogger.com/profile/01408958797259838599</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-3735487102995126471</id><published>2008-09-04T02:25:00.000-07:00</published><updated>2008-09-04T02:32:03.180-07:00</updated><title type='text'>Chronoscope Browser Benchmark Live</title><content type='html'>Hey guys, in order to assist in the browser wars, and to let everyone test browser performance, I&#39;ve built a publicly hosted version of the benchmark I used to test Google Chrome vs Firefox 3.1 and Safari 4.&lt;br /&gt;&lt;br /&gt;Now you can run the same benchmarks too, just hop on over to &lt;a href=&quot;http://api.timepedia.org/benchmark&quot;&gt;http://api.timepedia.org/benchmark&lt;/a&gt; and let the battle begin!&lt;br /&gt;&lt;br /&gt;As an errata for my previously published benchmarks, I did find that somehow my WebKit Nightly batch file wasn&#39;t invoking Safari correctly on my Win32 box (it was complaining about a file not found which I didn&#39;t see in the console), so my previous results were NOT with SquirrelFish. Upon rerunning with SquirrelFish, I find it actually does hold its own vs Chrome.&lt;br /&gt;&lt;br /&gt;If you&#39;re one of those poor folks who are running IE, don&#39;t try to run the benchmark. :) It might run, but chances are, you&#39;ll get a slow script warning. Also, it uses Flash for rendering instead of  Canvas, so it&#39;s not an Apples-to-Apples comparison.</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/3735487102995126471/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/3735487102995126471' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/3735487102995126471'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/3735487102995126471'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2008/09/chronoscope-browser-benchmark-live.html' title='Chronoscope Browser Benchmark Live'/><author><name>Timepedia</name><uri>http://www.blogger.com/profile/01408958797259838599</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2515037436118935802.post-7816993018853177792</id><published>2008-09-02T12:34:00.000-07:00</published><updated>2008-09-02T15:38:21.375-07:00</updated><title type='text'>Google Chrome smokes the competition on Chronoscope</title><content type='html'>The current browser wars make me feel like I&#39;m watching the Fast and the Furious, and Google&#39;s new Chrome browser is like Nitrous Oxide for the web. As soon as I heard that ex-HotSpot guys were working on it, I knew it could be good, as they did a marvelous job with Self (a fully dynamic language), as well as Java, but how good?&lt;br /&gt;&lt;br /&gt;To test, I decided to use my own codebase, &lt;a href=&quot;http://timepedia.org/&quot;&gt;Chronoscope&lt;/a&gt;, for several reasons. First of all, it is not a microbenchmark, it exercises a big swath of the browser&#39;s code path. Secondly, it is computationally intensive, so if anything were going to show speed on a &quot;real world&quot; app, this would be it. Finally, I need visualizations to run faster, as I want to use &lt;a href=&quot;http://openwebfoundation.org/&quot;&gt;Open Web technologies&lt;/a&gt; like Canvas or SVG rather than relying on Flash.&lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;http://timepedia.org/&quot;&gt;Chronoscope&lt;/a&gt; has a workload which contains elements of the following:&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Large amount of affine transforms calculated within Javascript code&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Many binary searches and recursive calls&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Frequent random and linear access to large objects (arrays with 30,000+ elements)&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Numerous DOM calls to absolute position DIVs (emulate text rendering on Canvas)&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Huge number of CanvasRenderingContext2D calls for rendering the chart&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Ubiquitous use of abstraction, delegation, inheritance&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;http://timepedia.org/&quot;&gt;Chronoscope&lt;/a&gt; is written in GWT, and to some extent, the GWT compiler may negate some of Chrome&#39;s V8 technology in the sense that GWT &quot;de-classes&quot; many OO polymorphic dispatches into a more functional style of programming, removing as much dynamic dispatch as possible, and eliminating prototype lookups and function call overhead through inlining. I don&#39;t know if GWT hurts &quot;hidden classes&quot; or not, but it might be possible that if GWT didn&#39;t provide such optimizations, the performance differential might be larger.&lt;br /&gt;&lt;br /&gt;Despite this, the results are still good. The test consisted of calling the chart&#39;s redraw() function 100 times per trial, with 10 trials. The slowest and fastest trial are thrown out, and the mean and standard deviation are calculated on the remaining data.&lt;br /&gt;&lt;br /&gt;I tested on a Mac Pro 2.66Ghz with 6Gb of memory, OSX 1.5. The tests were conducted within a Parallels VM running XP2 Service Pack 2, given 2 CPUs and 2Gb of memory. For each browser, I rebooted the VM from a clean start, and ran only the test browser.&lt;br /&gt;&lt;br /&gt;The tests were conducted with the lastest version of Safari 3.1 for Windows (run via WebKit nightly batch file), the Firefox 3.1 nightly with TraceMonkey enabled, and the Chrome beta download.&lt;br /&gt;&lt;br /&gt;&lt;table border=&quot;1&quot; cellspacing=&quot;0&quot;&gt;&lt;br /&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;Browser&lt;/th&gt;&lt;th&gt;Mean&lt;/th&gt;&lt;th&gt;Standard Deviation&lt;/th&gt;&lt;th&gt;Memory&lt;/th&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Firefox 3.1 (TraceMonkey)&lt;/td&gt;&lt;td&gt;3647ms&lt;/td&gt;&lt;td&gt;81ms&lt;/td&gt;&lt;td&gt;49M&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;Safari 3.1 (SquirrelFish)&lt;/td&gt;&lt;td&gt;3005ms&lt;/td&gt;&lt;td&gt;385ms&lt;/td&gt;&lt;td&gt;106M&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;Chrome&lt;/td&gt;&lt;td&gt;1690ms&lt;/td&gt;&lt;td&gt;190ms&lt;/td&gt;&lt;td&gt;44M&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;Chrome looks to be twice as fast as the competition on real world apps. Moreover, it&#39;s performance per Mb of memory is good as well. Even more interesting was the variation in Firefox 3.1, which appears to be related to garbage collection or memory allocation. In some runs, it was very fast, but in others, it was 50% slower.&lt;br /&gt;&lt;br /&gt;As soon as Chrome is released on OSX, it will replace Safari for me, for two reasons. The first, the process isolation and speed features. Secondly, Chrome includes a very nice Firebug-like debugger/inspector that I like better than the Safari equivalent.&lt;br /&gt;&lt;br /&gt;So in summary, Chrome rocks! All they were missing from the launch was Vin Diesel.&lt;br /&gt;&lt;br /&gt;-Ray</content><link rel='replies' type='application/atom+xml' href='http://timepedia.blogspot.com/feeds/7816993018853177792/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/2515037436118935802/7816993018853177792' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/7816993018853177792'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2515037436118935802/posts/default/7816993018853177792'/><link rel='alternate' type='text/html' href='http://timepedia.blogspot.com/2008/09/google-chrome-smokes-competition-on.html' title='Google Chrome smokes the competition on Chronoscope'/><author><name>Timepedia</name><uri>http://www.blogger.com/profile/01408958797259838599</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>10</thr:total></entry></feed>