High Performance GXT / GWT Applications: Coding Java for JavaScript

It's been some time, so I thought I would try and post another informative blog.
I thought my second blog would involve detailing the trouble points and steps involved in migrating between Gwt-Ext and GXT, however, it’s now been such a long time that I can no longer remember what the trouble points were except for a few:
- The Portal class: the way the columns are defined and the way you interact with the contents of those columns is considerably different. In Gwt-Ext you would instantiate your PortalColumn class and do all the setup with this, in GXT the Portal class itself has all the methods you require.
- Listeners and Events: Gwt-Ext was very tightly type-cast which made working out which listener to use and which events to raise incredibly easy. Actually, it was more than easy, you had to use a specific setter method for a specific type of listener so nothing could ever go wrong! In GXT the use of generics is taken to the next level (sometimes a bit excessively) and most classes will not have methods such as addClickListener() or addMouseListener(), instead they will all have addListener(). Some classes do have more tightly type-cast listeners (ListStore for example has an addStoreListener() method), but generally speaking generics are the rule of thumb which can be a pain initially.
- XmlReader / JsonReader: these do not support nested items in GXT. Hopefully this will change. In the mean time it’s easy enough to extend these classes to handle this for you.
If I have an epiphany I will come back and write something a bit more extensive on migration but for the time being it will have to wait. Having said this, we can now move onto a new hot topic: Coding Java for JavaScript!
GWT has provided us with a magical tool to work with which allows developers to code in Java and turn that into highly compact and optimised JavaScript. This is a great idea, now all Java developers can develop web pages, but, there is one flaw: JavaScript is not Java. This should come as no surprise to most of you but what may be a surprise are some of the performance impacts caused by developing JavaScript like it were Java.
I will only pick on a couple of these items for now and focus primarily on improving GXT’s performance. Note that the underlying principles of the following items can be applied to standard GWT:
- Objects: Creating objects in JavaScript is expensive. This will cause the page to take time to render and potentially freeze for a few moments depending on how old your PC is or whether you are running an old browser like Internet Explorer 6.
- Hash maps: They are slow. Forget slow, they are sluggish. See this post by the guys at Lombardi for some actual figures: http://development.lombardi.com/?p=95. The good thing is that the guys at Lombardi don’t just provide us with figures, they also provide us with a solution: FastStringMap.
- Events and Listeners: Attaching events and listening for these is another expensive activity. This can quickly become very slow as you create and attach multiple events and listeners to different items on your page.
Note that some of the points above are alleviated by new browser technologies (multi-threaded JavaScript and Chrome spring to mind!) but if you are a respectable developer, or better, a developer who wants maximum adoption for their site – then cross browser compatibility is not optional.
So, let’s get onto the topics above and explain how the performance can be improved!
Objects
This is one which can be explained relatively quickly: keep the objects you need to the minimum possible. In most cases the wonderful GWT compiler will take care of removing the objects and actually putting your code inline which will already give you improved performance. However, there are times when you can’t rely solely on the compiler and keeping in the back of your mind that less objects equals more performance is a must. So, unless it’s strictly necessary or unless your code looks like a big mess without them, keep objects to a minimum when possible.
This can prove difficult when using GXTs XML and JSON readers as they create Lists of objects (ModelData). On top of this, all the ModelData objects are based on HashMaps which adds an even greater burden! So, to minimise the pain, we can continue to use the objects but make them faster by removing the HashMaps which brings us onto our next topic: HashMaps.
HashMaps
Slow, slow, slow. Not a perfect English sentence, but it gets the point across! HashMaps are generally very useful, unfortunately these are also very slow in JavaScript. Luckily, the guys at Google have come to the rescue once again giving us the wonderfully fast FastStringMap. You can find more details on this topic here: http://development.lombardi.com/?p=95.
Now, how can we use this information to increase the performance of GXT?
As I mentioned above, all the ModelData objects are based on HashMaps, so in order to increase performance we need to create our own subset of ModelData objects and replace the HashMaps with FastStringMaps.
The first thing to do is subclass the FastStringMap to allow public access to this class (alternatively you can copy this class into your package structure to achieve the same results). The reason for doing this is that Google have unfortunately made this class private! The location of the FastStringMap is:
com.google.gwt.user.client.ui
Once you’ve done this, you need to create your own copies of BaseModel and BaseModelData (note you can also extend the BeanModel and any other model you may need to use, we only extended the model’s above as those were the only ones we needed). We created two new classes called FastBaseModel and FastBaseModelData. Note that the BaseModelData class is more compact as it doesn’t listen for changes to the data so use this class unless you really need the features offered by the BaseModel class or the BeanModel class.
The FastBaseModel and FastBaseModelData are replicas of their original classes except for the fact that they use FastStringMaps rather than HashMaps.
Once you've made these changes, you now need to ensure that you XmlReaders and JsonReaders actually use these objects. In order to do this, extend the two readers (we called them FastXmlReader and FastJsonReader) and override the newModelInstance() methods to return the FastBaseModelData objects:
protected ModelData newModelInstance() {
return new FastBaseModelData();
}
That’s it. You now have super fast readers and data objects. This alone will prove to be quite a performance improvement, especially when you are reading large data sets. The primary difference you will notice is that when large data sets are read, there will no longer be a pause between the download of the data – the reading of the data – and the displaying of the data.
Note that if you are using JsonTreeReader, you can make the same changes there to get the performance enhancements.
Events and Listeners
Last but not least... Events and Listeners. Unfortunately these are quite heavily used throughout GXT and there is little we can do about that. However, there are times when we can make the most of these. I like to revert back to standard GWT and assign browser event listeners (onMouseClick, onClick, etc) to the parent container and let them cascade down to the children. In most cases (unless the class you are using really needs the events) you can then cancel the bubble and stop the event on your overall listener which will improve performance by avoiding hundreds of listeners being fired up!
In your overall listener you can then identify the component which was selected and route accordingly or better, you can have methods to handle the events in the relevant objects and all you do is call these methods from your listener. For example:
GWT.sinkEvents(parent.getElement(), MyListOfEvents);
GWT.setEventListener(parent.getElement(), new EventListener() {
public void onBrowserEvent(Event event) {
if(event = onClick) {
for(myObjects)
myObject.clickHandler();
}
}
});
This is pseudo code but it should give you the general idea. This will then delegate the handling of the event to the relevant object rather than having to sieve or hardcode the elements into the parent class.
Conclusion
These are just some of the things which can be done to improve performance. The most noticeable one will be the HashMap changes (you can actually see the difference in behaviour when your data is loaded) but don’t forget to keep the other factors in the back of your mind as they WILL improve your performance.
I have now ran out of things to say, so for now, peace out. Oh, and if you have any other performance enhancements or items you recon are worth mentioning on this topic, please do leave comments!
We used all the enhancements above to make our first product SambaJAM. Even so we are continously working to improve performance. I'm looking to write an article on this which will include injecting HTML straight into the page and subsequently attaching listeners. In the mean time, if you wish to see what all this looks like as an application rather than snippets of code, feel free to join the Beta program.
We are not going to distribute the information you provide when joining the Beta, but SambaJam works on an invite only basis, so we use your details to invite you into SambaJam. You can then use it as much as you like and as often as you like to share documents, organise your calendar, create wiki's, discussion forums and easily invite other people to work with you.
You should see a box with the form on the right hand side which you can fill in.
If the form is not there, click here to see the form:
Latest Tweets
Blog
- Google phases out support for IE6
- The end of the recession (nearly!)
- The business cost of snow days
- Paper Is The New Plastic
- Good Design
- November Minibar Presentation - Using Open Source to acheive the impossible!
- Some Photos from Recent Exhbition at London Olympia
- Last chance to visit our stand at IMS 2009 tomorrow!
- Press Release: SambaStream to reveal SambaJAM at IMS 2009
- Come visit us at the Online Information Exhibition on 1st - 3rd December 2009
Delicious
Digg
StumbleUpon
Propeller
Reddit
Magnoliacom
Newsvine
Furl
Facebook
Google
Yahoo
GXT Hashmaps
Submitted by Chris moltisanti (not verified) on Thu, 25/02/2010 - 10:38.Hi Alessandro,
Very interesting article on GXT performance. I am implementing a huge enterprise application with GXT so any performance improvements are great to hear.
On your previous response, in GXT 2.1.1 RpcMap is now used. Are you saying that this is now a god and suitably fast replacement for HashMap? Would you suggest replacing this with FashStringMap still?
Thanks
Chris
Hi Chris, There is no need to
Submitted by Alessandro Gian... on Thu, 04/03/2010 - 04:09.Hi Chris,
There is no need to implement the FastStringMap anymore as GXT 2.0 upwards is using FastMap which is as fast, if not faster than, FastStringMap.
Also, Google have done us the courtesy of improving the performance of the standard HashMap as of GWT 2.0! The difference now between FastStringMap and a regular HashMap is very small.
Ale
------------------------------------------
Alessandro Giannone - Director SambaStream
New to GWT GXT
Submitted by Guest (not verified) on Wed, 24/02/2010 - 04:12.Hi,
I am very very new to GWT and GXT web technology.
Could you help me what kind of books and Website should I use to understand them.
I have read you post all the way long and I realize that you could help the needy with useful information. So please help me how to learn GWT and GXT.
Thanks in advance
GXT MVC and web desktop
Submitted by kurt (not verified) on Wed, 20/01/2010 - 04:29.Hi Alessandro,
We need to create a prototype based on GXT MVC and web desktop examples. Do you have any inputs on how to integrate the two structure?
I started out integrating the Login dialog from the Maill application (using the MVC structure) into the GXT dekstop. The login is already working and upon a successful login, the desktop and shortcuts are displayed.
Now, I have some questions on how to move forward. I hope you can help me out by pointing me to the right direction.
1. I'm just starting out with GXT MVC, and the only guide I have on how to implement it is the GXT mail application sample. To integrate the MVC structure into a a web desktop app, I'm planning to make a Controller and a View that corresponds to a DEsktop Shortcut/window. IS this correct?
Ex:
- My desktop has a shortcut/window called My Machines (A masterlist grid of all the machines, with all machine info and CRUD)
- MachineView -> Constructs the view (grid with CRUD on a window).
- MachineController -> Registers the ShowMachineWindow event. IF the event is dispatched, this controller displays the view MachineView.
This structure is repeated for every shorcut/window in the dektop. IS this how to go about it? OR am I doing it all wrong? What's the best way to do it?
Hi Kurt, I looked into the
Submitted by Alessandro Gian... on Thu, 21/01/2010 - 21:11.Hi Kurt,
I looked into the GXT MVC framework implementation quite some time back. However, it seemed overly complicated, not to mention it's implementation is actually flawed. With that in mind and to favour maintainability, we chose to not use the GXT MVC framework.
If you have no choice but to use it, then there are some good articles out there on how to use the MVC Framework :). Here is one which is quite good:
http://raibledesigns.com/rd/entry/gxt_s_mvc_framework
There are also other good GWT articles on RaibleDesigns.
However, the article by Olivier Gerardin (who recently wrote a book on GWT in french) is a very good explanation about why the GXT MVC implementation is flawed. Definetely worth a read:
http://blog.gerardin.info/archives/40
Hope this helps. Let me know how your prototype goes.
Ale
------------------------------------------
Alessandro Giannone - Director SambaStream
good post
Submitted by tragamonedas en linea (not verified) on Tue, 01/12/2009 - 11:14.Thanks for such a beautifully composed, informative article.I think your designing work to this is really great .I really appreciate your work to this site.So thanks for it.I hope you can continue this type of hard work to this site in future also..Because this blog is really very informative and it helps me lot.
Using GXT with Spring and Hibernate
Submitted by Guest (not verified) on Fri, 13/11/2009 - 13:29.I am as new to GWT & GXT as a newborn baby.
We need to get GXT working with Spring and Hibernate. Can you help / point to resources to get started on this please?
Thanks in advance.
Hi, Please see the links
Submitted by Alessandro Gian... on Wed, 25/11/2009 - 11:18.Hi,
Please see the links below, they should help you get started:
http://code.google.com/p/gwt-hibernate/
http://code.google.com/p/gwt-spring-hibernate-reference/
http://hibernate4gwt.sourceforge.net/hibernate_gwt_problem.html
http://www.rbgrn.net/content/32-using-hibernate-google-web-toolkit-gwt
http://swik.net/gwt+Hibernate
Regards,
Ale
------------------------------------------
Alessandro Giannone - Director SambaStream
HashMaps
Submitted by Olivier Gérardin (not verified) on Fri, 16/10/2009 - 20:45.Hi Alessandro,
In my current project we make heavy use of BaseModel subclasses both as transfer objects for the RPC services and as models for the FieldBindings. So far we haven't made any attempt to improve performance, but the FastStringMap hack looks interesting... We might try that soon.
BTW if Google developped a fast replacement for HashMap, why doesn't the compiler substitute this type for HashMap when possible?
Hi Olivier, Which version of
Submitted by Alessandro Gian... on Tue, 27/10/2009 - 18:29.Hi Olivier,
Which version of GXT are you using? This is only present in GXT 1.x. I showed them the blog and they put the HashMap improvements as part of the GXT 2.x release which means you already have this enhancement there. They also replaced HashMap's across most of their classes which is great.
The only thing to be careful with now is code being written as part of your project.
I agree with the comment on GWT replacing HashMaps. They do have their own class available, but even that is not extensible and cannot be accessed! I guess we have to live with a workaround for the time being.
Thanks,
Ale
------------------------------------------
Alessandro Giannone - Director SambaStream
great job.....
Submitted by Suresh sivanantham (suresh.sivanantham@gmail.com) (not verified) on Fri, 14/08/2009 - 18:07.hi ,
I am pretty new to GXT... and i feel your post will help me a lot to write efficient GXT code..... Its really awesome post....... hope i ll get more valuable points of GXt from you.....
:-) :-) :-)
:) :) :)
Hi Suresh, Glad I could help!
Submitted by Alessandro Gian... on Fri, 02/10/2009 - 08:58.Hi Suresh,
Glad I could help! Good luck on your programming travels through the world of GWT and GXT. The one thing I can say is that if you are a web developer, this is almost paradise! :)
Regards,
Ale
------------------------------------------
Alessandro Giannone - Director SambaStream
Listeners are being deprecated in GWT
Submitted by Emanuele Ziglioli (not verified) on Tue, 28/07/2009 - 04:05.Ciao Alessandro,
I've just found one post of yours about web forms in gxt and followed all the way to your company's web site and blog.
I've been working with GXT for the past two days and I'm impressed by their intimate knowledge of GWT. It seems they've taken it a step up, by improving where GWT is lacking and reusing all they could.
Anyway, their event model is all based on listeners but in a post to the GWT forum Joel Webber has recently written they will probably be removed in GWT 2.0 in favour of handlers.
Do you thing they're going to migrate GXT to the use of handlers?
Ciao Emanuele, GXT does bring
Submitted by Alessandro Gian... on Fri, 02/10/2009 - 09:08.Ciao Emanuele,
GXT does bring in an even higher level of abstraction with many useful libraries which with GWT you would have to build from scratch. I must say, it also has a very nice looking interface!
With regards to the migration to handlers, I'm not sure what their strategy is going to be, but it's likely they will also move to handlers in order to facilitate adoption of GXT, especially by people new to GWT. Otherwise people would get used to using Handlers and when they move to GXT they would need to learn a new paradigm, listeners. Only time will tell I guess.
Ale
------------------------------------------
Alessandro Giannone - Director SambaStream
Post new comment