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:

Raised the priority of the network thread when its working on important downloads (when showing a blocking progress bar).

Reduced repaints and animations, e.g. an animate method that returns true even when a glass pane was drawing progress on top of it.

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.

1 comment:

There is another blog that also says that LWUIT probabl speed up the network connection: http://eriksdiary.blogspot.com/2008/10/lwuit-first-impressions.html

"...our main problem is network traffic, which has nothing to do with Lwuit, but the toolkit does not seem to slow it down, possible even speeding things up a little (since our old interface repainted the whole screen a bit too often)..."