perf(mobile): reuse encoded rows in shell cache saves #16129
t3code/mobile-perf/lazy-thread-move-availability fromrefs/pull/16129/head·merged by juliusmarminge View on GitHubMobile periodically persists the entire shell snapshot while threads stream. With 1,210 shells and five changing threads, every save re-encoded the other 1,205 rows through the schema codec.
Give each persistence store an encoder with a weak map of successfully encoded row objects. The shell reducer replaces changed rows and preserves unchanged row references, so only cache misses need the canonical row codec. Cold rows still encode in batches of 32 with host yields. Envelope validation and JSON serialization remain unchanged.
Scope and approval#
Julius requested this mobile performance audit and one PR per measured hotspot in the maintainer session. This focused fix removes repeated serialization of unchanged shell rows without changing the stored format or product behavior. It is maintainer-directed work; there is no separate public issue or discussion approval link.
Release simulator evidence#
Same optimized native Release binary, Hermes -O, dev=false. Same 1,210-thread fixture restored before each run, Home visible with settled history expanded, 20 Hz foreground stream plus four 2 Hz background streams for 15 seconds. Both runs emitted 300 + 120 events. Only the two production files in this PR differ in the bundle source maps.
| Inclusive JS samples | Before | After |
|---|---|---|
| Shell-cache encoder stacks | 184 | 9 |
| Encoder share of active samples | 12.0% | 0.6% |
| Schema parser stacks | 266 | 53 |
| Active JS / total samples | 1,531 / 11,330 | 1,574 / 13,611 |
| Capture duration | 20.80 s | 20.86 s |
Measurement details · Before Hermes trace · After Hermes trace
The baseline is the saved trace from the exact parent release bundle at 82d061a. This one pair measures repeated saves after startup, not cold sync. Sampling and generated text vary. Total active JS samples are roughly unchanged, so this is evidence of removing the targeted encoding work, not an overall app-speed or battery claim. There is no visible UI change.
Correctness and tradeoff#
- The reducer's immutable upserts retain other row objects; full snapshots supply fresh objects and encode cold. The weak map is per store, with no TTL, eviction worker, or global cache.
- Validated row encodings are stored only after a successful batch. Failed rows are retried and still rejected. A cancelled save can retain completed valid batches.
- One encoded copy remains reachable per live row. The memory delta has not been measured; removed row keys can be collected. Future in-place mutation of shell rows would invalidate the identity assumption.
- Envelope encoding/stringification still visits the full snapshot.
Fifteen focused tests passed, including byte-identical canonical cold/warm payloads, replacement/addition/archive cases, invalid rows and cold-path cancellation. Targeted lint and formatting passed. The existing concurrency test uses fresh encoders; shared-instance concurrency is supported by immutable row values but has not received a separate test. Both iOS and Android use this mobile store; web/desktop, wire contracts, and providers are unchanged. Unrelated local widget edits are excluded.
Implementation: Claude Opus 5.5 through T3 Code. Integration, review, and release profiling: GPT-6 Astra through the Codex harness.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
juliusmarminge added labelsmacroscope-review
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
Approvability#
Verdict: Approved at
272ed9cMacroscope's review found this PR approvable — This is a contained mobile cache serialization optimization that reuses successful row encodings while preserving canonical cold-path encoding, envelope validation, and stored output. Focused tests cover warm saves, row replacement and archiving, failures, and byte-identical payloads.
Notes:
- Diff unchanged. Approvability was decided on eligibility alone.
No code changes detected at
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).b25dec4. Prior analysis still applies.Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
ℹ️ No successful
mainbaseline artifact is available yet. This run establishes the initial measurement.Provider Metric Main baseline This PR Impact PR ceiling Codex Total thread wire — 5.0 KiB — 6.8 KiB ✅ Codex Thread snapshot wire — 3.8 KiB — 4.9 KiB ✅ Codex Live turn WebSocket wire — 1.2 KiB — 2.0 KiB ✅ Codex Live turn WebSocket decoded — 20.9 KiB — 29.3 KiB ✅ Codex Live turn messages — 2 — 8 ✅ Claude Total thread wire — 5.0 KiB — 6.8 KiB ✅ Claude Thread snapshot wire — 3.8 KiB — 4.9 KiB ✅ Claude Live turn WebSocket wire — 1.2 KiB — 2.0 KiB ✅ Claude Live turn WebSocket decoded — 21.2 KiB — 29.3 KiB ✅ Claude Live turn messages — 2 — 8 ✅ Baseline: unavailable · PR result:
b25dec4· Source CI: successScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
- Codex decoded thread snapshot: 108.5 KiB
- Claude decoded thread snapshot: 108.8 KiB
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.
Important
Review skipped#
We couldn't safely recover the incremental review. No full review was started, and the last reviewed checkpoint was preserved. Retry later, or explicitly request a full review by commenting
@coderabbitai full review.You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
- 🔍 Trigger review
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
- Configuration used: Repository: pingdotgg/t3code/.coderabbit.yaml
- Review profile: CHILL
- Plan: Team
- Run ID:
02c086b8-3cc7-42aa-b45f-afef514cf338
📥 Commits
Reviewing files that changed from the base of the PR and between d5ddcbb1512ec68ed7faa8d33745dd54dc178977 and 272ed9c24a5019881b2a54278900c8adf4ad9bd4.
📒 Files selected for processing (3)
apps/mobile/src/connection/environment-cache-store.test.tsapps/mobile/src/connection/environment-cache-store.tsapps/mobile/src/connection/shell-cache-encoding.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.
📝 Walkthrough
Walkthrough#
Shell snapshot encoding now uses a reusable encoder instance. The encoder caches successfully encoded rows by object identity and encodes uncached rows in chunks. The environment cache store creates the encoder and uses it when saving snapshots.
Changes#
Shell snapshot encoding
Layer / File(s) Summary Cache-aware row encoding
apps/mobile/src/connection/shell-cache-encoding.tsmakeStoredShellSnapshotEncoderreturns an encoder that caches successful row encodings by object identity. It processes uncached rows in chunks and encodes the envelope after row processing.Store integration and encoder tests
apps/mobile/src/connection/environment-cache-store.ts,apps/mobile/src/connection/environment-cache-store.test.tsThe store creates and uses an encoder instance. Tests cover cold and warm encoding, changed and archived rows, timer yields, and retry after a failed encoding. Priority: ➖ Normal
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Refactor
Merge Risk: ⚪ Minimal · up to
272edThe cache change has no established behavior or data-safety issue blocking merge.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
Check name Status Explanation Resolution Description check ⚠️ Warning The description explains the problem, change, scope, verification, and tradeoffs. However, the required scope approval link is missing. It says the work was requested in a maintainer session but provi… Add a link to the triaged issue or maintainer discussion that contains explicit approval of the direction and scope. If the approval occurred in a private session, provide an acceptable reference or document the approval in a way that satis… ✅ Passed checks (4 passed)
Check name Status Explanation Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 3 files. Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request. Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request. Title check ✅ Passed The title clearly and concisely describes the main change: reusing encoded rows during mobile shell-cache saves. Full details: Description check
Explanation
The description explains the problem, change, scope, verification, and tradeoffs. However, the required scope approval link is missing. It says the work was requested in a maintainer session but provides no link to that discussion or explicit approval, and it does not qualify for the template’s obvious-bug exception.
Resolution
Add a link to the triaged issue or maintainer discussion that contains explicit approval of the direction and scope. If the approval occurred in a private session, provide an acceptable reference or document the approval in a way that satisfies the repository’s scope-and-approval requirement.
✨ Finishing Touches
📝 Generate docstrings
- Commit to this branch
- Create a new PR
🧪 Generate unit tests (beta)
- Commit to this branch
- Create a new PR
- Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Comment `@coderabbitai help` to get the list of available commands.juliusmarminge merged this8a1fc41
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes