Skip to content

perf(settings): map settings once off the main thread - #645

Merged
Sahank29 merged 1 commit into
mainfrom
perf/settings-flow-off-main
Nov 15, 2026
Merged

Sahank29 merged 1 commit into
mainfrom
perf/settings-flow-off-main

Conversation

@Sahank29

Copy link
Copy Markdown
Contributor

Problem

Follow-up to #644, which was merged before this commit landed on its branch.

On a CPH2649 (Snapdragon 8 Elite, release build), every cold start had 3 frames of 450–700 ms. Frame stats put all of that time before the frame starts (vsync → HandleInputStart), not in drawing. A scheduler trace shows the main thread running flat out for 439, 624 and 520 ms at those moments, in code with no trace sections. A method-sampling trace of the debuggable build attributes the work outside frames to SettingsPreferences.settings mapping (map…emit + collect + Keys init). The same burst likely explains the freeze when opening the app after the phone sleeps: work held back while the app was frozen runs together on unlock.

Change

SettingsPreferences.settings was a cold flow. Each of ~60 collectors, mostly on Main, re-ran the ~100-key mapping and the addons JSON parse on every write to the DataStore. It now maps once per change on Dispatchers.Default, skips unchanged values, and shares the latest one (shareIn, replay = 1). The type stays Flow<MiscSettings>, so callers are unchanged; first() and drop(1) behave as before.

Not verified

Compiles (Kotlin step) but has not been built into an APK or run on a device: local builds ran out of memory. Please check on device before merging: cold start, then lock for 5 min and reopen.

settings was a cold flow: each of ~60 collectors, mostly on Main,
re-ran the ~100-key mapping and the addons JSON parse on every write
to the DataStore. On a cold start that showed up as 450-620 ms bursts
of main-thread work outside any frame. Map once per change on
Default, drop unchanged values and share the latest one.
@Sahank29
Sahank29 merged commit 7070bb2 into main Nov 15, 2026
1 check passed
@Sahank29
Sahank29 deleted the perf/settings-flow-off-main branch November 15, 2026 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant