Mobile Development 9 min read

Optimizing Android AsyncLayoutInflater for Thread Safety and Performance

Xiaohongshu refactored Android’s AsyncLayoutInflater by introducing a configurable thread‑pool, safe object pools, and a singleton ViewCache, eliminating ArrayMap and LayoutInflater lock issues, which together yielded over 20% faster cold‑starts and page loads, demonstrating significant performance and business benefits.

Xiaohongshu Tech REDtech
Xiaohongshu Tech REDtech
Xiaohongshu Tech REDtech
Optimizing Android AsyncLayoutInflater for Thread Safety and Performance

AsyncLayoutInflater is an official Android tool that provides asynchronous layout inflation, offering a new approach to layout performance optimization. This article shares Xiaohongshu's experience in using and refactoring this tool, inviting discussion on further optimization methods.

As Xiaohongshu's user base grows, app performance becomes increasingly critical for user experience, affecting page opening speed and app launch time. Even improvements of a few tens of milliseconds can yield noticeable business benefits.

AsyncLayoutInflater first appeared in the support‑v4 library in 2015 to enable asynchronous inflation, reducing the main‑thread blocking that typically occurs during view inflation.

Using AsyncLayoutInflater is straightforward: add the dependency and invoke it in code (illustrated in the original images).

After asynchronous inflation completes, a callback provides the inflated view for further use.

The tool’s most impressive feature is that asynchronous view inflation does not introduce thread‑safety problems. The implementation relies on a singleton thread that contains a thread‑safe blocking queue and an object pool.

The singleton provides an enqueue method that puts requests into the blocking queue, guaranteeing thread safety. During inflation, mInflateThread.obtainRequest retrieves a request from the object pool and inserts it into the queue.

A simplified code flow shows a run loop with a blocking‑queue take operation that performs the inflation.

The analysis answers how to synchronize data between threads using a classic producer‑consumer model with thread‑safe containers.

Several practical issues were encountered:

4.1 Single‑thread vs Multi‑thread : The original InflateThread is a single‑thread singleton. Providing a configurable thread‑pool improves flexibility; single‑thread performs better when the main thread is idle, while multi‑thread excels during heavy CPU usage such as cold starts.

4.2 ArrayMap thread safety : SimpleArrayMap/ArrayMap use static arrays for caching, which can cause crashes under concurrent access. Clearing the cache on crash avoids the issue.

4.3 inflate lock : LayoutInflater’s inflate method holds a lock, preventing true multi‑threaded inflation. Overriding cloneInContext and using an object pool bypasses this lock.

4.4 BasicInflater refactor : The original BasicInflater was extended to allow custom thread‑pool injection, leveraging the underlying infrastructure for unified thread management. Experiments showed multi‑thread pools outperform single‑thread when CPU is busy, and vice‑versa when CPU is idle.

Additional work rewrote unsafe parts of ArrayMap and LayoutInflater, introduced thread‑safe containers in the onCreateView flow, and removed synchronized blocks.

4.5 ViewCache : A singleton ViewCache pre‑inflates modular views and stores them for quick retrieval, avoiding on‑demand inflation latency. Care must be taken to release unused views to prevent memory leaks.

Overall, the refactored AsyncLayoutInflater, together with the ViewCache and framework integration, delivered over 20% performance gains in cold‑start and note‑detail page scenarios, along with significant business impact. Future work will focus on further usability improvements and combining additional optimization techniques.

Author: Xiaohongshu Commercial Technology Android Engineer, formerly responsible for architecture design and performance optimization, now focusing on transaction‑link iteration and optimization.

mobile developmentperformanceAndroidThread SafetyAsyncLayoutInflater
Xiaohongshu Tech REDtech
Written by

Xiaohongshu Tech REDtech

Official account of the Xiaohongshu tech team, sharing tech innovations and problem insights, advancing together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.