Repository navigation
Conversation
… of state from ViewModels, particulary in debug
- Queue Normal-mode reducers in MockableMavericksStateStore so listeners receive accurate state transitions and mode switches stay synchronized - Drain channels via tryReceive in CoroutinesStateStore.flushQueuesOnceBlocking to prevent deadlocks under FORCE_SYNCHRONOUS_STATE_STORES - Synchronize MutableStateChecker.onStateChanged to prevent torn reads across threads - Await async state callbacks in MockableMavericksStateStoreTest Normal-mode tests Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Agent: GitHub Copilot/cli v1.0.99-2
Extract helper functions in buildPropertyIndexMap and parseDataClassConstructorParamNames to satisfy NestedBlockDepth, and collapse nested if statements to satisfy CollapsibleIfStatements. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Agent: GitHub Copilot/desktop v1.0.99-2
DGuerrero
reviewed
Oct 21, 2026
…sStateStore KDoc - Use Dispatchers.Unconfined for storeContextOverride in CollectAsStateTest.setUp so state updates complete before waitForIdle() returns - Update MockableMavericksStateStore class and addOnStateSetListener KDoc to document queued Normal-mode reducer application and callback threading Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Agent: GitHub Copilot/desktop v1.0.99-2
Count down `secondThreadReady` before calling `store.set`, wait for the second thread to block on `flushLock`, and assert that the second reducer was drained by the first thread during the overlapping `set` call. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes performance bottlenecks and recomposition issues across the
MavericksViewModel→MavericksRepository→MavericksStateStore→ ComposecollectAsStatepipeline that caused UI lag when driving frequent updates (such asText/TextFieldin Compose), while preserving all debug-mode safety guarantees (PureReducerValidationandMutableStateChecker).Root Causes & Changes Made
1.
MockableMavericksStateStore&MockableMavericksViewModelConfig(mvrx-mocking)MockableMavericksStateStore.setpreviously executedstate.stateReducer()synchronously on the calling (Main) thread, then calledrealImmediateStore.set(stateReducer)(runningstateReducera 2nd time on the Main thread), and then calledrealStore.set(stateReducer)(runningstateReducera 3rd time onflushDispatcher). Because debug mode wraps each reducer to run twice and computehashCode()twice over the entire state tree, everysetStateran the user's reducer 6x and computed full-statehashCode()6x (with 4 of those 6 blocking the Main thread).mockBehavior.stateStoreBehavior == StateStoreBehavior.Normal,MockableMavericksStateStore.setnow queuesstateReducerinpendingNormalReducersunderlockand applies it once onrealStore's flush thread (or drains pending reducers underlockprior to a mode switch ornext), updatingrealImmediateStore,scriptableStore, andonStateSetListenerswith the computed state.newStateis computed once and passed as{ newState }torealImmediateStore,scriptableStore, andrealStore.MockMavericksViewModelConfigFactory.buildConfigto passcontextOverride = storeContextOverridetoMockableMavericksStateStore.2.
MavericksComposeExtensions(mvrx-compose)VM.collectAsState(mapper)andVM.collectAsStateWithLifecycle(mapper)wroteval updatedMapper by rememberUpdatedState(mapper)and then readupdatedMappersynchronously insideinitial = withState(this) { updatedMapper(it) }during the same composition.initialwas notremembered,withState(this) { ... }ran on every recomposition (allocating awithStateChannelcallback and re-evaluatingmapperorprop1.geteven for unrelated recompositions).mappercaptured any local variable (creating a new lambda instance on recomposition), writing and readingupdatedMapperin the same composition immediately invalidated the currentRecomposeScope, causing an infinite recomposition loop.initialValue = remember(this, key) { withState(this) { mapper(it) } }(andremember(this, prop1) { withState(this) { prop1.get(it) } }) soinitialValueis only computed on initial composition or key change and does not readupdatedMapperduring composition.mappedFlowonremember(this, key)/remember(this, prop1)so changing theViewModelinstance updates the collected flow.3.
CoroutinesStateStore&MutableStateChecker(mvrx-common)CoroutinesStateStore.flushQueuesOnce()allocated aSelectImplementationstate machine on every singlesetStateemission even when multiple updates were already queued insetStateChannel.MutableStateCheckerallocated a newStateWrapper(which immediately computedstate.hashCode()) on everyonStateChangedcall and recomputedhashCode()even whennewState === previousState.setStateChannel.onReceive(andflushQueuesOnceBlocking()), drain any additional pendingsetStateChannelreducers viatryReceive()in a loop whilescope.isActive, preserving all state emission andwithStateordering invariants.StateWrapperallocations inMutableStateChecker, synchronizedonStateChanged, and skippedhashCode()recalculation whennewState === previousState.Performance Benchmark Results (Before vs. After)
1. State Store Throughput & Main-Thread Latency
Setup:
MavericksStatecontainingtext: String,counter: Int, anditems: List<BenchmarkItem>(200 items).setState { copy(text = newText, counter = counter + 1) }calls followed byawaitState().setState+awaitState()round-trips.debug=true, mocks=true)137.23 ms0.80 msdebug=true, mocks=true)142.95 ms36.95 msdebug=true, mocks=true)setState30,000(6x)10,000(2x)debug=true, mocks=true)hashCode()calls6,000,0002,000,000debug=true, mocks=false)0.42 ms0.67 ms< 1 ms)debug=true, mocks=false)hashCode()10,000(2x) /2M10,000(2x) /2M0StateWrapperallocs)debug=false, mocks=false)0.41 ms0.95 ms< 1 ms)debug=false, mocks=false)15.32 ms20.07 ms~0.004 ms/update(1xreducer,0hashCode())debug=false, mocks=false)19.22 ms(0.019 ms/op)24.21 ms(0.024 ms/op)~0.02 ms/op2. Compose
collectAsStateText UpdatesSetup: Compose screen with 4 property subscriptions (
collectAsState(State::prop)) andcollectAsState { state -> ... }mapper subscriptions, running 200 sequentialvm.setText("Hello world $i")updates withcomposeTestRule.waitForIdle().collectAsState { mapper }212.51 ms(1.06 ms/op)170.06 ms(0.85 ms/op)collectAsState { mapper }392(~2xper update)200(1xper update)collectAsState { mapper }53,591+recompositions, 60s timeout)174.93 ms(0.87 ms/op),1recomposition/update