Skip to content

perf(vector): compact varlen data in CopyBatch (cherry-pick #25824) - #26016

Merged
XuPeng-SH merged 1 commit into
matrixorigin:4.1-devfrom
ck89119:issue-25472-4.1-dev
Jul 22, 2026
Merged

XuPeng-SH merged 1 commit into
matrixorigin:4.1-devfrom
ck89119:issue-25472-4.1-dev

Conversation

@ck89119

@ck89119 ck89119 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #25472

What this PR does / why we need it:

Cherry-pick #25824 to 4.1-dev.

  • Adds Vector.CloneToFlatCompact, which returns a deep flat copy containing only varlen payload referenced by logical rows.
  • Switches sql/util.CopyBatch to the compact clone while preserving ownership and caller cleanup.
  • Preserves fixed/varlen values, NULL and grouping bitmaps, CONST/CONST NULL flattening, attributes, row count, and source/destination independence.

## 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>
@mergify
mergify Bot requested a review from XuPeng-SH July 22, 2026 07:06
@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Jul 22, 2026
@ck89119
ck89119 marked this pull request as ready for review July 22, 2026 07:15
@ck89119
ck89119 requested a review from aunjgr as a code owner July 22, 2026 07:15
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@aunjgr aunjgr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@mergify

mergify Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@XuPeng-SH
XuPeng-SH merged commit 0d5195e into matrixorigin:4.1-dev Jul 22, 2026
23 checks passed
@ck89119
ck89119 deleted the issue-25472-4.1-dev branch July 22, 2026 08:58

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants