Hacking on layout code
I've been spending a lot of time hacking on layout code at Facebook lately. Most performance wins in layout code can come from very simple changes in the code. They're fairly easy to spot with the right mindset. Here are some very general tips when working on layout code.Understand the layout system
Changing layout code without understanding at least its core features is a recipe for messy/cryptic layout code. Spend enough time reading the code (and specs, if any) so that you can be more deliberate about your changes.Everything is a loop
Layout code is iterative by nature. You should see every line of layout code as an operation that will be executed over and over again in a tight loop or recursion. A single costly operation can lead to bad systemic performance that is pretty hard to track down. Keep that in mind.Inline heavily
Generally speaking, layout code should follow two steps: first, fetch all necessary layout data from children; second, calculate position and size of all elements. Most of the data collection should happen outside iterative blocks in the algorithm if possible—especially when it involves method invocations and non-trivial data structures. This is especially relevant on Android. I've recently made a layout algorithm 50% faster by simply inlining/avoiding a bunch of method invocations in tight loops. Android's method profiler is your friend here.Cheat
Not all features in a layout system are created equal. Some are used more often than others. Look for shortcuts to optimize for the simplest and most common features.
Happy hacking! #androiddev