perf: memoize transcript pipeline + fix streaming lockups - #15
Open
robbe1912 wants to merge 3 commits into
Open
Conversation
Expo SDK 54+ removed --non-interactive. Scripts failed when run from a TTY. Set CI=1 in spawnSync env to force non-interactive mode.
Wave 1 (chat-cards, chat-content): - TranscriptMessage wrapped in React.memo with areEqual comparator keyed on entry ref + copied/speaking/canSpeak. Non-streaming cells skip re-render during token deltas. - maintainVisibleContentPosition extracted to module constant. - extraData wrapped in useMemo. - TranscriptSkeleton placeholder (3 pulsing bubbles, native driver) wired into FlashList ListEmptyComponent during initial load. Wave 2 (format, selectors, provider): - toTranscriptEntry: WeakMap cache keyed on record ref. Skips heavy if/else chain for unchanged records. - getSessionPreviewById: WeakMap cache keyed on messages array ref. Skips per-session getHistoryPreview recompute. - refreshMessages: merge new fetch with previous by record id. Preserves record refs AND array ref when content unchanged. Equality covers BOTH parts and info (catches post-hoc info.error additions during streaming-failure scenarios). - MarkdownText: tokenizer wrapped in useMemo + memo wrap. Net effect: during a token delta, only the one streaming cell re-renders. Previously every parent state change re-rendered all visible cells.
- Collapse leading '&&' operator to trailing style (0 other instances of leading '&&' chains in the repo; rest of file uses single-line or trailing operator). - Drop 'Wave 1' / 'Wave 2.2' plan nomenclature from comments — internal planning artifact absent from repo docs. Replaced with descriptive references to the actual optimizations. - Remove stray double blank line between TranscriptSkeleton memo and ChatContentProps type declaration. - Document the new cross-layer invariant in docs/state-and-data.md Important Data Invariants section: SessionMessageRecord objects in messagesBySession are never mutated in place. The WeakMap caches in format.ts and opencode-provider-selectors.ts depend on this contract; refreshMessages preserves refs when content is unchanged.
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.
Problem
The transcript list re-renders every visible cell on every parent state change during streaming. Token deltas from SSE cause
isRefreshingMessagestoggles,pendingInteractionscount updates,runningflips — each one re-renders all visibleTranscriptMessagerows even though only the streaming row changed. Result: UI lockups during assistant replies, especially on lower-end devices.Fix
Memoize the transcript pipeline end-to-end so non-streaming cells skip re-render during token deltas.
Wave 1 — cell-level memoization
components/chat/chat-cards.tsx: wrapTranscriptMessageexport inReact.memowithareEqualcomparator keyed on entry ref +copied/speaking/canSpeak. Callback identity intentionally excluded — they only fire on user action.components/chat/chat-content.tsx: extractmaintainVisibleContentPositionto a module-levelas const; memoizeextraDataviauseMemo([copiedMessageId, speakingMessageId]). AddTranscriptSkeletonplaceholder (3 pulsing bubbles, native-driver opacity loop) wired into FlashListListEmptyComponentduring initial load.Wave 2 — pipeline memoization
lib/opencode/format.ts:WeakMap<record, entry>cache intoTranscriptEntry. Skips heavy if/else tokenization chain for unchanged records.providers/opencode-provider-selectors.ts:WeakMapcache ingetSessionPreviewByIdkeyed on messages array ref.providers/opencode-provider.tsx:refreshMessagesmerges new fetch with previous by record id, preserving record refs AND array ref when content unchanged. Equality covers BOTH parts and info (catches post-hocinfo.erroradditions during streaming-failure scenarios).components/chat/chat-markdown.tsx: tokenizer wrapped inuseMemo+memowrap.Style pass
&&operator chain to trailing style (matches rest of codebase).chat-content.tsx.SessionMessageRecordimmutability invariant indocs/state-and-data.md§ Important Data Invariants.Build script fix
scripts/build-android-development.mjs+scripts/build-android-release.mjs: replaced deprecated--non-interactiveflag withCI=1env var. Expo SDK 54+ removed the flag; scripts failed when run from a TTY.Files changed
components/chat/chat-cards.tsxcomponents/chat/chat-content.tsxcomponents/chat/chat-markdown.tsxcomponents/chat/chat-view-styles.tslib/opencode/format.tsproviders/opencode-provider.tsxproviders/opencode-provider-selectors.tsscripts/build-android-development.mjsscripts/build-android-release.mjsdocs/state-and-data.mdValidation
npm run typecheck✓npm run lint✓npm run test:format✓npm run test:usage✓npm run test:fake-server:self✓Net effect
During a streaming token delta, only the one streaming cell re-renders. Previously every parent state change re-rendered all visible cells. During initial session load, a
TranscriptSkeletonplaceholder shows pulsing bubbles instead of a blank snap-to-populated.