Skip to content

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

Merged
XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
VioletQwQ-0:codex/p0-3-copybatch-compact-clone
Jul 17, 2026
Merged

XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
VioletQwQ-0:codex/p0-3-copybatch-compact-clone

Conversation

@VioletQwQ-0

@VioletQwQ-0 VioletQwQ-0 commented Jul 16, 2026 •

Copy link
Copy Markdown
Collaborator

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:

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 base 44,260.60 98,325.07
B 29550787803 P0-1 + P0-3 49,727.19 110,314.98
A2 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.

@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 →

@XuPeng-SH XuPeng-SH 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.

The compact-copy performance improvement is real, the branch merges cleanly with current main, and the package tests/race/build/vet checks pass. I found one correctness gap in the advertised clone contract: CONST rollup vectors lose their grouping bitmap.

Concrete repro: clone vector.NewRollupConst(types.T_int8.ToType(), 3, mp). The result is correctly flattened to three NULL rows, but dst.GetGrouping().Contains(0/1/2) is false for all rows. The non-FLAT branch delegates to GetUnionAllFunction and returns before copyBitmapWithinLength; the const paths in GetUnionAllFunction expand NULL/value data but do not propagate gsp. This contradicts the PR's explicit guarantee that grouping bitmaps and CONST flattening are preserved, and makes CloneToFlatCompact inconsistent between FLAT and CONSTANT inputs.

Please preserve [0, length) grouping bits on the fallback/CONST path and add regressions for rollup CONST fixed and varlen vectors (including CONST NULL).

Comment thread pkg/container/vector/vector.go Resolved

@XuPeng-SH XuPeng-SH 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.

Re-reviewed current head 6de31a8a80 against the complete CopyBatch/vector ownership and semantics closure. The previous blocker is fixed: the fallback/CONSTANT path now copies grouping bits only within [0, length), and the regressions cover fixed and varlen CONST values plus fixed and varlen rollup CONST NULL vectors.

The fast path remains semantically equivalent to the prior UnionAll copy for logical rows while avoiding stale varlen payload: fixed/varlen data, NULLs, grouping, CONST flattening, batch attrs/row count, source independence, and partial-allocation cleanup are covered. Allocation ownership is unchanged (CopyBatch owns the cloned vectors until workspace/batch cleanup); every failure path frees the partially built vector and previously cloned columns. No waits, goroutines, persistent cache, or unbounded cross-call accumulation are introduced.

Fresh validation passed: targeted tests -count=10, targeted race tests -race -count=5, full pkg/container/vector, pkg/sql/util, and pkg/vm/engine/disttae tests, and a local benchmark confirming lower time/bytes/allocs for stale-area inputs with no compact-input regression. I also merged the latest origin/main (e2c5dc00bb) into a temporary worktree without conflicts and reran the focused tests successfully.

@mergify mergify Bot added the queued label Jul 17, 2026
@mergify

mergify Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-17 05:47 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-07-17 07:51 UTC · at dab553b98dcafa535c821f2c1bb53044b640db7f

This pull request spent 2 hours 3 minutes 52 seconds in the queue, with no time running CI.

Reason

The pull request can't be updated

This pull request seems to come from a fork, and Mergify needs the author's permission to update its branch.
The author needs to enable "Allow edits from maintainers" on this pull request, or update the branch manually.

Hint

You should update or rebase your pull request manually. If you do, this pull request will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

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

  • Requeue this pull request

@XuPeng-SH
XuPeng-SH merged commit ba53171 into matrixorigin:main Jul 17, 2026
23 of 24 checks passed
@mergify mergify Bot added dequeued and removed queued labels Jul 17, 2026
@VioletQwQ-0
VioletQwQ-0 deleted the codex/p0-3-copybatch-compact-clone branch July 20, 2026 09:58
XuPeng-SH pushed a commit that referenced this pull request Jul 22, 2026
…26016)

## 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 #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.

Co-authored-by: 和光同尘 <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## 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>
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## 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>
VioletQwQ-0 added a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## 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>

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.

3 participants