perf(txn): avoid default event callback allocations#25710
Conversation
2efbb14 to
90cd31f
Compare
90cd31f to
54f0738
Compare
XuPeng-SH
left a comment
There was a problem hiding this comment.
Reviewed from lifecycle, concurrency, error-path, compatibility, and performance angles.
The client-owned defaults are initialized before publication and remain immutable; operators share only the read-only container, while the first custom append performs copy-on-write under the operator lock. Default-before-custom ordering, first-error behavior, close-once semantics, RestartTxn cleanup, failed creation, and snapshot/mirror behavior are preserved. The reset path also avoids retaining custom callback backing arrays across operator reuse.
Local validation on 54f0738:
- pkg/txn/client: count=3 PASS
- pkg/txn/client under -race: PASS, including the 50x100 concurrent New/Rollback lifecycle test
- focused callback lifecycle tests under -race count=3: PASS
- pkg/frontend and pkg/sql/compile compile tests: PASS
- allocation benchmarks reproduce 2000 B/12 allocs, 2528 B/17 allocs, and 4088 B/46 allocs for the three documented paths
No blocking correctness or robustness issue found. CI is still running and should remain a merge prerequisite.
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
…t-callbacks # Conflicts: # pkg/txn/client/client.go
What type of PR is this?
Which issue(s) this PR fixes:
issue #25472
What this PR does / why we need it:
Every new transaction currently builds a callback map and slice to install the
two client-owned
ClosedEventcallbacks. Under the 1000-VU point-selectworkload,
AppendEventCallbackaccounted for about 2.65 GB of allocation in a30-second profile.
This change stores the immutable default close callbacks once on
txnClient.Transaction operators share them until a caller adds a custom callback, at
which point the operator allocates its own callback map using copy-on-write.
Default callbacks still run before custom callbacks and preserve first-error,
commit, rollback, and restart behavior.
Changes:
ClosedEventcallbacks;Benchmark results on Apple M1 (
-benchmem, five runs; allocation values werestable across all runs):
Clean remote point-select A/B/A at 1000 VU:
AppendEventCallbackdisappeared from the patched allocation profile withoutan equivalent allocation transfer.
Validation:
go test -count=1 ./pkg/txn/clientgo test -race -count=1 ./pkg/txn/client -run '^(TestEventCallbacksDefaultOrderAndFirstError|TestDefaultEventCallbacksCopyOnWrite|TestClientClosedEventDefaultsRunBeforeCustomOnce|TestRestartTxnRestoresDefaultsAndDropsCustomCallbacks|TestClosedEvent|TestCommitClosedEventWhenWaitRunningSQLFails|TestRollbackClosedEventWhenWaitRunningSQLFails)$'go test -count=1 ./pkg/frontend ./pkg/sql/compilego test -run '^$' -bench '^(BenchmarkTxnNewRollback|BenchmarkTxnNewAppendCallbackRollback|BenchmarkTxnNewAppendTraceCallbacksRollback)$' -benchmem -count=5 ./pkg/txn/client