Having leapfrogged Google to become (for now) the go to source for news, Facebook has turned its attention to ensuring that all its users get their content as speedily as possible. Given that the huge and sprawling Android ecosystem is one of its most important gateways to its developing markets, the company has been investing some time in working to optimise Java bytecode for Android through an in-house project called Redex.
On the official Facebook blog, engineers Marty Greenia, Bert Maher, and Shane Ney explain that Redex takes the form of a pipeline for optimising .dex files. When a customisable series of transformations is applied. a source .dex can then be optimised before inclusion in an APK.
The Redex crew explain that their decision to do optimisations at bytecode level instead of directly on the source code was the fact that in gave them the ability to do “global, interclass optimizations across the entire binary” instead of relying on local class-level optimizations. Because certain transactions can only be performed post-DXing, the transforms are performed on dex bytecodes instead of Java – a process the team compare to the post-linking stage in a C-style compilation process, where you can make global optimisations across your whole binary.
The whole shebang is designed adapt to the breakneck pace at which other in-house engineers tend to develop their own bytecode optimisations, and is architected to be “something that would benefit from multiple engineers working on lots of optimizations.” The pipeline has been organised in a series of steps, each of which can be thought of as a stand-alone optimization “plug-in”,beginning with the “original” .dex and ending with the exit of the “transformed” .dex. These can be rearranged to chain multiple, potentially unrelated transforms behind each other, making for a good deal of flexibility for engineers, who can then plug their chains in “only when they are ready.”
Whilst Facebook is known for occasionally throwing projects over the wall, for now, there are no plans to unleash Redex as an open source offering. However, if you’re looking to get your apps running faster across the fragmented terrain of the Android universe, Facebook’s ideas might make for some interesting reading.