Facebook today provided an intimate peek at some of the code-level work its engineers are doing to keep its Android app performing at the top of its game. Facebook's optimization efforts center on reducing the amount of Java bytecode so older devices running on slower networks still have a good experience.
Not everyone enjoys the zippy speeds afforded by LTE 4G networks. Though LTE is widely available, its deployment in emerging markets definitely lags that of North America, Europe, and parts of Asia. More to the point, people in emerging markets can't afford the newest and fastest smartphones. Often, consumers buy older, used hardware and hold onto it for as long as they can.
This creates a challenge for Facebook. Given its global reach, Facebook knows it needs to be available to as wide a number of devices (and platforms) as possible. More to the point, it wants users to enjoy using Facebook. That's not going to happen with bloated apps that target high-end hardware.
Consider smartphones that have less than 1GB of storage. Every byte counts on such phones. Fewer bytes translates to faster download and install times, faster runtimes, and lower data usage across cell networks. It's a case of smaller is better.
"Android is one of our biggest platforms, and it's also the mobile platform with the greatest diversity of devices," said Facebook engineers Marty Greenia, Bert Maher, and Shane Nay in a blog post. "Any performance or efficiency improvements on these devices could better the experience for millions of people around the world."
In order to do this, Facebook put Redex to work behind the scenes to minimize Java bytecode in the Android APK. Redex is a pipeline for streamlining .dex files. Greenia, Maher, and Nay felt the best approach was to optimize the bytecode after the .dex files were formed, but before they were added to APKs.
You might be wondering why Facebook chose to optimize at the bytecode level rather than the source code level. Facebook said it "gives us the maximum ability to do global, interclass optimizations across the entire binary, rather than just doing local class-level optimizations." Moreover, certain optimizations can only be applied to dex bytecodes.
The good news here is Facebook soon realized that the bytecode contained some repeating patterns that its engineers knew could be stripped out through Redex.
For example, minification and compression are effective within Java, when applied to human-readable strings. Since the runtime care about descriptively named strings, many of them can be removed with shorter placeholders. Inlining is another tool Facebook found could be put to use in reducing bytecode. For example, inlining certain functions can reduce the amount of overhead without impacting its functionality. Then there's dead code elimination. Facebook calls this a straight-forward technique for reducing the binary size without introducing any problems.
The end result, says Facebook, is much more efficient code that will run well on Android devices old and new alike.
Greenia, Maher, and Nay provide an incredibly in-depth look at these techniques in a blog post. You can read more about it here.