Repository navigation
perf(mpool): track on-heap ownership per pool - #29353
Conversation
2fa6cf5 to
d99bf2c
Compare
d99bf2c to
4ca5753
Compare
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? |
4ca5753 to
606dd77
Compare
3007206 to
bd05d21
Compare
XuPeng-SH
left a comment
There was a problem hiding this comment.
本轮 deep review 结论:未发现新增的阻塞性缺陷。评审范围为 e914d987641c → 5d21f2e6b1 的完整 4 文件增量;逐个检查了实现、替换的测试、相邻消费者及 PR 描述。本轮由当前 reviewer 直接完成,未调用 subagent;历史模型评审与本地验证经源码指纹核对后复用。
设计与职责归属
计数变更归现有指针登记/移除边界,仍只有原来的 registry 和释放路径。成功登记增加 owner 计数,唯一成功的移除扣减;重复登记、重复释放不能重复改变计数。锁定 pool 的删除只做诊断和注销,已登记指针继续保有必要身份,late-free 不依赖 globalPools 中的 owner 是否仍存在。NoLock 的最终批量释放继续由其独占生命周期负责。
普通 registry value 用 owner 引用替换原 pool ID 槽,amd64 仍为 16 字节;owner 是独立 24 字节对象,没有反向持有完整 MPool。检查了 off-heap realloc 的同 owner guard、物理失败恢复、header 字段重建及跨 pool 分配复制释放路径;accounted 元数据与 lease 仍走原有 owner。旧 shard hint、扫描函数和多余 publication wrapper 已删除,没有保留两套机制。
| Unhappy-path 审查 | 结论与证据 |
|---|---|
| Q1:唯一有效释放 | 普通元数据成功移除或 NoLock 独占终止释放;正常/跨 pool/删除后释放、重复释放、realloc 回退均闭合。 |
| Q2:等待可终止 | 沿用已有 shard mutex;新增临界区内容为有界元数据操作和原子计数,没有新锁依赖、goroutine、RPC 或 I/O。并发用例在 workers 全部退出后停止并等待 reader。 |
| Q3:保留状态可回收 | 无 retired-owner map、额外 pointer index 或复用代际;本轮 GC 挑战验证完整 MPool 可先回收,最后一条登记移除后 compact owner 也可回收。原 map 的容量高水位与新增指针扫描成本已披露。 |
本轮新增 QA 证据
通过外部 Go overlay 执行,未向 PR 堆入额外测试框架:
TestReviewDeletedOwnerRealloc:删除原 owner 后,由另一 pool 将 64B buffer 扩容到 128B;原数据、扩展区域清零、两边 owner 字节/对象数和最终全局计数全部正确。TestReviewMetadataRetainsOnlyCompactIdentity:on-heap / off-heap 两个场景分别强制 GC;仍有登记时完整 MPool 已可回收、小 owner 身份仍活着;late-free 后小 owner 身份也可回收。- 两项 probe(含两个 heap 子场景)全部通过,mpool 测试进程 0.012s。未用睡眠、随机重试或大数据达成生命周期结论。
复用的有效验证包括 mpool normal/race 整包、两个并发用例各自 race 100 次、realloc 七场景、vector 的 rollback/dup-offheap 消费者、vet、仓库 lint 和 err-check。实现指纹与已推送内容一致。
实现、测试、文档分别审查
| 部分 | 相对 base 的增量 | 审查结果 |
|---|---|---|
| 生产实现 | +70 / -101,净 -31 行 | 新成本集中在 owner 身份及两项派生计数;没有新增状态机、存储索引或执行分支体系。 |
| 测试 | +494 / -280,净 +214 行 | 替换旧表示相关断言,保留混合 owner、off-heap 排除和并发行为覆盖;增加独立 registry oracle、失败/late-free/终止清理及共享 realloc 矩阵。普通 UT 使用小数据,100k 数据仅用于显式 benchmark。 |
| 文档 | 无新增 tracked 文档文件 | PR 描述披露了测量条件、代价和局限;没有把 allocator 微基准当作原业务 SQL 延迟改善的证明。 |
性能取舍仍然存在,不能忽略
复核已有同 harness 数据:100k 无关登记、128 次历史分配的完整 pool 生命周期,CPU=8 中位数由约 618µs 降至 19.9µs。共享 64B alloc/free 较原 PR 改善约 17–24%,但仍比 hinted-scan base 慢约 24–26%;交错测量的普通 off-heap 开销约 1.6–3.8%。每 pool 新增一个 24B 身份对象,没有每 buffer 额外 Go 堆分配,registry 仍新增了一个 GC 扫描指针槽。
这是已量化、已披露的设计取舍;没有新的事实推翻其成立条件。原业务负载的净吞吐/SQL tail latency 改善仍需同负载 profile,因此保持 Related to #28530,不宣称该 issue 的所有性能现象都已消失。
截至提交本评审前,PR 没有人工或技术 review comment,只有 Qodo 暂停提示,因此没有尚未解决的技术评论。当前 CI 未见失败,UT/SCA/coverage/BVT 仍在运行;本结论不将运行中或跳过的检查算作通过。
XuPeng-SH
left a comment
There was a problem hiding this comment.
补充评审:撤回上一轮对性能取舍的接受,当前性能验收仍未闭合。 本补充针对同一 head 5d21f2e6b1c3996d8ec1d715220e73b35adf6d72;功能、生命周期及 race 验证结论未发生变化。
共享小 buffer 分配/释放相对原基线慢 24–26%,发生在公共热路径。先前将其视为已披露即可接受的取舍,证据不足:销毁路径大幅提速并不能独立证明真实负载的净收益。
为定位成本,使用相同 Go/CGo 环境、同一个共享 pool 的 64B Alloc/Free benchmark,CPU=8,每组 300ms × 5,基线和当前版本在实验两端重复,共各 10 个样本。所有实验通过外部 Go overlay 执行,未修改提交内容:
| 实验 | 中位数 ns/op | 含义 |
|---|---|---|
| 原基线 | 81.9 | 参考值 |
| 当前 head | 105.7 | 重现明显退化 |
| 仅禁用 owner 计数更新 | 83.2 | 故意破坏计数正确性的消融实验,仅用于归因,不能作为修复交付 |
| 保留更新、移到 shard 锁外 | 112.4 | 此实验实现未改善 |
| owner 补齐到 64B | 104.7 | 未见有意义改善,额外增加每 pool 内存 |
主要成本来自每对 Alloc/Free 新增的四次 owner 原子计数更新;仅调整锁内外位置或 padding 不能解决本次测量中的退化。当前与基线各轮之间存在波动,因此不将单次比例作为精确业务预测;退化方向与此前独立测量一致。
未解决项: 优化计数维护,保持删除后 late-free、跨 pool free/realloc、并发与异常清理的正确性;同时复测分配热路径和销毁路径。若仍保留明显热路径退化,需要代表性同负载的端到端吞吐、CPU 与延迟证据,并明确接受边界。现有微基准不能证明 SQL 整体慢 24–26%,也不能证明业务净收益足以抵消这项代价。当前不应将性能标记为通过。
本轮未调用 subagent,未改生产代码、未合并或 rebase。
|
The previously tracked UT Coverage failure is no longer a blocker on head
|
5d21f2e to
afd8068
Compare
XuPeng-SH
left a comment
There was a problem hiding this comment.
Reviewed 72d4a49db7d750520e1a1654cb6091fc7169882b...ef899d64a333a06a8c71fc48f4eb8241220422b1. Approve: no blocking correctness, ownership, cleanup or design finding remains. The earlier shared-allocation performance objection is closed for this revision by the measurements below, with the remaining costs explicitly accepted.
The ownership design has one effective transition owner: successful insertion/removal in the existing pointer registry updates the original pool's counters under the existing shard lock. The compact identity has no back-reference to MPool, so deletion followed by cross-pool Free or Realloc does not retain the whole pool or lose the original ownership charge. NoLock remains single-owner and has terminal bulk cleanup/reset. Accounted leases, off-heap capacity admission, physical allocation/free, profiling and global ownership metrics keep their existing responsibilities. The old hints, scan path and publication wrappers were removed together; no duplicate fallback remains. This is focused internal performance maintenance, not a new public allocation policy or persistence format.
I checked all four changed files and the relevant callers/reverse paths, including duplicate registration, double free, realloc rejection/restoration, within-capacity resizing, deleted-owner transfer, mixed NoLock teardown and repeated deletion. Concurrent diagnostics remain approximate snapshots and become exact after quiescence; they do not control admission. The added packed arithmetic and fixed per-pool storage buy removal of registry-sized diagnostic work and its global-shard lock interference.
Independent validation on this head, Go 1.27.1, darwin/arm64, Apple M4, with freshly built matching CGo libraries:
- Complete mpool package: normal PASS (2.132s), race PASS (2.477s).
- Each changed concurrency test was selected individually under race, measured, then run 100 times in one process:
TestOnHeapOwnershipConcurrentPublicationAndCrossPoolFreeandTestOnHeapOwnershipDeleteAndLateFree, both PASS. - Reviewer-only Go overlays: a scalar oracle checked 4,096 mixed valid allocate/free transitions at both small and near-max allocation sizes, including repeated 4GiB carries/borrows and the packed object-count boundary. PASS. This is arithmetic verification without allocating multi-gigabyte buffers.
- Public-entry probe: on-heap allocation -> optional original-owner deletion -> other-pool ReallocZero -> Free, with on/off-heap destinations. Data preservation, zero extension, independent registry accounting and terminal counts passed in all four cases.
Performance was compared against the fresh merge-base using identical benchmark bodies and native/toolchain inputs, in fresh-process base/head/head/base order, CPU=8. Allocation results are medians of 10 samples per revision (500ms x 5 per process); lifecycle results use 6 samples (150ms x 3). Lifecycle includes construction, allocation, free and deletion, not just destroy.
| Scenario | Base ns/op | Head ns/op | Change |
|---|---|---|---|
| 64B locking on-heap, serial | 56.925 | 62.630 | +10.0% / +5.705ns |
| 64B NoLock on-heap, serial | 46.100 | 48.590 | +5.4% |
| 64B shared pool, no unrelated entries | 180.500 | 187.000 | +3.6% |
| 64B shared pool, 100k unrelated entries | 179.800 | 184.050 | +2.4% |
| 64B off-heap | 142.650 | 145.100 | +1.7% |
| 1KiB off-heap | 158.300 | 155.750 | -1.6% |
| Empty pool lifecycle, empty registry | 230.650 | 472.500 | +241.850ns |
| One-allocation lifecycle, empty registry | 295.650 | 560.250 | +264.600ns |
| One-allocation lifecycle, 100k unrelated entries | 4,249.500 | 602.300 | -85.8% |
| 128-allocation lifecycle, 100k unrelated entries | 331,970.500 | 15,953.000 | -95.2% |
Each new pool costs one extra allocation and 2,304 additional allocated bytes on this platform; ordinary per-buffer allocation counts are unchanged. I accept these bounded costs and the measured small absolute hot-path increase in exchange for the demonstrated scaling improvement. This supersedes my earlier acceptance of the old 24–26% shared-path regression: that older result must not be presented as accepted unchanged. These microbenchmarks do not establish production TPMC, Linux/x86 performance, or closure of the original QA SQL latency issue; #28530 should remain open for that operational verification.
Test-quality/delta review: implementation +139/-100; tests +532/-280; tracked documentation +0/-0. The retired hint-specific assertions no longer describe a live contract; allocation ownership, cross-pool cleanup and concurrent publication are retained with an independent registry oracle, and failure/realloc/terminal-cleanup coverage was added. The CI failure discussion refers to an older unrelated run; this head's required checks are green.
Non-blocking PR-description correction: “normal registration/removal uses one owner atomic” is inaccurate. An ordinary non-borrowing Free executes value.Add(delta) and then the high-word correction value.Add(...); an ordinary Alloc/Free pair therefore performs three owner atomic additions. The acceptance above uses the actual implementation, not that operation-count claim.
Merge Queue Status
This pull request spent 16 seconds in the queue, including 1 second running CI. Required conditions to merge
|
What type of PR is this?
Which issue(s) this PR fixes:
Related to #28530. The original QA workload still needs profiling before attributing its SQL tail latency to this change.
What this PR does / why we need it:
Locking-pool ownership diagnostics become O(1), independent of process-wide live pointers or the shards historically touched by a pool. Ownership remains correct after pool deletion and a subsequent same-pool or cross-pool free.
The deployed issue revision scanned all pointer shards; the intervening shard hints narrowed the scan but still visited unrelated entries. The incoming counter implementation had a lifetime regression:
Alloc(64, false) -> DeleteMPool(owner) -> other.Free(buffer)left a retained owner's diagnostics at 64 bytes / 1 object because itsglobalPoolsentry had disappeared. The parent passes this public-API reproduction, the incoming PR fails, and this implementation passes. This was stale diagnostic accounting, not retention of the freed Go buffer.Successful insertion/removal in the existing pointer registry now owns the counter transition. Each pool has one independently allocated compact identity/counter object; ordinary metadata references it rather than looking up a possibly deleted pool. No whole MPool, retired-owner registry, additional pointer map, or new cleanup protocol is retained. NoLock pools use the same counters under their existing single-owner contract and perform their existing terminal bulk release once.
The owner counters are split across the same 128 fixed slots as the global pointer registry. A normal transition performs one atomic add to a packed value: object count in the high 32 bits and the byte delta in the low 32 bits. A separate counter changes only when the low word crosses a 2^32-byte boundary. This removes the shared counter cache line while keeping insertion/removal transitions tied to successful pointer-registry ownership and preserving NoLock terminal cleanup.
The Go-only registry value replaces the duplicated numeric pool ID with the compact identity reference and remains 16 bytes on amd64. The physical
memHdrand public interfaces remain unchanged. Accounted allocation metadata, leases, global high-water metrics, off-heap admission, profiling, and physical allocation/free paths retain their existing owners. The unused per-pool high-water CAS and obsolete shard hints are removed.Validation
Linux/amd64, Intel i7-11700, Go 1.26.4, GOAMD64=v1, fresh worktree-native CGo prerequisites; no SQL/protocol behavior change requiring a BVT.
go vet, repository-configuredgolangci-lint(0 issues), andmake err-check: pass. Earlier compact-owner validation also covered the full package on Linux/amd64.TestAppendCheckpointRollbackandTestDupOffHeap: pass.go vet, repository-configuredgolangci-lint(0 issues), andmake err-check: pass.~/.codex/issue-to-pr/matrixone/pr29353/.Performance and costs
The lifecycle and earlier shared-path comparison used the same benchmark bodies and toolchain through overlays on parent
e914d987641c, incomingbd05d2156f5e, and the compact-counter implementation. Lifecycle medians use 150ms × 3, CPU=8; shared allocation/free uses 500ms × 5, CPU=8. Lifecycle includes pool construction, allocation, free, and deletion; these are not isolated destroy timings.The packed-counter revision was then compared in a fresh-process
base/current/current/basesequence on darwin/arm64 (Apple M5, Go 1.27.1, CPU=8, 500ms × 10 per process). Across 20 samples, 64B locking-pool medians were 187.30 -> 193.65 ns/op serial (+3.4%) and 208.85 -> 188.85 ns/op concurrent (-9.6%). Normal registration/removal therefore uses one owner atomic rather than two and removes the shared counter contention, but this is not a zero-overhead optimization.Off-heap results were order-sensitive. A fresh-process parent/final/final/parent comparison (500ms × 3 per process, CPU=8) measured six-sample medians 245.35 -> 254.6 ns for 64B (+3.8%) and 252.25 -> 256.25 ns for 1KiB (+1.6%). Earlier fixed-order estimates were larger; the order-controlled result does not establish that every workload is regression-free.
An external common control allocated/freed 100k mixed entries, forced GC, retained map capacity, then measured 64KiB on-heap allocation/free (500ms × 5). Parent/final medians were 5,690/4,809 ns at CPU=1 and 4,520/4,833 ns at CPU=8. It did not reproduce a stable 40% ordinary hot-path regression from an earlier mixed benchmark sequence. A scanned owner pointer slot remains, so GC effects still depend on workload and process history.
There are no additional per-buffer heap allocations. The independently allocated owner identity now contains 128 fixed 16-byte counter slots, so it costs about 2 KiB per pool on 64-bit platforms. Restoring 16-byte registry values avoids the draft's extra 8 bytes per ordinary entry (about 8 MB raw payload per million entries), but does not remove the added GC pointer slot. Ordinary off-heap entries also retain only this compact identity until removal.
Remaining limits
The packed byte/object word and rare 2^32-byte carry word are separate atomics; concurrent diagnostic reads can straddle a carry transition and are exact after quiescence. Locking deletion diagnoses outstanding ownership and permits existing late-free behavior; NoLock allocations still must not outlive their pool. Local allocator benchmarks establish the changed scaling and costs, not production TPMC or the complete original SQL latency issue. Fresh-head CI run 37954091649 completed successfully, including both BVT suites, UT, coverage, SCA, and the required check.