Repository navigation
perf(mpool): narrow on-heap teardown scans - #28799
Conversation
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? |
a32c4ae to
6cad156
Compare
XuPeng-SH
left a comment
There was a problem hiding this comment.
GPT-6 medium deep review at current head f8a11d4789c574bbd96115a69cb1be1293f1cecd: no verified product blocker found.
For the mpool change, all production on-heap registrations flow through recordPtrHdr; the owner shard hint is published under the same shard lock after successful registry insertion. Bits are monotonic, so frees and cross-pool frees can only leave harmless stale hints; the pointer maps remain authoritative. Off-heap, noLock, and public full-scan paths remain unchanged. The added two-word hint state introduces no per-allocation objects or reverse lock edge. Teardown scans one hinted shard at a time; high-fanout owners can still approach full-scan cost, as the PR's benchmark discussion acknowledges.
The current diff also contains the cross-CN DDL consistency test update. Both concurrent ALTERs finish before the test takes the maximum commit timestamp from the two CNs; both logtail barriers complete before the stronger schema/default/index assertions. The wait is bounded and the DDLs are not replayed. This change appears separate from the mpool optimization, so consider mentioning it in the PR description or splitting it for scope clarity.
Exact-head CI run 34765114432 completed successfully, including SCA, Ubuntu UT, coverage, shared build, and PROXY/PESSIMISTIC multi-CN BVT. I found no verified leak, double-free, hang, or false-negative shard-hint path in the reviewed diff.
XuPeng-SH
left a comment
There was a problem hiding this comment.
GPT-6 medium deep re-review at current merged head f8de819abeb33b9d45f124048e48c85aa2f2809f: no verified blocker found, including for a significant racing-UT runtime increase.
For each on-heap registration the change adds one atomic load after the existing shard-map publication; successful hint CAS operations are bounded by the number of shard bits per pool lifetime. The bits only advance, and the pointer maps remain authoritative. No additional lock, goroutine, per-allocation object, or cleanup owner was introduced. The new concurrent UT performs 8 x 64 allocations and joins/cleans up all workers; the 100,000-pointer fixture is outside the benchmark timer.
I compared PR base 6b948a9d0753599b353f84a2242ccd4c063f5fb2 with current head on the same Apple M4 using -race: the mpool package tests completed in 2.755s vs 2.616s; the existing 64-byte alloc/free benchmark was about 4% slower (median 1028 vs 1070 ns/op); a temporary parallel shared-pool alloc/free benchmark was about 1% slower (median 865.2 vs 874.8 ns/op). This does not indicate a large race-mode regression, though it is not a Linux full-suite timing measurement.
Current merged-head run 34768848650 has SCA and coverage complete, while Ubuntu UT and multi-CN BVT are still in progress. I am not reporting those checks as green. The earlier run 34765114432 passed on the prior head, but does not replace the current run.
## What type of PR is this? - [ ] API-change - [ ] BUG - [x] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: References matrixorigin#28530. This is a scoped mitigation and intentionally does not close the issue: pools that touch many pointer shards still inspect most of the registry, and the reported QA workload profile has not yet been re-captured with this change. ## What this PR does / why we need it: `MPool.destroy()` previously called `OnHeapOutstanding()` for locking pools, scanning and locking every process-wide pointer-registry shard even when the pool had allocated only a few on-heap objects. Each locking pool now records a conservative bitset of shards into which successful on-heap pointer registrations were published. Teardown scans only those shards and still derives exact outstanding bytes and object counts from the authoritative pointer maps. Bits are monotonic and are not cleared on free, so stale bits can only cause extra scans; they cannot hide a still-live pointer. Off-heap registrations do not set the hints, noLock pools retain their local exact-map path, and the public `OnHeapOutstanding()` full-scan behavior is unchanged. The hot path adds a bit-test on registration and a CAS only on first publication to a shard. The pool stores two atomic words (16 bytes at the current 128-shard configuration); it does not add per-allocation heap objects or change pointer-map ownership. ### Local benchmark evidence Linux/amd64, Ryzen 9 7900X, `GOMAXPROCS=8`, 3 samples, 100ms each. Each operation creates a target pool, allocates/frees the listed number of on-heap objects, then deletes it while unrelated registry entries remain live. Candidate medians: | Unrelated live entries | Target owned objects | Hinted shards | End-to-end pool lifecycle | |---:|---:|---:|---:| | 0 | 1 | 1 | 0.38 us/op | | 10,000 | 1 | 1 | 0.90 us/op | | 100,000 | 1 | 1 | 6.05 us/op | | 100,000 | 8 | 8 | 44.2 us/op | | 100,000 | 32 | 29 | 161.6 us/op | | 100,000 | 128 | 80 | 464.5 us/op | The benefit narrows as an owner touches more shards; the 128-object case is close to a full scan. A prior paired one-object microbenchmark on the same hint implementation measured about 5–8.5 us/op versus 501–737 us/op at the base. These are local microbenchmarks, not end-to-end SQL or the original QA workload. Please keep matrixorigin#28530 open until the same QA workload/profile is re-measured and the residual high-fanout cost is assessed. ### Test Plan - Focused UT: `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -run "^TestOnHeapShardHints" ./pkg/common/mpool` - Owning package UT and race: `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 ./pkg/common/mpool` and `.agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 ./pkg/common/mpool` - New concurrency cases: run each exact test with `-race -count=100`. - Scoped static checks: `golangci-lint run -c .golangci.yml ./pkg/common/mpool`, `go vet ./pkg/common/mpool`, and repository `molint` on the package. - Scale benchmark: `.agents/skills/mo-dev/scripts/mo-cgo-test -run "^$" -bench "^BenchmarkMPoolDestroyUnrelatedPointersByOwnerSize$" -benchtime=100ms -count=3 ./pkg/common/mpool`. BVT is not applicable: this changes internal allocator diagnostics only; SQL-visible results and ownership/free semantics are unchanged.
## What type of PR is this? - [ ] API-change - [ ] BUG - [x] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: References matrixorigin#28530. This is a scoped mitigation and intentionally does not close the issue: pools that touch many pointer shards still inspect most of the registry, and the reported QA workload profile has not yet been re-captured with this change. ## What this PR does / why we need it: `MPool.destroy()` previously called `OnHeapOutstanding()` for locking pools, scanning and locking every process-wide pointer-registry shard even when the pool had allocated only a few on-heap objects. Each locking pool now records a conservative bitset of shards into which successful on-heap pointer registrations were published. Teardown scans only those shards and still derives exact outstanding bytes and object counts from the authoritative pointer maps. Bits are monotonic and are not cleared on free, so stale bits can only cause extra scans; they cannot hide a still-live pointer. Off-heap registrations do not set the hints, noLock pools retain their local exact-map path, and the public `OnHeapOutstanding()` full-scan behavior is unchanged. The hot path adds a bit-test on registration and a CAS only on first publication to a shard. The pool stores two atomic words (16 bytes at the current 128-shard configuration); it does not add per-allocation heap objects or change pointer-map ownership. ### Local benchmark evidence Linux/amd64, Ryzen 9 7900X, `GOMAXPROCS=8`, 3 samples, 100ms each. Each operation creates a target pool, allocates/frees the listed number of on-heap objects, then deletes it while unrelated registry entries remain live. Candidate medians: | Unrelated live entries | Target owned objects | Hinted shards | End-to-end pool lifecycle | |---:|---:|---:|---:| | 0 | 1 | 1 | 0.38 us/op | | 10,000 | 1 | 1 | 0.90 us/op | | 100,000 | 1 | 1 | 6.05 us/op | | 100,000 | 8 | 8 | 44.2 us/op | | 100,000 | 32 | 29 | 161.6 us/op | | 100,000 | 128 | 80 | 464.5 us/op | The benefit narrows as an owner touches more shards; the 128-object case is close to a full scan. A prior paired one-object microbenchmark on the same hint implementation measured about 5–8.5 us/op versus 501–737 us/op at the base. These are local microbenchmarks, not end-to-end SQL or the original QA workload. Please keep matrixorigin#28530 open until the same QA workload/profile is re-measured and the residual high-fanout cost is assessed. ### Test Plan - Focused UT: `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -run "^TestOnHeapShardHints" ./pkg/common/mpool` - Owning package UT and race: `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 ./pkg/common/mpool` and `.agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 ./pkg/common/mpool` - New concurrency cases: run each exact test with `-race -count=100`. - Scoped static checks: `golangci-lint run -c .golangci.yml ./pkg/common/mpool`, `go vet ./pkg/common/mpool`, and repository `molint` on the package. - Scale benchmark: `.agents/skills/mo-dev/scripts/mo-cgo-test -run "^$" -bench "^BenchmarkMPoolDestroyUnrelatedPointersByOwnerSize$" -benchtime=100ms -count=3 ./pkg/common/mpool`. BVT is not applicable: this changes internal allocator diagnostics only; SQL-visible results and ownership/free semantics are unchanged.
## What type of PR is this? - [ ] API-change - [ ] BUG - [x] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: References matrixorigin#28530. This is a scoped mitigation and intentionally does not close the issue: pools that touch many pointer shards still inspect most of the registry, and the reported QA workload profile has not yet been re-captured with this change. ## What this PR does / why we need it: `MPool.destroy()` previously called `OnHeapOutstanding()` for locking pools, scanning and locking every process-wide pointer-registry shard even when the pool had allocated only a few on-heap objects. Each locking pool now records a conservative bitset of shards into which successful on-heap pointer registrations were published. Teardown scans only those shards and still derives exact outstanding bytes and object counts from the authoritative pointer maps. Bits are monotonic and are not cleared on free, so stale bits can only cause extra scans; they cannot hide a still-live pointer. Off-heap registrations do not set the hints, noLock pools retain their local exact-map path, and the public `OnHeapOutstanding()` full-scan behavior is unchanged. The hot path adds a bit-test on registration and a CAS only on first publication to a shard. The pool stores two atomic words (16 bytes at the current 128-shard configuration); it does not add per-allocation heap objects or change pointer-map ownership. ### Local benchmark evidence Linux/amd64, Ryzen 9 7900X, `GOMAXPROCS=8`, 3 samples, 100ms each. Each operation creates a target pool, allocates/frees the listed number of on-heap objects, then deletes it while unrelated registry entries remain live. Candidate medians: | Unrelated live entries | Target owned objects | Hinted shards | End-to-end pool lifecycle | |---:|---:|---:|---:| | 0 | 1 | 1 | 0.38 us/op | | 10,000 | 1 | 1 | 0.90 us/op | | 100,000 | 1 | 1 | 6.05 us/op | | 100,000 | 8 | 8 | 44.2 us/op | | 100,000 | 32 | 29 | 161.6 us/op | | 100,000 | 128 | 80 | 464.5 us/op | The benefit narrows as an owner touches more shards; the 128-object case is close to a full scan. A prior paired one-object microbenchmark on the same hint implementation measured about 5–8.5 us/op versus 501–737 us/op at the base. These are local microbenchmarks, not end-to-end SQL or the original QA workload. Please keep matrixorigin#28530 open until the same QA workload/profile is re-measured and the residual high-fanout cost is assessed. ### Test Plan - Focused UT: `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -run "^TestOnHeapShardHints" ./pkg/common/mpool` - Owning package UT and race: `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 ./pkg/common/mpool` and `.agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 ./pkg/common/mpool` - New concurrency cases: run each exact test with `-race -count=100`. - Scoped static checks: `golangci-lint run -c .golangci.yml ./pkg/common/mpool`, `go vet ./pkg/common/mpool`, and repository `molint` on the package. - Scale benchmark: `.agents/skills/mo-dev/scripts/mo-cgo-test -run "^$" -bench "^BenchmarkMPoolDestroyUnrelatedPointersByOwnerSize$" -benchtime=100ms -count=3 ./pkg/common/mpool`. BVT is not applicable: this changes internal allocator diagnostics only; SQL-visible results and ownership/free semantics are unchanged.
…xes (#29292) ## What type of PR is this? - [ ] API-change - [x] BUG - [x] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: References #28530 and #28609. Backports #28799 and #28610 to `4.2-dev`. The mpool change remains a scoped mitigation, not a claim that all costs in #28530 are eliminated. ## What this PR does / why we need it: Backports two already-merged main fixes onto `4.2-dev` at `7a68d42c4ec151f9af5890ee56b4d24afd87b843`, preserving original authors and `cherry-pick -x` provenance: 1. **#28610** (`6ad98a5f5e8cbfd8f328836676f2186bfa2e09c7`): replace the multi-term `ReadFilterSearch` int64-per-source-row marks with a compact bitset and allocate the result to its exact size. Preserve deduplication, ascending row order, and the existing single-term and malformed-input paths. Include the allocation regression test. 2. **#28799** (`14e457608003381ef409fbf0c475e0a931633a28`): record conservative per-pool pointer-shard hints and restrict destroy-time on-heap ownership scans to those shards. Registry maps remain authoritative; free does not clear hints; noLock pools and public `OnHeapOutstanding()` behavior are unchanged. Include the correctness/concurrency tests and scale benchmark. ### Release-branch adaptations - Resolve the objectio import conflict by adding only `math/bits`; unrelated main-only imports/implementations are not brought over. - Omit the unrelated `pkg/tests/issues/issue_277xx_ddl_consistency_test.go` adjustment bundled with #28799: that test does not exist on `4.2-dev`. Keep all direct mpool regression coverage. - Preserve the release branch's existing allocation-account/lease implementation. Accounted allocations are off-heap and do not require on-heap hints. Only four files change. No catalog/schema migration, configuration, wire or on-disk format, dependencies, or CI workflow changes. No changes to release tags or running deployments. ### Validation Fresh local validation on Go 1.26.4, Darwin/arm64: - `GOWORK=off go list -mod=readonly ./pkg/common/mpool ./pkg/objectio`: PASS. - `GOWORK=off go test -mod=readonly -count=1 -timeout=120s ./pkg/common/mpool`: PASS. - `GOWORK=off go test -mod=readonly -race -json -count=1 -timeout=240s ./pkg/common/mpool`: PASS (full package). - Repository `mo-cgo-test` wrapper, each with `-race -count=100 -timeout=120s`: PASS for `TestOnHeapShardHintsAreConservativeAndExact` and `TestOnHeapShardHintsConcurrentPublicationAndCrossPoolFree`. Initial JSON durations were below timer resolution; the adaptive budget therefore used N=100. - `.agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=180s ./pkg/objectio`: PASS (full package, including allocation and legacy predicate equivalence regressions; also a consumer of mpool). - `go build -mod=readonly` and `go vet -mod=readonly` for both owning packages with `GOWORK=off` and repository-local native include/library paths: PASS. Native prerequisites were built from this worktree's bundled thirdparty sources and CGo sources. - `git diff --check` and `gofmt -l` for all four changed files: PASS. - Self-review: exact ownership diagnostics, monotonic atomic hint publication, cross-pool frees, fixed-size hint storage, bitset bounds, result order and release-branch adaptations checked. No additional lifecycle, external wait or persistent state introduced. Full distributed BVT, end-to-end QA profiling and new performance benchmarks were not run for this backport. CI remains required; no end-to-end latency improvement is claimed from these unit tests. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: XuPeng-SH <[email protected]>
What type of PR is this?
Which issue(s) this PR fixes:
References #28530. This is a scoped mitigation and intentionally does not close the issue: pools that touch many pointer shards still inspect most of the registry, and the reported QA workload profile has not yet been re-captured with this change.
What this PR does / why we need it:
MPool.destroy()previously calledOnHeapOutstanding()for locking pools, scanning and locking every process-wide pointer-registry shard even when the pool had allocated only a few on-heap objects.Each locking pool now records a conservative bitset of shards into which successful on-heap pointer registrations were published. Teardown scans only those shards and still derives exact outstanding bytes and object counts from the authoritative pointer maps. Bits are monotonic and are not cleared on free, so stale bits can only cause extra scans; they cannot hide a still-live pointer. Off-heap registrations do not set the hints, noLock pools retain their local exact-map path, and the public
OnHeapOutstanding()full-scan behavior is unchanged.The hot path adds a bit-test on registration and a CAS only on first publication to a shard. The pool stores two atomic words (16 bytes at the current 128-shard configuration); it does not add per-allocation heap objects or change pointer-map ownership.
Local benchmark evidence
Linux/amd64, Ryzen 9 7900X,
GOMAXPROCS=8, 3 samples, 100ms each. Each operation creates a target pool, allocates/frees the listed number of on-heap objects, then deletes it while unrelated registry entries remain live. Candidate medians:The benefit narrows as an owner touches more shards; the 128-object case is close to a full scan. A prior paired one-object microbenchmark on the same hint implementation measured about 5–8.5 us/op versus 501–737 us/op at the base. These are local microbenchmarks, not end-to-end SQL or the original QA workload. Please keep #28530 open until the same QA workload/profile is re-measured and the residual high-fanout cost is assessed.
Test Plan
.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -run "^TestOnHeapShardHints" ./pkg/common/mpool.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 ./pkg/common/mpooland.agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 ./pkg/common/mpool-race -count=100.golangci-lint run -c .golangci.yml ./pkg/common/mpool,go vet ./pkg/common/mpool, and repositorymolinton the package..agents/skills/mo-dev/scripts/mo-cgo-test -run "^$" -bench "^BenchmarkMPoolDestroyUnrelatedPointersByOwnerSize$" -benchtime=100ms -count=3 ./pkg/common/mpool.BVT is not applicable: this changes internal allocator diagnostics only; SQL-visible results and ownership/free semantics are unchanged.