Saturday, January 9, 2010

LWUIT Is Faster Than LCDUI For Networking (at least on Samsung)

Readers of this blog should be familiar with the drill, operator goes ape sh*t over the performance of your application on a specific device and you get called in to do a weekend session on trying to figure out WTF is wrong???

Where do you even start?

I got a similar call over this weekend where an application under evaluation at an operator just didn't perform as expected for file download on Samsung devices (although is doing quite fine for other devices). Well, obviously LWUIT had nothing to do with it since its file downloads and unrelated to UI... Right?

I tried practically everything I could think of, from thread priorities to sleeps in various strategic places... Nothing.

Then I decided to start fresh, I created a pure LCDUI test case for downloading and got a respectable 80kbps download speed. When switching the test case UI to unthemed LWUIT I got a 140kbps download speed...

I always say LWUIT is fast but this REALLY floored me! There was practically no UI involved and the issue here was download speed rather than the UI speed, what was going on???

It turns out that the Samsung devices have a different threading model, even if you assign priorities to threads the background threads do tend to grab CPU even from networking (as opposed to e.g. Sony Ericsson where a network operation can stutter the UI). This behavior resulted in unbelievable performance difference between the LCDUI and LWUIT test case, even though the test is supposed to be completely IO bound.

Armed with this knowledge I did the following in the code:
  1. Raised the priority of the network thread when its working on important downloads (when showing a blocking progress bar).
  2. Reduced repaints and animations, e.g. an animate method that returns true even when a glass pane was drawing progress on top of it.
  3. I made another animate method refresh less frequently.
These changes coupled with some minor tunings got me far better performance which I can probably improve further.

Lesson learned: LWUIT's EDT thread spends allot of time sleeping and is very highly optimized, possibly more so than some LCDUI implementations. This seems like a small thing but in some devices its more important than others.

To optimize networking on Samsung devices don't think so much about networking, think about the threads you have in the background.

Having a single thread for all your IO/Networking is a good practice when writing mobile applications (obviously a single thread separate from the EDT!). This policy which was already in place had made my life very easy when debugging and optimizing issues since there was only one place to look.

Last but not least, its very difficult to debug performance problems. Its more guesswork than anything else since devices just don't give us any proper profilers or similar tools that we might have on the desktop. They also behave differently than you would normally expect (case in point above). Currently the best advice I can give with such problems is: Isolate a test case and start growing it until you reproduce and truly understand the problem.

Saturday, January 2, 2010

Tensile Drag, Centered Lists And Faster Slide

Running ahead I often forget that not everyone monitors our SVN commits so you don't all follow the latest and greatest in LWUIT (BTW to do so just go to the project mailing lists and subscribe to the commits mailing list to receive detailed commit notifications).
Three cool new things made their way into LWUIT two of which relate to touch support. The first is tensile drag demonstrated in the video to your right, this provides the feel of reaching the edge of the screen in a more physical way. This feature is now enabled for all LWUIT widgets seamlessly.
We now also support dragging on a list which is fixed on the screen vertically or horizontally (such as a carousel), this was ridiculously hard to implement and a long standing bug in our issue tracker. Fixed lists for those of you who haven't played with them, allow a developer to place the focus in a single point (top, bottom or center) and have the list scroll without the focus moving. This feature is essential for the spinner support and also allows very elaborate cool UI's.

Last but possibly most important is the fast slide transition support, some devices are relatively slow to render elaborate graphics (e.g. fonts). S60 specifically is very slow in this regard (reasonably justified since its fonts are quit crisp), LWUIT is quite efficient in font rendering however when running a slide transition rendering these fonts for every frame often bogs down the system. LWUIT now has a "fast slide" transition within the common transitions class that slides two images of the forms rather than the actual forms. This is noticeably faster on S60 devices but is also reasonably smoother on other devices.

This feature isn't enabled by default for two major reasons, the first is its higher RAM requirements and the second is that it breaks the ability to slide translucent/rounded menus/dialogs. However, if you don't intend to use these it might be worthwhile to switch to this transition for a smoother feel.