Repository navigation
perf(vector): compact varlen data in CopyBatch (cherry-pick #25824) - #26016
Merged
Merged
Conversation
## What type of PR is this? - [ ] API-change - [ ] BUG - [x] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: issue matrixorigin#25472 ## What this PR does / why we need it: ### Summary - Adds `Vector.CloneToFlatCompact`, which returns a deep flat copy containing only varlen payload referenced by logical rows. - Precomputes live varlen bytes, allocates the destination area once, and rewrites offsets instead of repeatedly growing the area. - Preserves fixed/varlen values, NULL and grouping bitmaps, CONST/CONST NULL flattening, attributes, row count, and source/destination independence. - Frees all partial mpool allocations on every error path. - Switches `sql/util.CopyBatch` to the compact clone while leaving ownership and caller cleanup unchanged. ### Why After row selection, a vector can retain a much larger stale varlen area. CopyBatch previously rebuilt it through repeated UnionAll growth, producing avoidable MPool Grow/reAlloc traffic, mutex contention, allocation bytes, and GC work. ### Compatibility and non-goals This introduces one Go API and changes only in-memory copying. It does not change SQL behavior, wire or persisted formats, configuration, `Dup`/`DupOffHeap` semantics, or batch ownership. ### Validation - `go build ./pkg/container/vector ./pkg/sql/util ./pkg/vm/engine/disttae` - `go vet ./pkg/container/vector ./pkg/sql/util ./pkg/vm/engine/disttae` - Full tests for vector, sql/util, and disttae - Clone and CopyBatch race tests repeated 10 times - Allocation-failure test verifies the destination mpool returns to zero - Combined P0-1/P0-3 branch: full test and race for lockservice, vector, sql/util, and disttae Current-main local benchmark, 10 samples: | Input | old median | compact median | time | bytes/op | allocs/op | |---|---:|---:|---:|---:|---:| | stale varlen area | 52.98 us | 19.48 us | -63.2% | 933,936 -> 141,456 (-84.9%) | 42 -> 32 | | already compact | 20.60 us | 18.14 us | -11.9% | 141,616 -> 141,456 | 42 -> 32 | ### Current-main integration evidence The fixed current-main A/B/A used base `1eeb912ada6f5eb0a29ac20503a8d7f50c3d3ebe` and combined candidate `e7568b1568ab8d74e499e1ff3c4c6f44f6b85f26`: | Run | Commit | tpmC | tpmTOTAL | |---|---|---:|---:| | A1 [29550222344](https://github.com/matrixorigin/mo-auto-test/actions/runs/29550222344) | base | 44,260.60 | 98,325.07 | | B [29550787803](https://github.com/matrixorigin/mo-auto-test/actions/runs/29550787803) | P0-1 + P0-3 | 49,727.19 | 110,314.98 | | A2 [29551321777](https://github.com/matrixorigin/mo-auto-test/actions/runs/29551321777) | base | 42,935.42 | 95,533.01 | Against the A1/A2 mean, B improved tpmC by **14.0584%** and tpmTOTAL by **13.8100%**. Baseline drift was 2.994%; all runs had zero transaction errors and passed consistency verification. The focused P0-3 profile matches the implementation mechanism. Against the A1/A2 mean, `CopyBatch` CPU fell 75.36%, its allocation space fell 95.76%, `MPool.Grow` CPU fell 62.69%, and GC cycles fell 28.98%. This combined A/B/A is integration evidence. The same P0-3 patch previously produced +13.90% tpmC and +14.14% tpmTOTAL in isolated run 29392593786. The profiled implementation commit was `b290103c8`; current PR head `6de31a8a` adds only the bounded grouping-bitmap copy on the fallback path for correctness, so it was not followed by another TPCC run. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
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? |
aunjgr
approved these changes
Jul 22, 2026
aunjgr
left a comment
Contributor
There was a problem hiding this comment.
Reviewed independently. The compact clone preserves logical values, null/grouping state, CONST flattening, and ownership while rebuilding varlen area from live rows only. Error cleanup and CopyBatch partial cleanup are covered. No actionable issues found.
Contributor
|
Tick the box to add this pull request to the merge queue (same as
|
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What type of PR is this?
Which issue(s) this PR fixes:
issue #25472
What this PR does / why we need it:
Cherry-pick #25824 to 4.1-dev.
Vector.CloneToFlatCompact, which returns a deep flat copy containing only varlen payload referenced by logical rows.sql/util.CopyBatchto the compact clone while preserving ownership and caller cleanup.