[フレーム]
BT

InfoQ Software Architects' Newsletter

A monthly overview of things you need to know as an architect or aspiring architect.

View an example

We protect your privacy.

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Unlock the full InfoQ experience

Unlock the full InfoQ experience by logging in! Stay updated with your favorite authors and topics, engage with content, and download exclusive resources.

Log In
or

Don't have an InfoQ account?

Register
  • Stay updated on topics and peers that matter to youReceive instant alerts on the latest insights and trends.
  • Quickly access free resources for continuous learningMinibooks, videos with transcripts, and training materials.
  • Save articles and read at anytimeBookmark articles to read whenever youre ready.

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

Oct 04, 2024 2 min read

Write for InfoQ

Feed your curiosity. Help 550k+ global
senior developers
each month stay ahead.
Get in touch

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

Sergio De Simone

Show moreShow less

Rate this Article

Adoption
Style

This content is in the Mobile topic

Related Topics:

Related Content

The InfoQ Newsletter

A round-up of last week’s content on InfoQ sent out every Tuesday. Join a community of over 250,000 senior developers. View an example

We protect your privacy.

BT

AltStyle によって変換されたページ (->オリジナル) /