One of the more technical sessions I attended at Google IO 2017 was the one titled “Performance and Memory Improvements in Android Run Time (ART)”. In it, Google developers detailed the changes they made under the hood of Android to speed it up in Android O. The question and answer section also revealed a possible change in the compiler for Android P.
The changes made from Android 7.0 to Android O promise to bring a much faster experience to the Android user. There are quite a few of these changes but bear with me while I attempt to explain each one (there is a tl;dr section at the bottom for those inclined that way).
New profile guided compilation
Profile guided compilation was introduced in Nougat and Google have worked hard on this to improve it even further. Now with Android O they have optimised it further so that apps load faster, use less memory and use less time allocating memory to each application.
First a quick overview on Android apps. An app is an apk file which contains one or more dex files which contain instructions for executing the application. The dex files are loaded into memory for the app to run by the Android Run Time (ART). This loading has a RAM cost and a startup time cost.
Profile guided compilation is Android learning how you use an application and allocating resources how you would normally use them. With the new profile guided compilation the location of these dex files is improved by the JIT profile information. It uses this information to move the important information (dex files) that you use the most together. This frees up RAM used by the dex files which in turn causes the app to not only run faster but launch faster.
New garbage collector
A garbage collector in Android is exactly that. It cleans up the processes that have been running for a while. With Android O Google have introduced a new garbage collector that offers large improvements to the whole system speed. This compaction of long lived processes decreases the amount of fragmentation of the RAM offering performance benefits.
In Android N the garbage collector (GC) offered compaction only for background apps but with Android O the garbage collector will compact all apps whether background or not. This new GC is region based and will release RAM in regions of high fragmentation causing a heap that has less wasted RAM. This in turn causes less pause time so the next UI process can occur faster resulting in a faster user experience. Google have found this new GC caused an increase of 40% in the internal benchmarks they use (which they make themselves).
Inliner improvements
This is where the session got really technical and my eyes glazed over for a bit. Essentially inliner improvements is the compiling of only the important things and with Android O it is more aggressive in it’s choice of these important things. This resulted in a 20% improvement in the benchmarks.
Code sinking
Code sinking is the moving of the required instructions (dex files) closer to where they are to be used. Obviously, the further away an instruction is from where it is to be executed the longer it takes to find the code and execute it. By moving the instructions closer to where they are to be used Google have seen a 15% improvement in the Android benchmarks.
Class hierarchy analysis
Class hierarchy analysis is when the run time infers (predicts) when classes and methods can be made final even when they aren’t final. This gives more room for the compiler to perform other things such as more optimisations. The more aggressive this is the faster the system will run. If a new class is introduced and the inference is broken the code is deoptimised and the inference run again.
Loop optimisations
With Android O Google have introduced processes to allow more efficient code for loops. Loops are the continual running of a process in an app. Programs often spend a lot of execution time in loops, thus generation of efficient code is important. The realistic improvements in loop optimisation has introduced realistic improvements of around 10% (the new code actually broke some of Google’s benchmarks).
Vectorisation
The last one I am going to mention is vectorisation. This is an improvement of the way some loop instructions occur. It is the converting of sequential code (usually loops) into code that is not sequential but simultaneous. This results in faster rendering with the possibility of rendering the display at 60fps using less CPU power than a previous rendering at 20fps. This less CPU usage frees it up for other optimisations yet again.
Others
But wait, there’s more. There are quite a few more than that which can be seen in the graph below. These were not discussed but should be mentioned that there are a lot more than just those mentioned above that will be bringing about the performance improvements when Android O is released.
tl;dr
Google has defragged Android making it faster.
A new compiler for Android P
After most of the fellow geeks had left the session a clever developer asked a question of the Googlers on stage that went something like this:
You have spent a lot of time improving the JIT for Android O, with the introduction of Kotlin as a first class language in Android have you looked into or thought about using the much faster Kotlin compiler for Android?
For those who are adept at reading people the answer would have been obvious. The faces of all four Googlers on stage lit up and they got very excited about it. It was obvious to all that were left in attendance that they were indeed testing it and the results were impressive. They talked about yes they were indeed testing a new compiler that may well make it into Android P (my wish is for pavlova).
I consider this piece of information to be one of the biggest tidbits from the session. I think, in my own opinion, that Android will eventually be a lot less (if any) Java and a whole lot more Kotlin, compiler included. Obviously each year Google try to speed up Android but changing the compiler to a Kotlin compiler signals a shift in the Android world.
Thanks for reading all of that – hope you found it informative. If you would like to listen to the entire session (minus the Q and A) head over to YouTube and check it out. As one of our editors said to me: Android O is faster than N, P will have new compiler. Water is wet.
Thank you for so much detail, Scott. I won’t pretend to understand all of it, but it’s still interesting to see what’s going on “under the hood”.