Skip to content

perf(txn): avoid default event callback allocations - #25710

Merged
mergify[bot] merged 6 commits into
matrixorigin:mainfrom
VioletQwQ-0:perf/txn-default-event-callbacks
Jul 17, 2026
Merged

mergify[bot] merged 6 commits into
matrixorigin:mainfrom
VioletQwQ-0:perf/txn-default-event-callbacks

Conversation

@VioletQwQ-0

Copy link
Copy Markdown
Collaborator

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

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 ClosedEvent callbacks. Under the 1000-VU point-select
workload, AppendEventCallback accounted for about 2.65 GB of allocation in a
30-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:

  • share immutable client-owned default ClosedEvent callbacks;
  • allocate an operator callback map only when custom callbacks are appended;
  • reset custom callbacks when an operator is reused;
  • add ordering, isolation, close-once, restart, race, and allocation coverage.

Benchmark results on Apple M1 (-benchmem, five runs; allocation values were
stable across all runs):

Benchmark Before After
New + rollback 2544 B / 17 allocs 2000 B / 12 allocs
New + one callback + rollback 2664 B / 19 allocs 2528 B / 17 allocs
New + 14 trace callbacks + rollback 5976 B / 50 allocs 4088 B / 46 allocs

Clean remote point-select A/B/A at 1000 VU:

  • baseline A: 180,508.66 TPS;
  • patched: 188,905.01 TPS;
  • baseline B: 178,377.58 TPS;
  • patched profile run: 186,083.03 TPS versus 180,208.74 TPS control;
  • AppendEventCallback disappeared from the patched allocation profile without
    an equivalent allocation transfer.

Validation:

  • go test -count=1 ./pkg/txn/client
  • go test -race -count=1 ./pkg/txn/client -run '^(TestEventCallbacksDefaultOrderAndFirstError|TestDefaultEventCallbacksCopyOnWrite|TestClientClosedEventDefaultsRunBeforeCustomOnce|TestRestartTxnRestoresDefaultsAndDropsCustomCallbacks|TestClosedEvent|TestCommitClosedEventWhenWaitRunningSQLFails|TestRollbackClosedEventWhenWaitRunningSQLFails)$'
  • go test -count=1 ./pkg/frontend ./pkg/sql/compile
  • go test -run '^$' -bench '^(BenchmarkTxnNewRollback|BenchmarkTxnNewAppendCallbackRollback|BenchmarkTxnNewAppendTraceCallbacksRollback)$' -benchmem -count=5 ./pkg/txn/client

@VioletQwQ-0
VioletQwQ-0 force-pushed the perf/txn-default-event-callbacks branch from 2efbb14 to 90cd31f Compare July 14, 2026 07:18
@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Jul 14, 2026

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@mergify

mergify Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-17 03:42 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-07-17 06:58 UTC · at 812aa5a5b14aee024282d6007918ba93a67e4e39 · squash

This pull request spent 3 hours 15 minutes 39 seconds in the queue, including 58 minutes 48 seconds running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • github-review-decision = APPROVED [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-neutral = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-skipped = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64

@mergify
mergify Bot merged commit 096940b into matrixorigin:main Jul 17, 2026
23 of 24 checks passed
@mergify mergify Bot removed the queued label Jul 17, 2026
@VioletQwQ-0
VioletQwQ-0 deleted the perf/txn-default-event-callbacks branch July 20, 2026 10:02
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
Every new transaction currently builds a callback map and slice to install the
two client-owned `ClosedEvent` callbacks. Under the 1000-VU point-select
workload, `AppendEventCallback` accounted for about 2.65 GB of allocation in a
30-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:
- share immutable client-owned default `ClosedEvent` callbacks;
- allocate an operator callback map only when custom callbacks are appended;
- reset custom callbacks when an operator is reused;
- add ordering, isolation, close-once, restart, race, and allocation coverage.

Benchmark results on Apple M1 (`-benchmem`, five runs; allocation values were
stable across all runs):

| Benchmark | Before | After |
|---|---:|---:|
| New + rollback | 2544 B / 17 allocs | 2000 B / 12 allocs |
| New + one callback + rollback | 2664 B / 19 allocs | 2528 B / 17 allocs |
| New + 14 trace callbacks + rollback | 5976 B / 50 allocs | 4088 B / 46 allocs |

Clean remote point-select A/B/A at 1000 VU:
- baseline A: 180,508.66 TPS;
- patched: 188,905.01 TPS;
- baseline B: 178,377.58 TPS;
- patched profile run: 186,083.03 TPS versus 180,208.74 TPS control;
- `AppendEventCallback` disappeared from the patched allocation profile without
an equivalent allocation transfer.

Validation:
- `go test -count=1 ./pkg/txn/client`
- `go test -race -count=1 ./pkg/txn/client -run '^(TestEventCallbacksDefaultOrderAndFirstError|TestDefaultEventCallbacksCopyOnWrite|TestClientClosedEventDefaultsRunBeforeCustomOnce|TestRestartTxnRestoresDefaultsAndDropsCustomCallbacks|TestClosedEvent|TestCommitClosedEventWhenWaitRunningSQLFails|TestRollbackClosedEventWhenWaitRunningSQLFails)$'`
- `go test -count=1 ./pkg/frontend ./pkg/sql/compile`
- `go test -run '^$' -bench '^(BenchmarkTxnNewRollback|BenchmarkTxnNewAppendCallbackRollback|BenchmarkTxnNewAppendTraceCallbacksRollback)$' -benchmem -count=5 ./pkg/txn/client`

Approved by: @XuPeng-SH

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants