High Performance GXT / GWT Applications: Coding Java for JavaScript

agiannone's picture

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:

  1. 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.
  2. 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.
  3. 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,sign-up for your free SambaJAM account: http://www.sambajam.com

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Migrating from gwt-ext to gxt

Hello Alessandro,

I have been working in gwt-ext for sometime and is planning to gxt, since gwt-ext has stopped their development and other licensing issues. Project I am working with is quite big and I would like to know how far the extent of migration is applicable. I would like to know whether I must start the project from scratch or just renaming and changing the required files for gxt.

agiannone's picture

Hi, I must admit it's been a

Hi,

I must admit it's been a while since we did the migration from gw-ext to GXT. As far as I remember there a number of changes, and this was migrating from gwt-ext to GXT 1.x, and there were some changes also between GXT 1.x and GXT 2.x

My suggestion is to try and remove the gwt-ext library and import the GXT library and see the amount of damage caused once you've re-imported the packages.

Either way (renaming or re-writing) will take time. The cleanest solution is to re-write the classes as there are a number of changes between gwt-ext and GXT. So writing GXT code from scratch will probably yield better results and cleaner code.

Thanks,
Ale

------------------------------------------
Alessandro Giannone - Director SambaStream

GXT Hashmaps

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

agiannone's picture

Hi Chris, There is no need to

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

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

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?

agiannone's picture

Hi Kurt, I looked into the

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

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

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.

agiannone's picture

Hi, Please see the links

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

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?

agiannone's picture

Hi Olivier, Which version of

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.....

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.....

:-) :-) :-)

:) :) :)

agiannone's picture

Hi Suresh, Glad I could help!

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

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?

agiannone's picture

Ciao Emanuele, GXT does bring

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

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Latest Tweets

SambaStream Newsletter

Follow our Product Updates and Company News with our Monthly Newsletter

Syndicate content