Skip to content
Rezha Julio

android

A Faster Cache Made My App Slower

3 min read

Caching the library read was supposed to fix laggy tab switches. It made them worse. The real cost was rendering every poster at once.

A delivery person tips an overflowing cart of posters onto a shopkeeper's counter in one huge heap, while a neat grid of nine framed posters hangs on the wall behind
A delivery person tips an overflowing cart of posters onto a shopkeeper's counter in one huge heap, while a neat grid of nine framed posters hangs on the wall behind

I have an Android app project that tracks what I’m watching. It pulls my library from Simkl: TV shows, anime, movies. The main screen has bottom navigation tabs, and each tab shows a grid or list of posters from that library.

Switching tabs felt slow. Tap, wait a beat, then the screen appears. I noticed it every time.

The obvious suspect

The tabs read the library from a local Room database. A query returning the whole library on every tab switch sounds like the problem, so the first theory was that the read is slow and needs a cache.

The first fix was a small in-memory cache keyed by the library’s version. If nothing changed, the screen gets the data instantly without hitting the database.

Reads became instant, and the app felt worse.

What was actually happening

The screens rendered the library like this:

Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
items.forEach { row -> LibraryRow(row) }
}

A Column in Compose composes all of its children, eagerly and synchronously, on first composition. That means every poster row, on the UI thread, before anything can draw.

Before the cache, the data arrived asynchronously. The tap painted an empty screen quickly, and the giant composition burst happened a beat later when the database answer landed. The cost was still there, just hidden behind the async read.

With the cache, data arrived instantly, so the entire burst landed inside the tap’s first frame. The UI thread blocked on composing hundreds of posters before it could paint anything. The cache had removed the async delay that was accidentally covering for the real problem.

The actual fix

Render only what’s on screen:

LazyColumn {
items(visibleRows, key = { it.id }) { row -> LibraryRow(row) }
}

LazyColumn and LazyVerticalGrid compose just the visible items and the rest as you scroll. The filtering and sorting also moved into remember, so the screen stops re-filtering the whole library on every recomposition:

val visibleRows = remember(library, filter, sort) {
library.filter(filter).sortedWith(sort.comparator)
}

The cache stays. With lazy rendering it finally does what it was meant to do: serve the data quickly without making the first frame pay for it.

The numbers, with an asterisk

The project now has a test that composes the screen against a synthetic 1,500-item library and times the first frame:

  • Eager version: ~1,735 ms of composition
  • Lazy version: 45 ms warm, 563 ms cold

That is a big gap, with two caveats. First, 1,500 is a test fixture, not my actual library size. Second, this ran under Robolectric, so it measures composition work only: no real layout, no draw, no image decoding, and definitely not tap-to-first-paint on a device.

The check I trust more is simpler. I installed the build on my phone and tapped through the tabs. It’s snappy now.

What I learned

Speeding up the layer you can measure can move the cost to a layer you weren’t looking at. The database was the first suspect because that’s where the visible wait seemed to be. The stall I felt, from tap to first paint, came from somewhere else.

Measure where the user feels the pain, even if that layer is harder to instrument. And when an optimization makes things worse, it still tells you where the real cost is.


Related Posts


Previous Post
Why I Am Building NextFrame