Accelerating our Android apps with Baseline Profiles

With billions of users engaging with Android applications, enhancing the Meta app experience remains a top priority. Recent advancements have seen the integration of Android’s Baseline Profiles, which have significantly bolstered performance across Meta’s suite of applications.

This exploration delves into the performance challenges faced by Meta’s applications, acknowledging the evolving complexities of user needs and the infrastructure developed to address these issues. Insights into the creation of Baseline Profiles, informed by user data, reveal that these profiles have led to performance enhancements of up to 40% across various critical metrics.

Application performance is paramount for ensuring a seamless user experience. Factors such as slow startups, dropped frames, and unresponsive interfaces are major contributors to user frustration and potential attrition. A keen focus on performance during application development, alongside the implementation of efficient data structures, algorithms, and caching strategies, is essential for mitigating these concerns. Equally important is a comprehensive understanding of the compiled application code’s underlying representations and the methods of loading and executing it, allowing for optimal configuration and tuning of build tools and runtimes.

Over recent years, Meta has established a robust infrastructure for profile-guided compiler and runtime optimizations specifically targeting Android applications. A cornerstone of this infrastructure is the Android Runtime’s Baseline Profiles feature, which has been extensively utilized to enhance the performance of Meta’s Android applications.

ART Performance Considerations

In the realm of Android, Kotlin and Java dominate as the preferred languages for application development. These languages compile to Dalvik bytecode, packaged into “.dex” files organized by classes and methods reflecting their original sources. The execution of dex code requires the parent class to be loaded by the Android Runtime (ART), a process that incurs overhead during application execution. This overhead can lead to temporary performance degradation, particularly during cold starts, where the app is launched for the first time, necessitating a complete class load.

ART employs a just-in-time compiler to translate hot methods into machine code, significantly speeding up execution compared to interpretation. However, both class loading and method execution incur runtime costs that can affect user experience. Cold starts are particularly challenging, as they require classes to be reloaded, although advancements in Android 14+ have introduced runtime app images to mitigate some of these issues.

Meta’s Mobile App Challenges

Meta’s mobile applications serve as the primary access point for most users, with a significant majority utilizing Android devices. The challenge lies in balancing the rapid deployment of new features with the imperative of maintaining high performance. Startup performance is critical, as it can disproportionately influence user satisfaction.

As new features are integrated—such as Instagram Reels or Messenger’s End-to-End Encryption—the number of classes loaded at startup increases. Essential functionalities like crash reporting and login authentication also contribute to this growth. For instance, Facebook and Instagram each load over 20,000 classes at startup, with additional classes required for feed scrolling.

Beyond startup, optimizing user journeys—such as scrolling through feeds or rendering photos—remains a priority. These journeys are meticulously analyzed to understand user behavior and the specific classes loaded during different interactions. Data collected from diverse user profiles reveals significant variability, necessitating a tailored approach to optimization.

ART Install-Time Optimizations

Since Android 9, ART has introduced install-time optimizations, including:

  • Ahead of Time (AOT) compilation of specified methods.
  • Creation of an app image containing specified classes.

AOT compilation allows specified methods to be pre-compiled to machine code, eliminating the overhead of initial interpretation. The app image facilitates rapid class loading at startup, significantly enhancing performance. These optimizations can be activated through special profiles provided at app installation, with Cloud Profiles and Baseline Profiles being the two primary mechanisms.

Cloud Profiles

Cloud Profiles aggregate profiling data from early users during the rollout of an app version. While beneficial, they present several limitations:

  • Early users do not benefit from the optimizations, as they provide the profiling data.
  • Developers lack control over the classes and methods included in the profile.
  • They are skewed towards early startup improvements.
  • Only available through Google Play, limiting their applicability for sideloaded apps.

Baseline Profiles

In contrast, Baseline Profiles are generated and provided by developers, granting them full control over install-time optimizations. By packaging Baseline Profiles within the corresponding APK or AAB, developers can tailor optimizations to their app’s specific needs, extending beyond just startup scenarios.

While Google offers mechanisms for generating Baseline Profiles from benchmarks, developers can also specify classes and methods directly using a tool called profgen, allowing for greater flexibility.

How We’ve Created Baseline Profiles at Meta

Meta has long recognized the performance challenges associated with cold starts, particularly given the extensive class loading required. By optimizing the order of classes in the dex file based on typical load positions—an approach known as “Interdex Ordering”—Meta has seen significant performance gains. ART’s install-time optimizations further enhance this by eliminating loading costs for certain classes and ensuring hot methods are compiled before the app’s first run.

To create Baseline Profiles, Meta utilizes a variety of data sources, continuously experimenting and tuning configurations. Initially relying on static profiles from AndroidX libraries, the company has since developed sophisticated collection technologies to capture class and method usage data from users.

Collecting Profile Data

Benchmarks serve as one method for gathering profile data, but for complex applications like Facebook and Instagram, user data collection is essential. A custom ClassLoader logs class loading events, which are periodically uploaded for analysis. This process is carefully managed to minimize performance impact, with only a low sample rate enabled.

Method usage is tracked through specialized telemetry, allowing for granular identification of commonly invoked method clusters. This data is aggregated and processed into a “Human Readable Profile,” which is then used to generate the final Baseline Profile.

Tuning and Experimentation

Initially, the focus was on optimizing cold starts, with strict thresholds for including classes and methods in the profiles. Over time, these thresholds have been adjusted, expanding optimization efforts to encompass other user interactions. Currently, classes and methods appearing in at least 20% of cold start traces are included in the profiles, addressing interactions such as newsfeed scrolling and navigation within the apps.

While some regressions in performance have been observed during experiments that increase profile size, careful measurement and targeted additions have allowed for significant growth in profile entries, with tens of thousands now included across Meta’s applications.

The Impact of Baseline Profiles at Meta

Over the past few years, the implementation of Baseline Profiles across Meta’s major Android apps has yielded consistently positive results. As the integration and refinement of these profiles have progressed, notable improvements in app startup times, scrolling performance, navigation latency, and other critical metrics have been recorded, with enhancements ranging from 3% to 40%.

Baseline Profiles have emerged as a powerful tool for Meta’s teams, enabling meaningful improvements to user experience year after year. The ongoing investment and experimentation with Baseline Profiles have proven invaluable, offering valuable insights for Android developers looking to enhance their applications.

AppWizard
Accelerating our Android apps with Baseline Profiles