Repository navigation
perf: linearize saved aggregate argument recovery - #28168
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? |
1e558f8 to
3bb0a6a
Compare
3bb0a6a to
e9b552a
Compare
Signed-off-by: XuPeng-SH <[email protected]>
e9b552a to
18cd84c
Compare
LeftHandCold
left a comment
There was a problem hiding this comment.
Reviewed exact HEAD 18cd84c. No blocking findings.
The optimized recovery path preserves the existing wire format and DISTINCT/input-order semantics. Accounted insertion computes one deterministic AddPlan and its exact consumed/trailing footprint, checks duplicates before otherwise-unnecessary growth, and only publishes a relocated arena after allocation and GrowArena succeed. On failure the old arena, published keys, cached inserter, and account state remain valid; on success the inserter is invalidated before reuse and the old mpool allocation has one release owner.
All production callers provide key/value bytes outside the target arena during relocation, and no target iterator crosses GrowArena. Fixed-width, opaque varlen, non-DISTINCT ordered recovery, post-relocation duplicate handling, continued writes, and capacity rejection are covered by focused tests. The unaccounted legacy rebuild path and bounded 64 KiB DISTINCT spill work-set policy remain unchanged.
Q1 ownership/cleanup: one publication point and one old/new arena cleanup owner. Q2 liveness: no new waits or asynchronous work. Q3 boundedness: account capacity, allocator limits, and bounded spill growth remain enforced; geometric recovery growth has a linear fallback under pressure.
The change is an internal performance optimization related to #27720, not an implementation of that issue's full adaptive execution model, so the broader issue correctly remains open. Exact-head PR validation and shared build are green; repository UT/SCA are still in progress, while the PR records focused normal, repeated race, lint, correctness, and benchmark evidence.
## What type of PR is this? - [ ] API-change - [ ] BUG - [x] Improvement - [ ] Documentation - [ ] Feature - [x] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: Related to matrixorigin#27720. ## What this PR does / why we need it: Restoring saved aggregate arguments rebuilt the ordered skiplist one key at a time. Once its arena filled, every fixed-size growth allocated a new buffer and reinserted every existing node. A high-NDV partial DISTINCT state therefore paid superlinear relocation/rebuild cost before the normal merge even began. This change: - grows accounted argument arenas through `GrowArena`, relocating the offset-based skiplist in one buffer copy instead of rebuilding every node; - reuses one `arenaskl.Inserter` while reading the canonical sorted argument stream, avoiding a root search for every restored key; - computes one deterministic `AddPlan` and exact footprint per accounted insertion, using it for both capacity admission and publication; - hoists fixed-width wire shape, scratch sizing, and group-prefix construction out of the per-argument recovery loop; - checks duplicates only at a capacity boundary, preserving preflight's zero-allocation publication contract without adding a second search to the common new-key path; and - preserves the partial/spill wire format, DISTINCT equality, input-order handling, allocation ownership, memory-pressure fallback, and the bounded 64 KiB work-set growth policy added on current `main`. ### Performance The original end-to-end measurement used a clean build of `origin/main@0fdc39e`, the same 10M-row table, and an exact result oracle: ```sql SELECT COUNT(DISTINCT UserID), COUNT(DISTINCT SearchPhrase) FROM roi_group.hits WHERE UserID <= ?; ``` | selected rows | measured main | this PR | speedup | |---:|---:|---:|---:| | 1M | 2.623 s | 0.998 s | 2.63x | | 2M | 7.855 s | 1.800 s | 4.36x | | 5M | 40.752 s | 4.353 s | 9.36x | The candidate completed the full 10M rows in 8.39-8.45 s with exact result `(10000000, 9896908)`. The focused 250K-key recovery benchmark improved from 381 ms/op on the unoptimized reader to 12.76-12.84 ms/op on the final rebased head. CPU profiling moved the 10M-row `readStateArg` cumulative work below the old 2M-row cost (2.25 s versus 6.51 s). Non-target controls did not regress: a 10M-row VARCHAR `GROUP BY` remained 6.16-6.48 s on the measured main and 6.19-6.28 s on the candidate; single-DISTINCT and mixed grouped aggregates remained within run-to-run variation. ### Latest-main integration Final head `18cd84cb6516ee7d0d71a5cbea8667cc04729eef` is rebased on `origin/main@922e792`, including bounded exact DISTINCT-key spill. Rebase testing exposed and fixed an interaction where preflight intentionally reserved no space for existing/work-unit duplicate keys, but publication attempted an unnecessary 512 KiB growth before discovering the duplicate. The fix performs a no-allocation lookup only when capacity is otherwise insufficient. The complete new aggexec and Group DISTINCT-spill suites pass with the bounded growth policy intact. ### Test plan Local deterministic validation on the final rebased head: - full `mo-cgo-test -count=1 ./pkg/sql/colexec/aggexec`; - full `mo-cgo-test -count=1 ./pkg/sql/colexec/group`; - fixed-width and VARCHAR saved-state round trips across repeated arena growth and two group-key ranges; - post-relocation duplicate detection plus insertion of a new boundary key; - non-DISTINCT ordered `GROUP_CONCAT` round trip with two groups, opaque VARCHAR payloads, several arena relocations, exact output-order oracle, and continued writes after recovery; - capacity rejection preserving arena identity, published keys, cached Inserter usability, account usage, and zero leaked bytes; - latest-main existing/work-unit duplicate merge regression; - the three focused boundary tests repeated 20 times, with the multi-group recovery case repeated 100 times under `-race`; - targeted `golangci-lint` and `molint`: zero findings; - focused 250K-key recovery benchmark with allocation reporting. Earlier candidate black-box validation on 55 covered the following matrix. The final loop-hoisting and capacity-boundary refinement was validated locally by the exact UT/race/benchmark contracts above and was not rerun on 55: - global and 100-group aggregation; - one and two DISTINCT aggregates, plus multi-column DISTINCT arguments; - fixed-width, VARCHAR, and multi-column keys; - low NDV, near-row-count NDV, duplicates, NULL expressions, and empty input; - DISTINCT mixed with `COUNT`, `SUM`, and `MIN`; - default DOP and `max_dop=1`; - forced aggregate spill (`agg_spill_mem=1 MiB`), producing 160 MiB of spill in the profiled run and returning the exact oracle; - input-order/order-sensitive `GROUP_CONCAT(DISTINCT ...)` and `MEDIAN(DISTINCT ...)`; - client cancellation followed by a same-instance health/result query; - 1M, 2M, 5M, and 10M scaling plus CPU profiles. The 55 service, generated database, profiles, symlink, logs, and temporary files were removed after validation; no `mo-service` process remains. --------- Signed-off-by: XuPeng-SH <[email protected]>
## What type of PR is this? - [ ] API-change - [ ] BUG - [x] Improvement - [ ] Documentation - [ ] Feature - [x] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: Related to matrixorigin#27720. ## What this PR does / why we need it: Restoring saved aggregate arguments rebuilt the ordered skiplist one key at a time. Once its arena filled, every fixed-size growth allocated a new buffer and reinserted every existing node. A high-NDV partial DISTINCT state therefore paid superlinear relocation/rebuild cost before the normal merge even began. This change: - grows accounted argument arenas through `GrowArena`, relocating the offset-based skiplist in one buffer copy instead of rebuilding every node; - reuses one `arenaskl.Inserter` while reading the canonical sorted argument stream, avoiding a root search for every restored key; - computes one deterministic `AddPlan` and exact footprint per accounted insertion, using it for both capacity admission and publication; - hoists fixed-width wire shape, scratch sizing, and group-prefix construction out of the per-argument recovery loop; - checks duplicates only at a capacity boundary, preserving preflight's zero-allocation publication contract without adding a second search to the common new-key path; and - preserves the partial/spill wire format, DISTINCT equality, input-order handling, allocation ownership, memory-pressure fallback, and the bounded 64 KiB work-set growth policy added on current `main`. ### Performance The original end-to-end measurement used a clean build of `origin/main@0fdc39e`, the same 10M-row table, and an exact result oracle: ```sql SELECT COUNT(DISTINCT UserID), COUNT(DISTINCT SearchPhrase) FROM roi_group.hits WHERE UserID <= ?; ``` | selected rows | measured main | this PR | speedup | |---:|---:|---:|---:| | 1M | 2.623 s | 0.998 s | 2.63x | | 2M | 7.855 s | 1.800 s | 4.36x | | 5M | 40.752 s | 4.353 s | 9.36x | The candidate completed the full 10M rows in 8.39-8.45 s with exact result `(10000000, 9896908)`. The focused 250K-key recovery benchmark improved from 381 ms/op on the unoptimized reader to 12.76-12.84 ms/op on the final rebased head. CPU profiling moved the 10M-row `readStateArg` cumulative work below the old 2M-row cost (2.25 s versus 6.51 s). Non-target controls did not regress: a 10M-row VARCHAR `GROUP BY` remained 6.16-6.48 s on the measured main and 6.19-6.28 s on the candidate; single-DISTINCT and mixed grouped aggregates remained within run-to-run variation. ### Latest-main integration Final head `18cd84cb6516ee7d0d71a5cbea8667cc04729eef` is rebased on `origin/main@922e792`, including bounded exact DISTINCT-key spill. Rebase testing exposed and fixed an interaction where preflight intentionally reserved no space for existing/work-unit duplicate keys, but publication attempted an unnecessary 512 KiB growth before discovering the duplicate. The fix performs a no-allocation lookup only when capacity is otherwise insufficient. The complete new aggexec and Group DISTINCT-spill suites pass with the bounded growth policy intact. ### Test plan Local deterministic validation on the final rebased head: - full `mo-cgo-test -count=1 ./pkg/sql/colexec/aggexec`; - full `mo-cgo-test -count=1 ./pkg/sql/colexec/group`; - fixed-width and VARCHAR saved-state round trips across repeated arena growth and two group-key ranges; - post-relocation duplicate detection plus insertion of a new boundary key; - non-DISTINCT ordered `GROUP_CONCAT` round trip with two groups, opaque VARCHAR payloads, several arena relocations, exact output-order oracle, and continued writes after recovery; - capacity rejection preserving arena identity, published keys, cached Inserter usability, account usage, and zero leaked bytes; - latest-main existing/work-unit duplicate merge regression; - the three focused boundary tests repeated 20 times, with the multi-group recovery case repeated 100 times under `-race`; - targeted `golangci-lint` and `molint`: zero findings; - focused 250K-key recovery benchmark with allocation reporting. Earlier candidate black-box validation on 55 covered the following matrix. The final loop-hoisting and capacity-boundary refinement was validated locally by the exact UT/race/benchmark contracts above and was not rerun on 55: - global and 100-group aggregation; - one and two DISTINCT aggregates, plus multi-column DISTINCT arguments; - fixed-width, VARCHAR, and multi-column keys; - low NDV, near-row-count NDV, duplicates, NULL expressions, and empty input; - DISTINCT mixed with `COUNT`, `SUM`, and `MIN`; - default DOP and `max_dop=1`; - forced aggregate spill (`agg_spill_mem=1 MiB`), producing 160 MiB of spill in the profiled run and returning the exact oracle; - input-order/order-sensitive `GROUP_CONCAT(DISTINCT ...)` and `MEDIAN(DISTINCT ...)`; - client cancellation followed by a same-instance health/result query; - 1M, 2M, 5M, and 10M scaling plus CPU profiles. The 55 service, generated database, profiles, symlink, logs, and temporary files were removed after validation; no `mo-service` process remains. --------- Signed-off-by: XuPeng-SH <[email protected]>
## What type of PR is this? - [ ] API-change - [ ] BUG - [x] Improvement - [ ] Documentation - [ ] Feature - [x] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: Related to matrixorigin#27720. ## What this PR does / why we need it: Restoring saved aggregate arguments rebuilt the ordered skiplist one key at a time. Once its arena filled, every fixed-size growth allocated a new buffer and reinserted every existing node. A high-NDV partial DISTINCT state therefore paid superlinear relocation/rebuild cost before the normal merge even began. This change: - grows accounted argument arenas through `GrowArena`, relocating the offset-based skiplist in one buffer copy instead of rebuilding every node; - reuses one `arenaskl.Inserter` while reading the canonical sorted argument stream, avoiding a root search for every restored key; - computes one deterministic `AddPlan` and exact footprint per accounted insertion, using it for both capacity admission and publication; - hoists fixed-width wire shape, scratch sizing, and group-prefix construction out of the per-argument recovery loop; - checks duplicates only at a capacity boundary, preserving preflight's zero-allocation publication contract without adding a second search to the common new-key path; and - preserves the partial/spill wire format, DISTINCT equality, input-order handling, allocation ownership, memory-pressure fallback, and the bounded 64 KiB work-set growth policy added on current `main`. ### Performance The original end-to-end measurement used a clean build of `origin/main@0fdc39e`, the same 10M-row table, and an exact result oracle: ```sql SELECT COUNT(DISTINCT UserID), COUNT(DISTINCT SearchPhrase) FROM roi_group.hits WHERE UserID <= ?; ``` | selected rows | measured main | this PR | speedup | |---:|---:|---:|---:| | 1M | 2.623 s | 0.998 s | 2.63x | | 2M | 7.855 s | 1.800 s | 4.36x | | 5M | 40.752 s | 4.353 s | 9.36x | The candidate completed the full 10M rows in 8.39-8.45 s with exact result `(10000000, 9896908)`. The focused 250K-key recovery benchmark improved from 381 ms/op on the unoptimized reader to 12.76-12.84 ms/op on the final rebased head. CPU profiling moved the 10M-row `readStateArg` cumulative work below the old 2M-row cost (2.25 s versus 6.51 s). Non-target controls did not regress: a 10M-row VARCHAR `GROUP BY` remained 6.16-6.48 s on the measured main and 6.19-6.28 s on the candidate; single-DISTINCT and mixed grouped aggregates remained within run-to-run variation. ### Latest-main integration Final head `18cd84cb6516ee7d0d71a5cbea8667cc04729eef` is rebased on `origin/main@922e792`, including bounded exact DISTINCT-key spill. Rebase testing exposed and fixed an interaction where preflight intentionally reserved no space for existing/work-unit duplicate keys, but publication attempted an unnecessary 512 KiB growth before discovering the duplicate. The fix performs a no-allocation lookup only when capacity is otherwise insufficient. The complete new aggexec and Group DISTINCT-spill suites pass with the bounded growth policy intact. ### Test plan Local deterministic validation on the final rebased head: - full `mo-cgo-test -count=1 ./pkg/sql/colexec/aggexec`; - full `mo-cgo-test -count=1 ./pkg/sql/colexec/group`; - fixed-width and VARCHAR saved-state round trips across repeated arena growth and two group-key ranges; - post-relocation duplicate detection plus insertion of a new boundary key; - non-DISTINCT ordered `GROUP_CONCAT` round trip with two groups, opaque VARCHAR payloads, several arena relocations, exact output-order oracle, and continued writes after recovery; - capacity rejection preserving arena identity, published keys, cached Inserter usability, account usage, and zero leaked bytes; - latest-main existing/work-unit duplicate merge regression; - the three focused boundary tests repeated 20 times, with the multi-group recovery case repeated 100 times under `-race`; - targeted `golangci-lint` and `molint`: zero findings; - focused 250K-key recovery benchmark with allocation reporting. Earlier candidate black-box validation on 55 covered the following matrix. The final loop-hoisting and capacity-boundary refinement was validated locally by the exact UT/race/benchmark contracts above and was not rerun on 55: - global and 100-group aggregation; - one and two DISTINCT aggregates, plus multi-column DISTINCT arguments; - fixed-width, VARCHAR, and multi-column keys; - low NDV, near-row-count NDV, duplicates, NULL expressions, and empty input; - DISTINCT mixed with `COUNT`, `SUM`, and `MIN`; - default DOP and `max_dop=1`; - forced aggregate spill (`agg_spill_mem=1 MiB`), producing 160 MiB of spill in the profiled run and returning the exact oracle; - input-order/order-sensitive `GROUP_CONCAT(DISTINCT ...)` and `MEDIAN(DISTINCT ...)`; - client cancellation followed by a same-instance health/result query; - 1M, 2M, 5M, and 10M scaling plus CPU profiles. The 55 service, generated database, profiles, symlink, logs, and temporary files were removed after validation; no `mo-service` process remains. --------- Signed-off-by: XuPeng-SH <[email protected]>
What type of PR is this?
Which issue(s) this PR fixes:
Related to #27720.
What this PR does / why we need it:
Restoring saved aggregate arguments rebuilt the ordered skiplist one key at a
time. Once its arena filled, every fixed-size growth allocated a new buffer and
reinserted every existing node. A high-NDV partial DISTINCT state therefore paid
superlinear relocation/rebuild cost before the normal merge even began.
This change:
GrowArena, relocating theoffset-based skiplist in one buffer copy instead of rebuilding every node;
arenaskl.Inserterwhile reading the canonical sorted argumentstream, avoiding a root search for every restored key;
AddPlanand exact footprint per accountedinsertion, using it for both capacity admission and publication;
out of the per-argument recovery loop;
zero-allocation publication contract without adding a second search to the
common new-key path; and
handling, allocation ownership, memory-pressure fallback, and the bounded
64 KiB work-set growth policy added on current
main.Performance
The original end-to-end measurement used a clean build of
origin/main@0fdc39e2363ec12439502cdaac22b48d49320e12, the same 10M-row table,and an exact result oracle:
The candidate completed the full 10M rows in 8.39-8.45 s with exact result
(10000000, 9896908). The focused 250K-key recovery benchmark improved from381 ms/op on the unoptimized reader to 12.76-12.84 ms/op on the final rebased
head. CPU profiling moved the 10M-row
readStateArgcumulative work below theold 2M-row cost (2.25 s versus 6.51 s).
Non-target controls did not regress: a 10M-row VARCHAR
GROUP BYremained6.16-6.48 s on the measured main and 6.19-6.28 s on the candidate;
single-DISTINCT and mixed grouped aggregates remained within run-to-run
variation.
Latest-main integration
Final head
18cd84cb6516ee7d0d71a5cbea8667cc04729eefis rebased onorigin/main@922e792ebf14b0f25d5028af59479e334e8bf692, including bounded exactDISTINCT-key spill. Rebase testing exposed and fixed an interaction where
preflight intentionally reserved no space for existing/work-unit duplicate keys,
but publication attempted an unnecessary 512 KiB growth before discovering the
duplicate. The fix performs a no-allocation lookup only when capacity is
otherwise insufficient. The complete new aggexec and Group DISTINCT-spill suites
pass with the bounded growth policy intact.
Test plan
Local deterministic validation on the final rebased head:
mo-cgo-test -count=1 ./pkg/sql/colexec/aggexec;mo-cgo-test -count=1 ./pkg/sql/colexec/group;GROUP_CONCATround trip with two groups, opaque VARCHARpayloads, several arena relocations, exact output-order oracle, and continued
writes after recovery;
usability, account usage, and zero leaked bytes;
-race;golangci-lintandmolint: zero findings;Earlier candidate black-box validation on 55 covered the following matrix. The final
loop-hoisting and capacity-boundary refinement was validated locally by the exact
UT/race/benchmark contracts above and was not rerun on 55:
COUNT,SUM, andMIN;max_dop=1;agg_spill_mem=1 MiB), producing 160 MiB of spill inthe profiled run and returning the exact oracle;
GROUP_CONCAT(DISTINCT ...)andMEDIAN(DISTINCT ...);The 55 service, generated database, profiles, symlink, logs, and temporary files
were removed after validation; no
mo-serviceprocess remains.