Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(server): SQL transactions stop copying every service twice #41379

Mergedjuliusmarmingemerged 1 commit intomain fromrefs/pull/41379/headmerged by juliusmarminge View on GitHub

juliusmarmingecommented

Every SQL transaction and savepoint copied the server's whole service map twice. Effect's SqlClient.withTransaction captures the fiber's context, extends it with the transaction's services, and provides that with Effect.provideContext, which merges the fiber's context into a copy of the extension. With 5 threads streaming, those copies were about 45% of everything the server allocated (536 of 1,178 MB sampled in 20 s). Provider commit batches open a savepoint per snapshot, so the cost grows with streaming threads.

When the fiber's context is still the one captured, the extended context already is the merge result. The patch to effect@4.1.5 provides it directly in that case and keeps the merge otherwise. Services resolve exactly as before.

Evidence

scripts/server-metrics.ts ab --only streaming --rounds 3 (harness from #41282), 5 threads streaming, before 1368854828e2 and after 5584981290c0, linux-x64. The machine was shared with other benchmarks, so CPU sits inside the noise.

metricbeforeafterchange95% interval
streaming.cpuPct36.534.3-6%-2.7 to 6.1
streaming.wakeupsPerSec940.9815.7-13%-188 to -4.1
streaming.rssPeakMB822773-6%-91 to -1
streaming.heapAfterGcMB192.7192.9+0%-0.3 to 1.2
streaming.loopDelayP99Ms5.25.7+10%-3.1 to 3.5
streaming.gcCount12993-28%-41 to -33better
streaming.gcPauseMs575.9441.2-23%-360.6 to 34.3

Sampled allocation profiles, one streaming run each, 20 s window: 1,178 MB on main and 836 MB with the patch (−29%). Map copies from Context fell from 660 MB to 330 MB. What remains is mostly ProviderCommitBatch's provideContext(caller) per snapshot, whose caller context is fresh for every snapshot so it cannot be memoized safely, and the one flattening copy Context.add makes at the overlay depth cap.

Tests

No new test: this is a dependency patch, and the merge it skips returns the same context. vp run check:changed passes, including the unused-patches check. vp test run src/orchestration-v2/EventSink.test.ts src/orchestration-v2/ProviderCommitBatch.test.ts src/persistence passes (40 files, 114 tests).

The same change would fit upstream in Effect's SqlClient.

Written by Claude Opus 5.5, in Claude Code, run from T3 Code.

🤖 Generated with Claude Code

    1. Thread transfer impact

      ✅ Thread transfer remains within every enforced ceiling.

      ProviderMetricMain baselineThis PRImpactPR ceiling
      CodexTotal thread wire5.0 KiB5.0 KiB0 B (0.0%)6.8 KiB✅
      CodexThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      CodexLive turn WebSocket wire1.2 KiB1.2 KiB0 B (0.0%)2.0 KiB✅
      CodexLive turn WebSocket decoded21.5 KiB21.5 KiB0 B (0.0%)24.4 KiB✅
      CodexLive turn messages110 (0.0%)8✅
      ClaudeTotal thread wire5.0 KiB5.0 KiB0 B (0.0%)6.8 KiB✅
      ClaudeThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      ClaudeLive turn WebSocket wire1.2 KiB1.2 KiB0 B (0.0%)2.0 KiB✅
      ClaudeLive turn WebSocket decoded21.9 KiB21.9 KiB0 B (0.0%)24.4 KiB✅
      ClaudeLive turn messages110 (0.0%)8✅

      Other scenarios

      All 64 scenario measurements match main.

      All 64 scenario measurements
      ProviderScenarioMetricMain baselineThis PRImpactPR ceiling
      Codexshort turn resumeWebSocket decoded10.5 KiB10.5 KiB0 B (0.0%)12.7 KiB✅
      Codexshort turn resumeWebSocket messages220 (0.0%)2✅
      Codexlong turn running, cold openHTTP decoded281.7 KiB281.7 KiB0 B (0.0%)302.7 KiB✅
      Codexlong turn running, cold openrows75750 (0.0%)75✅
      Codexlong turn running, history pageHTTP decoded92.4 KiB92.4 KiB0 B (0.0%)107.4 KiB✅
      Codexlong turn running, history pagerows75750 (0.0%)75✅
      Codexlong turn finished, cold openHTTP decoded381.5 KiB381.5 KiB0 B (0.0%)410.2 KiB✅
      Codexlong turn finished, cold openrows1511510 (0.0%)151✅
      Codexlong turn finished, socket cold openWebSocket decoded381.6 KiB381.6 KiB0 B (0.0%)410.2 KiB✅
      Codexlong turn finished, socket cold openWebSocket messages220 (0.0%)2✅
      Codexlong turn finished, socket cold openrows1511510 (0.0%)151✅
      Codexlong turn finished, work pageHTTP decoded186.2 KiB186.2 KiB0 B (0.0%)200.2 KiB✅
      Codexlong turn finished, work pagerows1501500 (0.0%)150✅
      Codexidle thread resumeWebSocket decoded65 B65 B0 B (0.0%)200 B✅
      Codexidle thread resumeWebSocket messages110 (0.0%)1✅
      Codexfleet shell snapshotHTTP decoded42.4 KiB42.4 KiB0 B (0.0%)53.7 KiB✅
      Codexfleet shell snapshotrows27270 (0.0%)27✅
      Codexfleet shell subscribeWebSocket decoded65 B65 B0 B (0.0%)200 B✅
      Codexfleet shell subscribeWebSocket messages110 (0.0%)1✅
      Codexfleet shell resume checkHTTP decoded18 B18 B0 B (0.0%)100 B✅
      Codexfleet shell resume checkrows0000✅
      Codexstale shell reloadHTTP decoded42.4 KiB42.4 KiB0 B (0.0%)53.7 KiB✅
      Codexstale shell reloadrows27270 (0.0%)27✅
      Codexstale shell resumeWebSocket decoded140 B140 B0 B (0.0%)400 B✅
      Codexstale shell resumeWebSocket messages220 (0.0%)2✅
      Codexstale todo list cold openHTTP decoded72.4 KiB72.4 KiB0 B (0.0%)83.0 KiB✅
      Codexstale todo list cold openrows75750 (0.0%)75✅
      Codexstale todo list cold opennodes220 (0.0%)4✅
      Codexstale todo list cold opensubagents220 (0.0%)4✅
      Codexstale todo list cold openplans110 (0.0%)2✅
      Codexstale todo list cold openproviderTurns110 (0.0%)2✅
      Codexstale todo list cold opencheckpoints110 (0.0%)2✅
      Claudeshort turn resumeWebSocket decoded10.6 KiB10.6 KiB0 B (0.0%)12.7 KiB✅
      Claudeshort turn resumeWebSocket messages220 (0.0%)2✅
      Claudelong turn running, cold openHTTP decoded283.3 KiB283.3 KiB0 B (0.0%)302.7 KiB✅
      Claudelong turn running, cold openrows75750 (0.0%)75✅
      Claudelong turn running, history pageHTTP decoded93.4 KiB93.4 KiB0 B (0.0%)107.4 KiB✅
      Claudelong turn running, history pagerows75750 (0.0%)75✅
      Claudelong turn finished, cold openHTTP decoded384.2 KiB384.2 KiB0 B (0.0%)410.2 KiB✅
      Claudelong turn finished, cold openrows1511510 (0.0%)151✅
      Claudelong turn finished, socket cold openWebSocket decoded384.3 KiB384.3 KiB0 B (0.0%)410.2 KiB✅
      Claudelong turn finished, socket cold openWebSocket messages220 (0.0%)2✅
      Claudelong turn finished, socket cold openrows1511510 (0.0%)151✅
      Claudelong turn finished, work pageHTTP decoded188.1 KiB188.1 KiB0 B (0.0%)200.2 KiB✅
      Claudelong turn finished, work pagerows1501500 (0.0%)150✅
      Claudeidle thread resumeWebSocket decoded66 B66 B0 B (0.0%)200 B✅
      Claudeidle thread resumeWebSocket messages110 (0.0%)1✅
      Claudefleet shell snapshotHTTP decoded43.0 KiB43.0 KiB0 B (0.0%)53.7 KiB✅
      Claudefleet shell snapshotrows27270 (0.0%)27✅
      Claudefleet shell subscribeWebSocket decoded66 B66 B0 B (0.0%)200 B✅
      Claudefleet shell subscribeWebSocket messages110 (0.0%)1✅
      Claudefleet shell resume checkHTTP decoded18 B18 B0 B (0.0%)100 B✅
      Claudefleet shell resume checkrows0000✅
      Claudestale shell reloadHTTP decoded43.0 KiB43.0 KiB0 B (0.0%)53.7 KiB✅
      Claudestale shell reloadrows27270 (0.0%)27✅
      Claudestale shell resumeWebSocket decoded140 B140 B0 B (0.0%)400 B✅
      Claudestale shell resumeWebSocket messages220 (0.0%)2✅
      Claudestale todo list cold openHTTP decoded73.4 KiB73.4 KiB0 B (0.0%)83.0 KiB✅
      Claudestale todo list cold openrows75750 (0.0%)75✅
      Claudestale todo list cold opennodes220 (0.0%)4✅
      Claudestale todo list cold opensubagents220 (0.0%)4✅
      Claudestale todo list cold openplans110 (0.0%)2✅
      Claudestale todo list cold openproviderTurns110 (0.0%)2✅
      Claudestale todo list cold opencheckpoints110 (0.0%)2✅

      Baseline: f9f34aa · PR result: 5584981 · Source CI: success

      Scenario 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.

    2. Explain Change →

      No actionable comments were generated in the recent review.

      Walkthrough

      The transaction and savepoint path now uses a helper to select the transaction context or merge it with the current context.

      Changes: Transaction Context Handling

      Layer / File(s)Summary
      Select and apply transaction context
      patches/effect@4.1.5.patch
      The helper uses the transaction context directly when the current context is the original services context. Otherwise, it merges the contexts. The transaction and savepoint path calls this helper. The generated JavaScript includes the corresponding changes.

      Estimated code review effort: 2 (Simple) | ~10 minutes

      Change: Refactor

      Merge Risk: Minimal · up to 55849 · assessed changes only

      Path filters excluded changed files. This rating applies only to the assessed changes.

      Pre-merge checks: Passed 4

      Check nameStatusExplanation
      Title checkPassedThe title names the main change: SQL transactions and savepoints no longer copy the service map twice. It uses a conventional commit prefix. The title is specific and matches the summary of the patch …
      Description checkPassedThe description covers the problem, the change, the evidence, and the tests. It explains that SqlClient.withTransaction copies the service map twice, and it describes the conditional fix. It includes …
      Linked Issues checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck skipped because no linked issues were found for this pull request.
      Full details: Title check

      Explanation

      The title names the main change: SQL transactions and savepoints no longer copy the service map twice. It uses a conventional commit prefix. The title is specific and matches the summary of the patch to effect@4.1.5, which provides the extended context directly when the fiber context is unchanged.

      Full details: Description check

      Explanation

      The description covers the problem, the change, the evidence, and the tests. It explains that SqlClient.withTransaction copies the service map twice, and it describes the conditional fix. It includes benchmark data, allocation profile numbers, and test commands. It also names the model and harness, as the template asks. The Scope section is weak: it does not link an issue that shows the bug, and it does not explain why no issue is needed. The author also presents the change as a performance improvement, while the template says to accept bug fixes, not features. The description does not state whether this is a bug fix. The template asks for a short recording or screenshots only for UI changes, so their absence is acceptable.

      Resolution

      Add a Scope note. Link the issue that shows the bug, or explain why a small fix needs no issue. Also state whether this is a bug fix or a performance improvement, since the template accepts bug fixes only.

      Finishing Touches

      Generate unit tests (beta)

      • Commit to this branch
      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
      Recent review info

      Run configuration

      Commits

      Reviewing files that changed from the base of the PR and between f9f34aab7807168a0fdae1c9d447eb271ec5f229 and 5584981290c09b0003a9b0e41dcff983aba0307a.

      Files ignored due to path filters: 1

      • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
      Files selected for processing: 1

      • patches/effect@4.1.5.patch

      Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.

    3. juliusmarminge merged this977b7c7

    4. juliusmarminge closed this

Sign in to join this conversation.