Google I/O 2009: Native Code, for Good and Ill

google_io_091Next up, we have Dan Morrill’s presentation to a packed house, “How Do I Code Thee? Let Me Count the Ways”, to cover various options for building Android apps.

Dan’s theme was that Android is a “loose confederation” of components working together, regardless of how those components may be developed: managed code (a.k.a., stuff written in Java for Dalvik), AJAX (using WebView), or native code.

We're giving stuff away to help celebrate our tenth anniversary. Are you in?

Some things Dan said would be worked on in future editions of Android, to boost the performance of Dalvik apps, include JIT, improved garbage collection, more APIs (hinting specifically at Bluetooth), and more optimization of the core libraries that Dalvik apps depend upon. As one might expect from this sort of presentation, though, these were general statements, not announcements of future timetables and such.

Android presently integrates WebKit v528.5 along with SquirrelFish for faster Javascript execution. While it lacks HTML5, you do get Gears, at least when using the standalone existing Browser application.

Dan also emphasized a point I try to harp on: Android runs on phones, and phones are much, much slower than modern desktops and notebooks. He demonstrated some code, computing an algorithm, that took under 10 milliseconds in Javascript in Safari…but took nearly 600 milliseconds in Dalvik on an actual device. This is not to say that developing on Android is bad; rather, one must set one’s expectations to match what is realistic for a given platform.

On the Javascript front, there is work going on in Android to experiment with other Javascript virtual machines, to see which is the fastest on a mobile environment, and so they may elect to switch to a different VM if it seems to be faster yet still compatible.

The most interesting area, of course, was the discussion of native C code. Dan indicated that a work-in-progress native development kit (NDK) is now available in the Android git repositories, on the donut branch. This will help developers build shared libraries that can be accessed via JNI from ordinary Android apps…and (hopefully) still allow for distribution via normal APKs via facilities like Android Market. He even hinted that an early release of the NDK might be more widely available within the next few weeks.

Native code could be used for lots of things, such as game physics. However, the intention is still that a Dalvik application is the core, using native code for selective speedups, versus creating applications completely out of C code.

But bear in mind that native code is still not a solution for all performance issues. The algorithm he demonstrated before, that runs on the order of 10ms in Javascript on a notebook and 600ms in Dalvik still takes 100ms when the guts of the algorithm are reimplemented in native code and linked to via JNI. That’s a six-fold increase, but it still pales to modern desktop and notebook CPUs. Again, device CPUs are just plain slow.