Skip to content

perf(shared): lazily materialize RPC contract Zod v4 schemas on first use - #49897

Merged
nwparker merged 1 commit into
mainfrom
perf/lazy-rpc-param-zod-schemas
Nov 26, 2026
Merged

nwparker merged 1 commit into
mainfrom
perf/lazy-rpc-param-zod-schemas

Conversation

@nwparker

@nwparker nwparker commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Defers Zod v4 schema construction across 151 src/shared/rpc-contract/ files (~880 schemas) until first property access so unused RPC contracts do not eagerly allocate thousands of live Zod v4 schema, closure, and traits Set objects at startup:

  1. src/shared/schema/lazy-zod-schema.ts
    • Adds lazyZodSchema<T extends ZodType>(factory: () => T): T backed by a Proxy + WeakMap<object, () => ZodType> resolver, preserving === object identity (required by generate-rpc-params-catalog.mjs), z.infer, instanceof ZodType, and full Zod v4 composition (extend, partial, discriminatedUnion, nested objects).
    • Adds unwrapLazyZodSchema so zod.compile always clones and AOT-compiles the real underlying ZodType instance.
  2. src/main/schema-validation/zod-schema-compilation.ts
    • Unwraps lazy schemas via unwrapLazyZodSchema(schema) before compile() in getCompiledSchema.
  3. src/shared/rpc-contract/**/*.ts (151 files)
    • Wraps top-level Zod schema definitions in lazyZodSchema(() => ...).

Performance

Measured on an isolated clone of a 1,280-worktree heavy profile (xray-t75-full.mjs) and a standalone Zod v4 microbenchmark (1,000 eager z.object = 22.16 MiB vs 1,000 lazyZodSchema = 0.46 MiB, -98%):

  • Main V8 heapUsed (t+75s): 90.30 MiB → 86.34 MiB (-3.96 MiB)
  • Main Process Footprint (t+75s): 280.82 MiB → 260.21 MiB (-20.61 MiB)
  • Renderer Process Footprint (t+75s): 219.49 MiB → 211.14 MiB (-8.35 MiB)
  • Zero negative user-facing tradeoffs: first invocation of any RPC method materializes its schema once in ~0.02ms and caches both the schema and its zod.compile fast path.

Verification

  • pnpm check:changed --base=perf/cache-unchanged-list-environments-stat: 29/29 passed (including rpc params catalog, typecheck, unit tests, changed-code quality)
  • pnpm run build:electron-vite: succeeded
Files Added Deleted Net
Test 0 0 0 0
Prod 153 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​7403 $\color{#cf222e}{\Huge{\mathbf{−}}}$​5854 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​1549

@nwparker
nwparker changed the base branch from perf/cache-unchanged-list-environments-stat to main November 26, 2026 12:29
@nwparker
nwparker force-pushed the perf/lazy-rpc-param-zod-schemas branch from 7fa2658 to 7443d37 Compare November 26, 2026 12:32
@nwparker
nwparker merged commit e26d6d5 into main Nov 26, 2026
51 of 52 checks passed
@nwparker
nwparker deleted the perf/lazy-rpc-param-zod-schemas branch November 26, 2026 12:33
joei4cm added a commit to joei4cm/orca that referenced this pull request Nov 27, 2026
stablyai#49406 added this schema bare on top of stablyai#49897's lazy migration, leaving
the eager-schema ratchet one over baseline: every PR touching src/main or
src/shared now fails the ratchets job. Wrap it like its siblings.

Co-Authored-By: Claude Code <[email protected]>
DoozyX added a commit to DoozyX/orca that referenced this pull request Nov 27, 2026
Merges stablyai/orca 7030f25 (320 commits) into fork main 3e499a0.

Conflict resolutions:
- Antigravity usage (stablyai#50137): upstream stops reading agy usage for account
  safety. Carried into the fork's per-account status bar: dropped the
  Antigravity usage menu, accounts prop and checked-item setup flag from
  AccountUsageDetailsMenu, StatusBarUsageArea, the controller and provider
  visibility (and its fork-retained test); the omp slot stays, including in
  the rate-limit full cycle and the usage-tracking doc.
- Lazy Zod schemas (stablyai#49897): took upstream's lazyZodSchema wrapping and kept
  the fork's ProjectGroupUpdate.claudeAccountId, the omp status-bar item and
  _ompStatusBarDefaultAdded, and the federated release receipt's
  currentOwnerDispatchId/terminalReleaseState. Fork-only omp usage and
  AccountHome schemas are now lazy too; baseline pruned. Feature-interaction schemas
  stay in the fork's feature-interaction-params.ts, now lazy too.
- Git graph: openGitGraph keeps the fork's options object (afterOpen) and
  takes upstream's placementWorkspaceId (stablyai#49629, stablyai#50017) as an option;
  callers and upstream's test use it. The fork's branches/tags dock passes
  the folder placement too (same bug as stablyai#49597), and it and the graph tab
  share a new useGitGraphRefsEpoch hook (keeps git-graph-tab under 400). Source control keeps the fork's
  branches/tags dock beside upstream's file-list header; fork git-graph
  rendering tests pass the new tabId.
- Claude background tasks: upstream skips ambient artifact monitors; the
  fork's monitored-work rows and untyped-row handling stay.
- Claude managed hook: local POSIX keeps the fork's runtime-home wrapped
  command; getPosixManagedEventHook takes the command, so upstream's
  PreToolUse timeout applies to both local and remote entries.
- local-worker-start: keep the fork's reminted pane handle (#29) and add
  upstream's retained-chat assertStarting and launch warnings.
- Mobile web bundle fence: keep the fork's 11.25 MiB (above upstream's
  11 MiB + 16 KiB).
- en.json: union of the fork's ProjectGroupSettingsDialog and upstream's
  AutoNameFailed keys.
- Skills: regenerate bundled guides (upstream's shared CLI paragraph), union
  the add-only digest ledger, regenerate manifest and sources.
- tracked-upstream-cache test: fixed far-future expiry instead of
  Date.now() for upstream's new wall-clock ratchet.

Claude-Session: https://claude.ai/code/session_01BPjp5wba1X5fDbTK4YStHM
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