Skip to content

perf(objectio): bulk-copy disjoint sorted batches - #28074

Merged
XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/optimize-load-indexed-followup
Sep 3, 2026
Merged

XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/optimize-load-indexed-followup

Conversation

@XuPeng-SH

Copy link
Copy Markdown
Contributor

Summary

  • detect when already-sorted input batch key ranges are strictly disjoint
  • concatenate those batches with bounded UnionWindow copies instead of a row-at-a-time heap merge
  • retain the existing k-way merge for overlapping or equal boundaries and floating-point keys
  • preserve sink-buffer and putBack ownership, including the existing comparator-visible row domain
  • avoid extra fallback allocations by reusing row-index scratch and building the heap only when required
  • make the general path handle all-empty batch sets without indexing a missing heap element

Related to #27775. This PR does not close the issue; distributed 100M/1B confirmation remains the final acceptance step.

First-principles design

MergeSortBatches already requires every input batch to be sorted by the same ascending key relation. If the batches can be ordered so that every next minimum is strictly greater than the previous maximum, their sorted union is exactly their concatenation. Per-row heap comparison is then redundant.

The fast path is deliberately conservative:

  • overlapping ranges fall back
  • equal range boundaries fall back so duplicate-key tie behavior remains unchanged
  • FLOAT and DOUBLE fall back because NaN does not define the total order needed by endpoint proof
  • NULL boundaries use the same NULL-first relation as the existing merge
  • output remains bounded by objectio.BlockMaxRows
  • each source is returned only after its copied data is independent of that source
  • failures return the current buffer and retain the existing cleanup owner

There is no LOAD, index kind, schema, issue, or data-size branch.

Performance evidence

Apple M4, Go 1.26.4, 16 input batches x 8192 rows, two int64 columns:

Shape main PR Allocations
strictly disjoint 12.95-13.81 ms 48.48-56.06 us 7 alloc/op on both
fully overlapping 15.67-15.89 ms 15.52-16.78 ms 7 alloc/op on both

The disjoint merge itself improves about 99.6%; the required overlap fallback has no measurable median regression.

Fresh-process local 30M LOAD DATA with composite PK, secondary index, UNIQUE index, and a no-index control:

Revision/run Indexed Control Ratio
current-main baseline 9.32 s 2.64 s 3.53x
PR run 1 7.40 s 2.76 s 2.68x
PR final run 8.57 s 3.05 s 2.81x

The control-normalized remaining overhead improves about 20-24% across the two fresh PR runs. Lowering the existing 1 GiB promotion threshold was separately rejected: forcing that path at 10M was about 40% slower.

Correctness and unhappy paths

The 30M black-box run verified:

  • base and control counts, sums, and payload min/max match
  • base, hidden secondary, and hidden UNIQUE tables each contain 30M rows
  • forced UNIQUE point lookups at first/middle/last keys return exact rows
  • forced secondary range lookup returns the expected 60,030 rows

Focused coverage includes:

  • reversed disjoint ranges and exact BlockMaxRows boundary splitting
  • overlapping ranges
  • equal boundaries
  • mixed/all-NULL boundaries
  • varlena keys
  • sink failure propagation and buffer ownership
  • all-empty floating-point/general path
  • putBack ordering and payload association

Validation

  • .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=120s ./pkg/objectio/mergeutil
  • .agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 -timeout=240s ./pkg/objectio/mergeutil
  • .agents/skills/mo-dev/scripts/mo-cgo-test -run '^TestMergeSortBatches$' -count=1 -timeout=120s ./pkg/sql/colexec
  • .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=180s ./pkg/objectio/ioutil
  • go vet ./pkg/objectio/mergeutil ./pkg/objectio/ioutil with repository CGo flags
  • make build
  • fresh-process 30M indexed/control SQL validation described above

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

@LeftHandCold LeftHandCold left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed exact HEAD cafc61be6fd2ad62c5aaef7a43d384b16a922df8 against base ffc8d9f0625c76e87ba967984ce19db2d082b0ba. No blocking code findings.

The fast path is conservatively gated by strict, comparator-consistent range separation; equal/overlapping ranges and FLOAT/DOUBLE retain the existing k-way merge. Block splitting remains bounded by objectio.BlockMaxRows, and source batches are returned only after UnionWindow has made an independent copy. Sink/pre-extend failures preserve the existing buffer and outer cleanup ownership.

Q1 ownership: pass. Q2 waits/control: pass; no new asynchronous or unbounded wait edge. Q3 growth: pass; order/heap scratch is bounded by input batch count and output remains one block at a time.

Evidence reviewed: exact-head focused/race/consumer/build and 30M indexed/control evidence in the PR; shared CI build is green and the remaining CI jobs are still governed by branch protection. A local duplicate of the focused package test was not run because this detached worktree has no matching cgo/libmo.dylib; I did not reuse a mismatched native artifact. Distributed 100M/1B confirmation appropriately remains follow-up acceptance for #27775 rather than a correctness blocker for this general optimization.

@mergify

mergify Bot commented Sep 3, 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 deleted the codex/optimize-load-indexed-followup branch September 3, 2026 11:54
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## Summary

- detect when already-sorted input batch key ranges are strictly
disjoint
- concatenate those batches with bounded UnionWindow copies instead of a
row-at-a-time heap merge
- retain the existing k-way merge for overlapping or equal boundaries
and floating-point keys
- preserve sink-buffer and putBack ownership, including the existing
comparator-visible row domain
- avoid extra fallback allocations by reusing row-index scratch and
building the heap only when required
- make the general path handle all-empty batch sets without indexing a
missing heap element

Related to matrixorigin#27775. This PR does not close the issue; distributed 100M/1B
confirmation remains the final acceptance step.

## First-principles design

MergeSortBatches already requires every input batch to be sorted by the
same ascending key relation. If the batches can be ordered so that every
next minimum is strictly greater than the previous maximum, their sorted
union is exactly their concatenation. Per-row heap comparison is then
redundant.

The fast path is deliberately conservative:

- overlapping ranges fall back
- equal range boundaries fall back so duplicate-key tie behavior remains
unchanged
- FLOAT and DOUBLE fall back because NaN does not define the total order
needed by endpoint proof
- NULL boundaries use the same NULL-first relation as the existing merge
- output remains bounded by objectio.BlockMaxRows
- each source is returned only after its copied data is independent of
that source
- failures return the current buffer and retain the existing cleanup
owner

There is no LOAD, index kind, schema, issue, or data-size branch.

## Performance evidence

Apple M4, Go 1.26.4, 16 input batches x 8192 rows, two int64 columns:

| Shape | main | PR | Allocations |
|---|---:|---:|---:|
| strictly disjoint | 12.95-13.81 ms | 48.48-56.06 us | 7 alloc/op on
both |
| fully overlapping | 15.67-15.89 ms | 15.52-16.78 ms | 7 alloc/op on
both |

The disjoint merge itself improves about 99.6%; the required overlap
fallback has no measurable median regression.

Fresh-process local 30M LOAD DATA with composite PK, secondary index,
UNIQUE index, and a no-index control:

| Revision/run | Indexed | Control | Ratio |
|---|---:|---:|---:|
| current-main baseline | 9.32 s | 2.64 s | 3.53x |
| PR run 1 | 7.40 s | 2.76 s | 2.68x |
| PR final run | 8.57 s | 3.05 s | 2.81x |

The control-normalized remaining overhead improves about 20-24% across
the two fresh PR runs. Lowering the existing 1 GiB promotion threshold
was separately rejected: forcing that path at 10M was about 40% slower.

## Correctness and unhappy paths

The 30M black-box run verified:

- base and control counts, sums, and payload min/max match
- base, hidden secondary, and hidden UNIQUE tables each contain 30M rows
- forced UNIQUE point lookups at first/middle/last keys return exact
rows
- forced secondary range lookup returns the expected 60,030 rows

Focused coverage includes:

- reversed disjoint ranges and exact BlockMaxRows boundary splitting
- overlapping ranges
- equal boundaries
- mixed/all-NULL boundaries
- varlena keys
- sink failure propagation and buffer ownership
- all-empty floating-point/general path
- putBack ordering and payload association

## Validation

- .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=120s
./pkg/objectio/mergeutil
- .agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 -timeout=240s
./pkg/objectio/mergeutil
- .agents/skills/mo-dev/scripts/mo-cgo-test -run
'^TestMergeSortBatches$' -count=1 -timeout=120s ./pkg/sql/colexec
- .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=180s
./pkg/objectio/ioutil
- go vet ./pkg/objectio/mergeutil ./pkg/objectio/ioutil with repository
CGo flags
- make build
- fresh-process 30M indexed/control SQL validation described above
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## Summary

- detect when already-sorted input batch key ranges are strictly
disjoint
- concatenate those batches with bounded UnionWindow copies instead of a
row-at-a-time heap merge
- retain the existing k-way merge for overlapping or equal boundaries
and floating-point keys
- preserve sink-buffer and putBack ownership, including the existing
comparator-visible row domain
- avoid extra fallback allocations by reusing row-index scratch and
building the heap only when required
- make the general path handle all-empty batch sets without indexing a
missing heap element

Related to matrixorigin#27775. This PR does not close the issue; distributed 100M/1B
confirmation remains the final acceptance step.

## First-principles design

MergeSortBatches already requires every input batch to be sorted by the
same ascending key relation. If the batches can be ordered so that every
next minimum is strictly greater than the previous maximum, their sorted
union is exactly their concatenation. Per-row heap comparison is then
redundant.

The fast path is deliberately conservative:

- overlapping ranges fall back
- equal range boundaries fall back so duplicate-key tie behavior remains
unchanged
- FLOAT and DOUBLE fall back because NaN does not define the total order
needed by endpoint proof
- NULL boundaries use the same NULL-first relation as the existing merge
- output remains bounded by objectio.BlockMaxRows
- each source is returned only after its copied data is independent of
that source
- failures return the current buffer and retain the existing cleanup
owner

There is no LOAD, index kind, schema, issue, or data-size branch.

## Performance evidence

Apple M4, Go 1.26.4, 16 input batches x 8192 rows, two int64 columns:

| Shape | main | PR | Allocations |
|---|---:|---:|---:|
| strictly disjoint | 12.95-13.81 ms | 48.48-56.06 us | 7 alloc/op on
both |
| fully overlapping | 15.67-15.89 ms | 15.52-16.78 ms | 7 alloc/op on
both |

The disjoint merge itself improves about 99.6%; the required overlap
fallback has no measurable median regression.

Fresh-process local 30M LOAD DATA with composite PK, secondary index,
UNIQUE index, and a no-index control:

| Revision/run | Indexed | Control | Ratio |
|---|---:|---:|---:|
| current-main baseline | 9.32 s | 2.64 s | 3.53x |
| PR run 1 | 7.40 s | 2.76 s | 2.68x |
| PR final run | 8.57 s | 3.05 s | 2.81x |

The control-normalized remaining overhead improves about 20-24% across
the two fresh PR runs. Lowering the existing 1 GiB promotion threshold
was separately rejected: forcing that path at 10M was about 40% slower.

## Correctness and unhappy paths

The 30M black-box run verified:

- base and control counts, sums, and payload min/max match
- base, hidden secondary, and hidden UNIQUE tables each contain 30M rows
- forced UNIQUE point lookups at first/middle/last keys return exact
rows
- forced secondary range lookup returns the expected 60,030 rows

Focused coverage includes:

- reversed disjoint ranges and exact BlockMaxRows boundary splitting
- overlapping ranges
- equal boundaries
- mixed/all-NULL boundaries
- varlena keys
- sink failure propagation and buffer ownership
- all-empty floating-point/general path
- putBack ordering and payload association

## Validation

- .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=120s
./pkg/objectio/mergeutil
- .agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 -timeout=240s
./pkg/objectio/mergeutil
- .agents/skills/mo-dev/scripts/mo-cgo-test -run
'^TestMergeSortBatches$' -count=1 -timeout=120s ./pkg/sql/colexec
- .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=180s
./pkg/objectio/ioutil
- go vet ./pkg/objectio/mergeutil ./pkg/objectio/ioutil with repository
CGo flags
- make build
- fresh-process 30M indexed/control SQL validation described above
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## Summary

- detect when already-sorted input batch key ranges are strictly
disjoint
- concatenate those batches with bounded UnionWindow copies instead of a
row-at-a-time heap merge
- retain the existing k-way merge for overlapping or equal boundaries
and floating-point keys
- preserve sink-buffer and putBack ownership, including the existing
comparator-visible row domain
- avoid extra fallback allocations by reusing row-index scratch and
building the heap only when required
- make the general path handle all-empty batch sets without indexing a
missing heap element

Related to matrixorigin#27775. This PR does not close the issue; distributed 100M/1B
confirmation remains the final acceptance step.

## First-principles design

MergeSortBatches already requires every input batch to be sorted by the
same ascending key relation. If the batches can be ordered so that every
next minimum is strictly greater than the previous maximum, their sorted
union is exactly their concatenation. Per-row heap comparison is then
redundant.

The fast path is deliberately conservative:

- overlapping ranges fall back
- equal range boundaries fall back so duplicate-key tie behavior remains
unchanged
- FLOAT and DOUBLE fall back because NaN does not define the total order
needed by endpoint proof
- NULL boundaries use the same NULL-first relation as the existing merge
- output remains bounded by objectio.BlockMaxRows
- each source is returned only after its copied data is independent of
that source
- failures return the current buffer and retain the existing cleanup
owner

There is no LOAD, index kind, schema, issue, or data-size branch.

## Performance evidence

Apple M4, Go 1.26.4, 16 input batches x 8192 rows, two int64 columns:

| Shape | main | PR | Allocations |
|---|---:|---:|---:|
| strictly disjoint | 12.95-13.81 ms | 48.48-56.06 us | 7 alloc/op on
both |
| fully overlapping | 15.67-15.89 ms | 15.52-16.78 ms | 7 alloc/op on
both |

The disjoint merge itself improves about 99.6%; the required overlap
fallback has no measurable median regression.

Fresh-process local 30M LOAD DATA with composite PK, secondary index,
UNIQUE index, and a no-index control:

| Revision/run | Indexed | Control | Ratio |
|---|---:|---:|---:|
| current-main baseline | 9.32 s | 2.64 s | 3.53x |
| PR run 1 | 7.40 s | 2.76 s | 2.68x |
| PR final run | 8.57 s | 3.05 s | 2.81x |

The control-normalized remaining overhead improves about 20-24% across
the two fresh PR runs. Lowering the existing 1 GiB promotion threshold
was separately rejected: forcing that path at 10M was about 40% slower.

## Correctness and unhappy paths

The 30M black-box run verified:

- base and control counts, sums, and payload min/max match
- base, hidden secondary, and hidden UNIQUE tables each contain 30M rows
- forced UNIQUE point lookups at first/middle/last keys return exact
rows
- forced secondary range lookup returns the expected 60,030 rows

Focused coverage includes:

- reversed disjoint ranges and exact BlockMaxRows boundary splitting
- overlapping ranges
- equal boundaries
- mixed/all-NULL boundaries
- varlena keys
- sink failure propagation and buffer ownership
- all-empty floating-point/general path
- putBack ordering and payload association

## Validation

- .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=120s
./pkg/objectio/mergeutil
- .agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 -timeout=240s
./pkg/objectio/mergeutil
- .agents/skills/mo-dev/scripts/mo-cgo-test -run
'^TestMergeSortBatches$' -count=1 -timeout=120s ./pkg/sql/colexec
- .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=180s
./pkg/objectio/ioutil
- go vet ./pkg/objectio/mergeutil ./pkg/objectio/ioutil with repository
CGo flags
- make build
- fresh-process 30M indexed/control SQL validation described above

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

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