Repository navigation
perf(malloc): reduce mmap faults for large hash tables - #26143
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? |
aptend
left a comment
There was a problem hiding this comment.
Reviewed the mmap-cache ownership/lifecycle paths and segmented hash-table growth. Relevant package tests, race checks, vet, formatting, and additional multi-block growth stress coverage passed. The current Ubuntu failure is the unrelated JSON-overlaps allocation-count flake also seen on PR #26147.
c16e72c to
6e812f4
Compare
LeftHandCold
left a comment
There was a problem hiding this comment.
Deep-reviewed the complete diff at 6e812f4, including mmap/MADV_FREE ownership, timer generations, capacity and expiry bounds, allocation-failure atomicity, segmented Int64/String hash-table addressing and in-place rehash, serialization compatibility, and the upper hashmap/group call paths. I found no remaining actionable correctness, lifecycle, or normal-business regression issue.
Fresh local evidence: build and vet passed for all affected/dependent packages; full UT and full -race passed for malloc, mpool, hashtable, hashmap, and colexec/group; focused segmented-growth/failure-atomicity tests passed for 10 repetitions. The current Ubuntu job failure is logservicedriver.TestReplay1 rejecting LogShardID=0, outside the changed code paths.
## What type of PR is this? - [x] Improvement ## What this PR does / why we need it Reduce contention in the shared shuffle pool without changing shuffle backpressure or buffering limits: - account only batches changed by each union instead of rescanning the whole bucket - shard batch memory tracking to remove the global accounting mutex - shard recycled batches by the local holder bound, with two padded embedded slots per shard - keep recycle lookup O(1) independently of the global shuffle bucket count - let a cold holder shard warm once without allowing a previously hot bucket to monopolize the cache The recycle capacity remains exactly `2 * maxHolders`. Sharding follows the local concurrency bound rather than `bucketNum`, which can grow as stage nodes times DOP in multi-CN plans. This is complementary to the allocator/mmap work in #26143 and is kept separate so both changes can be reviewed, measured, and reverted independently. Related to #26132. Follow-up to #26143. ## Which issue(s) this PR fixes None. #26143 already owns the closing reference for #26132. ## Testing - focused recycle/accounting/error-path stress under `-race -count=50` - `go test -race ./pkg/sql/colexec/shuffle` - `go test ./pkg/sql/colexec/shuffle ./pkg/sql/compile ./pkg/vm/pipeline ./pkg/vm` - `go test -race ./pkg/vm/pipeline` - `go build ./pkg/sql/colexec/shuffle` - `go vet ./pkg/sql/colexec/shuffle` - benchmark matrix covering 16/128/1024 buckets, fixed-worker concurrency, single-CN, multi-CN, drain-all, round-robin, and phased skew
…26143) ## What type of PR is this? - [ ] API-change - [x] BUG - [x] Improvement - [ ] Documentation - [ ] Feature - [x] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: Fixes matrixorigin#26132 ## What this PR does / why we need it: ### Corrected root cause The original issue attributed Q18 to MARK join falling back to LoopJoin. Controlled plans and adjacent-commit A/B refute that attribution: canonical Q18 remains a SEMI hash join, and matrixorigin#25822 is neutral/slightly faster than its direct parent. Further bisection identifies `7ffd26c7e1` from matrixorigin#25820 as the first bad commit. Besides its pipeline lifecycle changes, that commit moved all off-heap allocations at or above 128 KiB from libc to immediate `mmap`/`munmap`. Q18 builds a roughly 1 GiB, 15-million-row hash aggregate; repeated growth faults transient tables, and immediate unmap makes the final working set fault again on the next execution. Disabling only that mmap path restores the old timing, while the physical plan and scan cardinalities remain unchanged. ### Changes 1. Segment large Int64/String hash tables at 4 MiB instead of 256 MiB. Growth appends blocks and preserves existing mappings rather than allocating and faulting another full contiguous table. Load-factor decisions still use the total table size, preserving the original 1/2, 2/3, 3/4, and 4/5 thresholds. 2. Add bounded exact-size reuse for Linux mmap allocations used by zeroing `Allocate`/`ReallocZero`: - freed pages are marked `MADV_FREE`, so the kernel can reclaim them under pressure; - retained virtual memory is bounded by `min(global memory limit / 4, 1 GiB)`; - all cached mappings are actively `munmap`ed after one second of inactivity; - `Malloc` does not reuse LazyFree pages because its no-clear contract cannot safely reactivate every page; - non-Linux platforms keep the existing immediate-unmap behavior; - cached bytes are exposed as `mo_mem_offheap_inuse_bytes{type="mpool-cache"}`. ### Performance Single-node launch, TPCH 10G Q18, same data and machine: | Version | Wall time | |---|---:| | current main before this change | 0.67-0.70 s, median 0.68 s | | segmented hash table only | 0.55-0.58 s, median 0.57 s | | final branch, after allocator cache idle | 0.517 s | | final branch, burst steady state | 0.302-0.323 s, median 0.317 s | | pre-regression `0d8111ae` | 0.47-0.50 s | A 10-query process-counter sample reduced minor faults from 5,826,419 to 1,361,708 and system CPU ticks from 6,484 to 997. The cache sample contained about 1.045 GiB of `LazyFree` pages; after two idle seconds `LazyFree` returned to zero and RSS fell from 2.65 GiB to 1.61 GiB. Spot checks of Q1, Q6, Q9, Q13, Q17, and Q21 against the main baseline found no regression; all were neutral or faster. ### Ownership / unhappy-path review - Mapping ownership has one linear path: mmap caller -> cache -> zeroing caller, or cache -> idle timer -> munmap. - Capacity rejection and failed `madvise` retain caller ownership and fall back to immediate munmap. - The cache mutex removes mappings before unmapping, so take/expiry cannot double-release. - Cache growth has both a hard byte bound and an idle recycle point. - Old timer callbacks check the current idle generation before releasing mappings. ### Validation ```text .agents/skills/mo-dev/scripts/mo-cgo-test -race \ -run TestSimpleCAllocatorMmapCache -count=20 -timeout=240s \ ./pkg/common/malloc .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=180s \ ./pkg/common/malloc ./pkg/common/mpool ./pkg/container/hashtable \ ./pkg/common/hashmap ./pkg/sql/colexec/group .agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 -timeout=240s \ ./pkg/common/malloc ./pkg/common/mpool ./pkg/container/hashtable \ ./pkg/common/hashmap ./pkg/sql/colexec/group make build go vet ./pkg/common/malloc ./pkg/common/mpool ./pkg/container/hashtable \ ./pkg/common/hashmap ./pkg/sql/colexec/group ``` All commands passed after rebasing onto current main.
## What type of PR is this? - [x] Improvement ## What this PR does / why we need it Reduce contention in the shared shuffle pool without changing shuffle backpressure or buffering limits: - account only batches changed by each union instead of rescanning the whole bucket - shard batch memory tracking to remove the global accounting mutex - shard recycled batches by the local holder bound, with two padded embedded slots per shard - keep recycle lookup O(1) independently of the global shuffle bucket count - let a cold holder shard warm once without allowing a previously hot bucket to monopolize the cache The recycle capacity remains exactly `2 * maxHolders`. Sharding follows the local concurrency bound rather than `bucketNum`, which can grow as stage nodes times DOP in multi-CN plans. This is complementary to the allocator/mmap work in matrixorigin#26143 and is kept separate so both changes can be reviewed, measured, and reverted independently. Related to matrixorigin#26132. Follow-up to matrixorigin#26143. ## Which issue(s) this PR fixes None. matrixorigin#26143 already owns the closing reference for matrixorigin#26132. ## Testing - focused recycle/accounting/error-path stress under `-race -count=50` - `go test -race ./pkg/sql/colexec/shuffle` - `go test ./pkg/sql/colexec/shuffle ./pkg/sql/compile ./pkg/vm/pipeline ./pkg/vm` - `go test -race ./pkg/vm/pipeline` - `go build ./pkg/sql/colexec/shuffle` - `go vet ./pkg/sql/colexec/shuffle` - benchmark matrix covering 16/128/1024 buckets, fixed-worker concurrency, single-CN, multi-CN, drain-all, round-robin, and phased skew
…26143) ## What type of PR is this? - [ ] API-change - [x] BUG - [x] Improvement - [ ] Documentation - [ ] Feature - [x] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: Fixes matrixorigin#26132 ## What this PR does / why we need it: ### Corrected root cause The original issue attributed Q18 to MARK join falling back to LoopJoin. Controlled plans and adjacent-commit A/B refute that attribution: canonical Q18 remains a SEMI hash join, and matrixorigin#25822 is neutral/slightly faster than its direct parent. Further bisection identifies `7ffd26c7e1` from matrixorigin#25820 as the first bad commit. Besides its pipeline lifecycle changes, that commit moved all off-heap allocations at or above 128 KiB from libc to immediate `mmap`/`munmap`. Q18 builds a roughly 1 GiB, 15-million-row hash aggregate; repeated growth faults transient tables, and immediate unmap makes the final working set fault again on the next execution. Disabling only that mmap path restores the old timing, while the physical plan and scan cardinalities remain unchanged. ### Changes 1. Segment large Int64/String hash tables at 4 MiB instead of 256 MiB. Growth appends blocks and preserves existing mappings rather than allocating and faulting another full contiguous table. Load-factor decisions still use the total table size, preserving the original 1/2, 2/3, 3/4, and 4/5 thresholds. 2. Add bounded exact-size reuse for Linux mmap allocations used by zeroing `Allocate`/`ReallocZero`: - freed pages are marked `MADV_FREE`, so the kernel can reclaim them under pressure; - retained virtual memory is bounded by `min(global memory limit / 4, 1 GiB)`; - all cached mappings are actively `munmap`ed after one second of inactivity; - `Malloc` does not reuse LazyFree pages because its no-clear contract cannot safely reactivate every page; - non-Linux platforms keep the existing immediate-unmap behavior; - cached bytes are exposed as `mo_mem_offheap_inuse_bytes{type="mpool-cache"}`. ### Performance Single-node launch, TPCH 10G Q18, same data and machine: | Version | Wall time | |---|---:| | current main before this change | 0.67-0.70 s, median 0.68 s | | segmented hash table only | 0.55-0.58 s, median 0.57 s | | final branch, after allocator cache idle | 0.517 s | | final branch, burst steady state | 0.302-0.323 s, median 0.317 s | | pre-regression `0d8111ae` | 0.47-0.50 s | A 10-query process-counter sample reduced minor faults from 5,826,419 to 1,361,708 and system CPU ticks from 6,484 to 997. The cache sample contained about 1.045 GiB of `LazyFree` pages; after two idle seconds `LazyFree` returned to zero and RSS fell from 2.65 GiB to 1.61 GiB. Spot checks of Q1, Q6, Q9, Q13, Q17, and Q21 against the main baseline found no regression; all were neutral or faster. ### Ownership / unhappy-path review - Mapping ownership has one linear path: mmap caller -> cache -> zeroing caller, or cache -> idle timer -> munmap. - Capacity rejection and failed `madvise` retain caller ownership and fall back to immediate munmap. - The cache mutex removes mappings before unmapping, so take/expiry cannot double-release. - Cache growth has both a hard byte bound and an idle recycle point. - Old timer callbacks check the current idle generation before releasing mappings. ### Validation ```text .agents/skills/mo-dev/scripts/mo-cgo-test -race \ -run TestSimpleCAllocatorMmapCache -count=20 -timeout=240s \ ./pkg/common/malloc .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=180s \ ./pkg/common/malloc ./pkg/common/mpool ./pkg/container/hashtable \ ./pkg/common/hashmap ./pkg/sql/colexec/group .agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 -timeout=240s \ ./pkg/common/malloc ./pkg/common/mpool ./pkg/container/hashtable \ ./pkg/common/hashmap ./pkg/sql/colexec/group make build go vet ./pkg/common/malloc ./pkg/common/mpool ./pkg/container/hashtable \ ./pkg/common/hashmap ./pkg/sql/colexec/group ``` All commands passed after rebasing onto current main.
## What type of PR is this? - [x] Improvement ## What this PR does / why we need it Reduce contention in the shared shuffle pool without changing shuffle backpressure or buffering limits: - account only batches changed by each union instead of rescanning the whole bucket - shard batch memory tracking to remove the global accounting mutex - shard recycled batches by the local holder bound, with two padded embedded slots per shard - keep recycle lookup O(1) independently of the global shuffle bucket count - let a cold holder shard warm once without allowing a previously hot bucket to monopolize the cache The recycle capacity remains exactly `2 * maxHolders`. Sharding follows the local concurrency bound rather than `bucketNum`, which can grow as stage nodes times DOP in multi-CN plans. This is complementary to the allocator/mmap work in matrixorigin#26143 and is kept separate so both changes can be reviewed, measured, and reverted independently. Related to matrixorigin#26132. Follow-up to matrixorigin#26143. ## Which issue(s) this PR fixes None. matrixorigin#26143 already owns the closing reference for matrixorigin#26132. ## Testing - focused recycle/accounting/error-path stress under `-race -count=50` - `go test -race ./pkg/sql/colexec/shuffle` - `go test ./pkg/sql/colexec/shuffle ./pkg/sql/compile ./pkg/vm/pipeline ./pkg/vm` - `go test -race ./pkg/vm/pipeline` - `go build ./pkg/sql/colexec/shuffle` - `go vet ./pkg/sql/colexec/shuffle` - benchmark matrix covering 16/128/1024 buckets, fixed-worker concurrency, single-CN, multi-CN, drain-all, round-robin, and phased skew
…26143) ## What type of PR is this? - [ ] API-change - [x] BUG - [x] Improvement - [ ] Documentation - [ ] Feature - [x] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: Fixes matrixorigin#26132 ## What this PR does / why we need it: ### Corrected root cause The original issue attributed Q18 to MARK join falling back to LoopJoin. Controlled plans and adjacent-commit A/B refute that attribution: canonical Q18 remains a SEMI hash join, and matrixorigin#25822 is neutral/slightly faster than its direct parent. Further bisection identifies `7ffd26c7e1` from matrixorigin#25820 as the first bad commit. Besides its pipeline lifecycle changes, that commit moved all off-heap allocations at or above 128 KiB from libc to immediate `mmap`/`munmap`. Q18 builds a roughly 1 GiB, 15-million-row hash aggregate; repeated growth faults transient tables, and immediate unmap makes the final working set fault again on the next execution. Disabling only that mmap path restores the old timing, while the physical plan and scan cardinalities remain unchanged. ### Changes 1. Segment large Int64/String hash tables at 4 MiB instead of 256 MiB. Growth appends blocks and preserves existing mappings rather than allocating and faulting another full contiguous table. Load-factor decisions still use the total table size, preserving the original 1/2, 2/3, 3/4, and 4/5 thresholds. 2. Add bounded exact-size reuse for Linux mmap allocations used by zeroing `Allocate`/`ReallocZero`: - freed pages are marked `MADV_FREE`, so the kernel can reclaim them under pressure; - retained virtual memory is bounded by `min(global memory limit / 4, 1 GiB)`; - all cached mappings are actively `munmap`ed after one second of inactivity; - `Malloc` does not reuse LazyFree pages because its no-clear contract cannot safely reactivate every page; - non-Linux platforms keep the existing immediate-unmap behavior; - cached bytes are exposed as `mo_mem_offheap_inuse_bytes{type="mpool-cache"}`. ### Performance Single-node launch, TPCH 10G Q18, same data and machine: | Version | Wall time | |---|---:| | current main before this change | 0.67-0.70 s, median 0.68 s | | segmented hash table only | 0.55-0.58 s, median 0.57 s | | final branch, after allocator cache idle | 0.517 s | | final branch, burst steady state | 0.302-0.323 s, median 0.317 s | | pre-regression `0d8111ae` | 0.47-0.50 s | A 10-query process-counter sample reduced minor faults from 5,826,419 to 1,361,708 and system CPU ticks from 6,484 to 997. The cache sample contained about 1.045 GiB of `LazyFree` pages; after two idle seconds `LazyFree` returned to zero and RSS fell from 2.65 GiB to 1.61 GiB. Spot checks of Q1, Q6, Q9, Q13, Q17, and Q21 against the main baseline found no regression; all were neutral or faster. ### Ownership / unhappy-path review - Mapping ownership has one linear path: mmap caller -> cache -> zeroing caller, or cache -> idle timer -> munmap. - Capacity rejection and failed `madvise` retain caller ownership and fall back to immediate munmap. - The cache mutex removes mappings before unmapping, so take/expiry cannot double-release. - Cache growth has both a hard byte bound and an idle recycle point. - Old timer callbacks check the current idle generation before releasing mappings. ### Validation ```text .agents/skills/mo-dev/scripts/mo-cgo-test -race \ -run TestSimpleCAllocatorMmapCache -count=20 -timeout=240s \ ./pkg/common/malloc .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=180s \ ./pkg/common/malloc ./pkg/common/mpool ./pkg/container/hashtable \ ./pkg/common/hashmap ./pkg/sql/colexec/group .agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 -timeout=240s \ ./pkg/common/malloc ./pkg/common/mpool ./pkg/container/hashtable \ ./pkg/common/hashmap ./pkg/sql/colexec/group make build go vet ./pkg/common/malloc ./pkg/common/mpool ./pkg/container/hashtable \ ./pkg/common/hashmap ./pkg/sql/colexec/group ``` All commands passed after rebasing onto current main.
## What type of PR is this? - [x] Improvement ## What this PR does / why we need it Reduce contention in the shared shuffle pool without changing shuffle backpressure or buffering limits: - account only batches changed by each union instead of rescanning the whole bucket - shard batch memory tracking to remove the global accounting mutex - shard recycled batches by the local holder bound, with two padded embedded slots per shard - keep recycle lookup O(1) independently of the global shuffle bucket count - let a cold holder shard warm once without allowing a previously hot bucket to monopolize the cache The recycle capacity remains exactly `2 * maxHolders`. Sharding follows the local concurrency bound rather than `bucketNum`, which can grow as stage nodes times DOP in multi-CN plans. This is complementary to the allocator/mmap work in matrixorigin#26143 and is kept separate so both changes can be reviewed, measured, and reverted independently. Related to matrixorigin#26132. Follow-up to matrixorigin#26143. ## Which issue(s) this PR fixes None. matrixorigin#26143 already owns the closing reference for matrixorigin#26132. ## Testing - focused recycle/accounting/error-path stress under `-race -count=50` - `go test -race ./pkg/sql/colexec/shuffle` - `go test ./pkg/sql/colexec/shuffle ./pkg/sql/compile ./pkg/vm/pipeline ./pkg/vm` - `go test -race ./pkg/vm/pipeline` - `go build ./pkg/sql/colexec/shuffle` - `go vet ./pkg/sql/colexec/shuffle` - benchmark matrix covering 16/128/1024 buckets, fixed-worker concurrency, single-CN, multi-CN, drain-all, round-robin, and phased skew
What type of PR is this?
Which issue(s) this PR fixes:
Fixes #26132
What this PR does / why we need it:
Corrected root cause
The original issue attributed Q18 to MARK join falling back to LoopJoin. Controlled plans and adjacent-commit A/B refute that attribution: canonical Q18 remains a SEMI hash join, and #25822 is neutral/slightly faster than its direct parent.
Further bisection identifies
7ffd26c7e1from #25820 as the first bad commit. Besides its pipeline lifecycle changes, that commit moved all off-heap allocations at or above 128 KiB from libc to immediatemmap/munmap. Q18 builds a roughly 1 GiB, 15-million-row hash aggregate; repeated growth faults transient tables, and immediate unmap makes the final working set fault again on the next execution. Disabling only that mmap path restores the old timing, while the physical plan and scan cardinalities remain unchanged.Changes
Allocate/ReallocZero:MADV_FREE, so the kernel can reclaim them under pressure;min(global memory limit / 4, 1 GiB);munmaped after one second of inactivity;Mallocdoes not reuse LazyFree pages because its no-clear contract cannot safely reactivate every page;mo_mem_offheap_inuse_bytes{type="mpool-cache"}.Performance
Single-node launch, TPCH 10G Q18, same data and machine:
0d8111aeA 10-query process-counter sample reduced minor faults from 5,826,419 to 1,361,708 and system CPU ticks from 6,484 to 997. The cache sample contained about 1.045 GiB of
LazyFreepages; after two idle secondsLazyFreereturned to zero and RSS fell from 2.65 GiB to 1.61 GiB.Spot checks of Q1, Q6, Q9, Q13, Q17, and Q21 against the main baseline found no regression; all were neutral or faster.
Ownership / unhappy-path review
madviseretain caller ownership and fall back to immediate munmap.Validation
All commands passed after rebasing onto current main.