Skip to content

fix(core): skip encoding in-memory shared keys - #258

Open
Tarquinen wants to merge 2 commits into
devfrom
skip-encoding-in-memory-keys
Open

Tarquinen wants to merge 2 commits into
devfrom
skip-encoding-in-memory-keys

Conversation

@Tarquinen

@Tarquinen Tarquinen commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

The app could freeze while a session's transcript updated (Play ANR: main thread waiting on garbage collection). Every change to an in-memory shared value, such as the transcript cache, turned the whole value into JSON on the main thread and kept that copy. Nothing ever read the copy back. With up to 16 cached sessions and inline images, one change could allocate tens of MB, and phones with a small heap stalled on GC.

Now in-memory keys are never encoded. The value stays in its shared reference, as with iOS's in-memory keys. The transcript cache still saves to disk on its own background writer, and leaving the app now flushes that writer on IO too instead of encoding on the main thread.

  • Part of Freeze: Input dispatching timed out in [libart.so] art::gc::Heap::WaitForGcToCompleteLocked #244: this addresses the reports there that freeze while encoding the transcript cache (6 of the 13 I read). The rest, like the Markdown drawing sample in the issue, freeze in ordinary allocations once the heap is full, so the issue stays open.
  • Verification: a new test writes an in-memory key through the live storage and counts encodes. It fails on dev (2 encodes) and passes here. scripts/check.sh passes.
  • No media, because nothing visible changes.

Every write to an in-memory shared key JSON-encoded the whole value on
the calling thread, and the live storage kept that encoding, which
nothing ever read back: a reference loads its bytes only once, when it
is created, and references stay cached per key for the life of the
process. For the transcript cache (up to 16 sessions, with inline images
as base64) that meant tens of MB of allocation on the main thread on
every change. On phones with a tight heap the main thread then waited on
GC long enough to freeze (Play ANR in Heap::WaitForGcToCompleteLocked).

LiveSharedBytes now does nothing for InMemory keys. The value itself
stays in SharedReference, as with iOS's in-memory keys, and the
transcript cache's disk copy is still written by TranscriptCacheWriter.

Verified: new LiveSharedStorageTests.anInMemoryValueIsNeverEncoded
failed before the change (2 encodes) and passes after it. Full
scripts/check.sh on the VM passes. The first full run hit a 50 ms timing
limit in AgentFileLoadingTests (50.002 ms) on the busy VM; that test
passed alone and the second full run passed.

Part of #244
Leaving the app flushed a pending transcript cache write on the main
thread, encoding the whole cache there, before the effect switched to IO.
Both flushes now run inside the IO block, still in the same order, as
iOS awaits them off the main thread. Nothing relies on the file being
written before the effect's first suspension: the pause flush is the
only caller and no test asserts it.

Also scopes the shared-storage comments and the core parity row: live
storage encodes file keys on its writer thread, AppStorage and Keychain
keys on the caller, and never encodes in-memory keys; the test store
still encodes every key.

Verified: full scripts/check.sh on the VM passes, including
:feature:drawer tests.
@Tarquinen
Tarquinen force-pushed the skip-encoding-in-memory-keys branch from 1a92047 to 55c76e6 Compare November 21, 2026 05:16

This branch has not been deployed

No deployments
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