Skip to content
AppDues
Log in
Contact salesSign up

Why a Compose paywall tap can pause: recalculation, not recomposition

A tap on a subscription package should update only the components affected by the new selection. In a large server-driven Compose paywall, however, hundreds of components can recalculate on every tap and then discover that nothing changed. This guide explains how that cost was isolated, which fixes mattered, and how to apply the same checks to your own Compose code.

Start with the symptom, not a guess

The reported symptom was a brief freeze after tapping a plan: a ripple began, roughly a dozen identical frames appeared, and only then did the selected plan change. Pauses in the recordings ranged from about 0.33 to 0.50 seconds. Because unrelated animation also stopped, the investigation focused on work blocking the main thread after the state change rather than on the blurred background.

Measure scaling before changing code

An instrumented test rendered a synthetic paywall, changed the selected package on the UI thread, and waited for Compose to become idle. Each run used six warmups and twenty measured selections, reporting the median of at least three runs. Varying the number of plain text components showed whether the cost grew with screen size or was paid once per tap. It grew with component count, pointing to repeated per-component work.

Recalculation can cost more than recomposition

On a 300-text paywall, one tap with the main fix disabled took 700 ms, with 308 override resolutions, zero text recompositions, and 600 variable passes. With the fix enabled, the same tap took 184 ms, with zero override resolutions and zero recompositions. The variable passes remained because texts outside a package still depend on the selected package. The key distinction is that Compose skipped recomposition successfully; the expensive part was recalculating values to prove they were unchanged.

Why derivedStateOf still runs

When a dependency changes, Compose recalculates each affected derived state and compares the result with the last value seen by its readers. Equality allows the reader to skip recomposition, but only after the calculation has run. A derived state without an explicit policy can also create a new record after recalculation, causing downstream derived states to recalculate as well. In the paywall, one private derived value fed about a dozen visible properties, multiplying that work across every text.

Read state only on the path that uses it

The first major fix made condition inputs lazy. Instead of reading selection, offer eligibility, and selected-package data before evaluating any override, each condition received a lambda and invoked only the input it needed. Because snapshot dependencies are recorded from actual reads, an unused lambda creates no dependency. Components whose overrides never mention the selection therefore stop resolving on every tap.

What lazy inputs bought

Disabling only the lazy-input half raised the 300-text tap from 184 ms to 450 ms. On an 89-component template with selection overrides and price variables, override resolutions fell from 99 to 22 per tap. The remaining resolutions belonged to components genuinely affected by selection, such as plan cards. The practical rule is to pass a provider into derivedStateOf, snapshotFlow, layout, draw, and composable scopes, then read it inside the branch that consumes it.

Cache repeated regex compilation

Text templates are processed twice: once for display and once for a spoken version used by screen readers. The spoken pass expands period abbreviations with ten regular expressions. Before caching, those patterns were compiled on every pass, producing 3,000 Regex compilations per tap for 300 texts. Keeping the compiled list beside its source word pairs made later passes cheaper without reducing the number of passes.

The regex cache was the other half

With only the regex cache disabled, a tap took 433 ms instead of 184 ms. With both halves of the main fix disabled, it took 700 ms. On the 89-component template, the combined comparison was 100 ms against 50 ms. The two halves therefore contributed roughly similar savings and were additive.

Fonts need value equality

Downloaded fonts exposed a different failure: equality could never succeed. Each resolution created new font objects, but the font classes used identity comparison rather than value equality. Compose therefore treated every recalculation as a font change, recomposed the text, missed font caches, and parsed the font file again on the main thread. Giving the downloaded-font type value-based equality and a matching hash code restored cache hits.

A real defect is not automatically the reported cause

On a 300-text paywall with a downloaded font, disabling both the font fix and the main fix produced 833 ms per tap, 300 recompositions, and 300 font loads. Restoring only font equality reduced that to 667 ms with zero recompositions and zero loads. Once lazy inputs prevented plain texts from recalculating on selection changes, the font fix had little effect on that path, but it remains important wherever a font does recalculate, such as text inside a selected plan card.

Memoize work whose inputs rarely change

Three smaller audit fixes reduced work that ran more often than its inputs changed. Markdown parsing was memoized by text, shadow drawing moved reusable objects into a cached draw setup, and window size class was derived from window size and density instead of collecting a flow in every component. Screenshot comparisons showed no visible change for the first two, which proves visual parity rather than a latency gain.

Move per-frame animation into the draw phase

For button fading, passing an alpha provider into a graphics layer lets Compose update the layer during drawing without recomposing or laying out the whole stack. The shipped approach still read the animated value in the composable body to decide whether to supply a provider, so the button itself recomposed on every frame. Moving that check into derivedStateOf reduced a standalone press from about 60 button and stack recompositions to four and two respectively.

Do not compose infinite animations at alpha zero

An always-composed indeterminate progress indicator can keep producing frames even at zero alpha. In one report, an untouched paywall rendered 307 frames in five seconds. After the fix, the indicator was composed only while visible; on the test emulator, an untouched paywall rendered zero frames in five seconds instead of 302 to 303. Alpha hides a component, but it does not stop an infinite animation.

Reordering inside one task has limits

Applying the selection before calling an interaction listener saved only about 10 ms with a 300 ms blocking listener, because both operations remained in the same main-thread click task. Posting the listener to the main thread did not help. Deferring it until after the next frame reduced the measured tap to 41 to 48 ms, but delayed later frames and changed callback timing. The safe default is to keep interaction callbacks short and move slow work off the main thread.

A practical checklist for shared Compose state

Use this checklist when a shared state makes a Compose screen feel slow: identify every reader of the shared state; pass lazy providers so unused branches do not read it; count resolutions, recompositions, file loads, and template passes per interaction; cache expensive compiled or parsed objects; implement value equality for snapshot-held values; keep per-frame animation in draw or layer properties; avoid composing infinite animations while invisible; and keep a measurement switch so each fix can be compared independently. The central question is simple: who reads this state, and do they all need to?

Frequently asked questions

Does derivedStateOf prevent recalculation?

No. Skipping happens only after the new derived value is calculated and compared with the previous one. If many components read a shared selection, each may recalculate even when its result stays equal and no recomposition follows.

How can a component avoid depending on a selection it does not use?

Pass the value through a lambda and invoke it only in the branch that needs it. A state read that never runs does not become a dependency, so unrelated components are not invalidated by the shared selection.

Why can an invisible spinner still cause continuous frames?

Check whether the animation is infinite. Transparency hides the component visually but does not stop an indeterminate animation from invalidating drawing and producing frames.

Does applying the selection before a listener remove listener delay from the tap?

Reordering inside the same main-thread click task saves little because the next frame cannot start until that task returns. Deferring past the frame removes the delay from the tap but changes callback timing, so it is an API decision.