perf(server): SQL transactions stop copying every service twice #41379
main fromrefs/pull/41379/head·merged by juliusmarminge View on GitHubEvery 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.
| metric | before | after | change | 95% interval | |
|---|---|---|---|---|---|
| streaming.cpuPct | 36.5 | 34.3 | -6% | -2.7 to 6.1 | |
| streaming.wakeupsPerSec | 940.9 | 815.7 | -13% | -188 to -4.1 | |
| streaming.rssPeakMB | 822 | 773 | -6% | -91 to -1 | |
| streaming.heapAfterGcMB | 192.7 | 192.9 | +0% | -0.3 to 1.2 | |
| streaming.loopDelayP99Ms | 5.2 | 5.7 | +10% | -3.1 to 3.5 | |
| streaming.gcCount | 129 | 93 | -28% | -41 to -33 | better |
| streaming.gcPauseMs | 575.9 | 441.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
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
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.patchThe 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 to55849· assessed changes only-
Path filters excluded changed files. This rating applies only to the assessed changes.
Pre-merge checks:
4-
Check name Status Explanation Title check 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 … Description check 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 … Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check 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-
- Source mode: Auto
- Repository: pingdotgg/t3code
- Branch: t3/perf-sql-transaction-context
- File: .coderabbit.config.ts
- Commit: 5584981
- Layers: Repository TS config
- Review profile: CHILL
- Plan: Team
- Run ID:
1eabd6d1-2abc-4b10-8891-b00db6a3a654
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.yamlis 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.
-
-
juliusmarminge merged this977b7c7
juliusmarminge closed this
Sign in to join this conversation.
Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Other scenarios#
All 64 scenario measurements match
main.All 64 scenario measurements
short turn resumeWebSocket decodedshort turn resumeWebSocket messageslong turn running, cold openHTTP decodedlong turn running, cold openrowslong turn running, history pageHTTP decodedlong turn running, history pagerowslong turn finished, cold openHTTP decodedlong turn finished, cold openrowslong turn finished, socket cold openWebSocket decodedlong turn finished, socket cold openWebSocket messageslong turn finished, socket cold openrowslong turn finished, work pageHTTP decodedlong turn finished, work pagerowsidle thread resumeWebSocket decodedidle thread resumeWebSocket messagesfleet shell snapshotHTTP decodedfleet shell snapshotrowsfleet shell subscribeWebSocket decodedfleet shell subscribeWebSocket messagesfleet shell resume checkHTTP decodedfleet shell resume checkrowsstale shell reloadHTTP decodedstale shell reloadrowsstale shell resumeWebSocket decodedstale shell resumeWebSocket messagesstale todo list cold openHTTP decodedstale todo list cold openrowsstale todo list cold opennodesstale todo list cold opensubagentsstale todo list cold openplansstale todo list cold openproviderTurnsstale todo list cold opencheckpointsshort turn resumeWebSocket decodedshort turn resumeWebSocket messageslong turn running, cold openHTTP decodedlong turn running, cold openrowslong turn running, history pageHTTP decodedlong turn running, history pagerowslong turn finished, cold openHTTP decodedlong turn finished, cold openrowslong turn finished, socket cold openWebSocket decodedlong turn finished, socket cold openWebSocket messageslong turn finished, socket cold openrowslong turn finished, work pageHTTP decodedlong turn finished, work pagerowsidle thread resumeWebSocket decodedidle thread resumeWebSocket messagesfleet shell snapshotHTTP decodedfleet shell snapshotrowsfleet shell subscribeWebSocket decodedfleet shell subscribeWebSocket messagesfleet shell resume checkHTTP decodedfleet shell resume checkrowsstale shell reloadHTTP decodedstale shell reloadrowsstale shell resumeWebSocket decodedstale shell resumeWebSocket messagesstale todo list cold openHTTP decodedstale todo list cold openrowsstale todo list cold opennodesstale todo list cold opensubagentsstale todo list cold openplansstale todo list cold openproviderTurnsstale todo list cold opencheckpointsBaseline:
f9f34aa· PR result:5584981· 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.
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.