BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Compiler Explorer Provides Insights into Low-Level Android App Optimization

Compiler Explorer Provides Insights into Low-Level Android App Optimization

Android engineers at Google recently added support for the Java and Kotlin programming languages to Compiler Explorer, an open source Cloud-based tool aimed at exploring how compilers work by compiling code in real-time. Using Compiler Explorer, Android engineers can optimize the performance of their apps by observing how the compiler works under the hood instead of using a set of pre-defined best practices.

In particular, say Google engineers, using Compiler Explorer it is possible to gain an intuition of the optimizations performed by the compiler, estimate memory usage, and identify ways to generate more efficient instructions that translate into faster execution or lower memory usage.

At Google our engineers use this tool to study different coding patterns for efficiency [...] Instead of teaching developers to memorize different rules for how to write efficient code or what the compiler might or might not optimize, give the engineers the tools to find out for themselves what happens when they write their code in different ways, and let them experiment and learn.

Compiler Explorer may be used to identify opportunities to improve the compiler itself, as Google engineers did by implementing coalescing returns from switch statements, but it can be applied to any context. For example, it easily shows that the following two implementations are translated into exactly the same code by the compiler (due to an optimization called constant folding:

class ConstantFoldingDemo2 {
   static int demo(int num) {
       int result = num;
       if (num == 2) {
           result = num + 2;
       }
       return result;
   }
}

class ConstantFoldingDemo1 {
   static int demo(int num) {
       int result = num;
       if (num == 2) {
           result = 4; //-- return a constant instead of calculatin a sum
       }
       return result;
   }
}

Another interesting use case Google engineers highlight is comparing if the Java and Kotlin compilers translate code differently or not, e.g. when using string Java concatenation vs. Kotlin string interpolation.

The Kotlin syntax is easier to read and write, but does this convenience come at a cost? If you try this example in Compiler Explorer, you may find that the Dalvik bytecode output is roughly the same! In this case we see that Kotlin offers an improved syntax, while the compiler emits similar bytecode.

Compiler Explorer, explain Google engineers, also makes it possible to understand how minifying an app using R8 helps make it smaller and faster and provides insights into the three main benefits it brings, namely, dead-code elimination, byte-code optimization, and obfuscation.

You can use R8 in Compiler Explorer to understand how minification affects your app, and to experiment with different ways to configure R8.

A final example Google engineers provide is using Compiler Explorer to fine-tune Baseline Profiles, which are configurations developers provide to the Android Runtime so it knows which methods are critical for performance and thus worth compiling ahead of time (AOT) instead of just in time (JIT). Optimizing an app using Baseline Profiles can make your app faster on launch by up to 40%.

Compiler Explorer is available for over 30 languages, including C/C++, C#/F#, Rust, Go, Python, and Java. You can use it through its public website or install it locally.

About the Author

Rate this Article

Adoption
Style

BT