Skip to content

[core] Slice lookup changelog rows instead of copying them - #1480

Open
JeonDaehong wants to merge 2 commits into
apache:mainfrom
JeonDaehong:perf-slice-lookup-changelog
Open

JeonDaehong wants to merge 2 commits into
apache:mainfrom
JeonDaehong:perf-slice-lookup-changelog

Conversation

@JeonDaehong

@JeonDaehong JeonDaehong commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Linked issue: none, follows #1474

With changelog-producer = lookup, every changelog row is built with take, which copies all of its columns into a new one-row batch, and a key with several changes is concatenated once more. Since #1474 the reader concatenates changelog rows into output batches anyway, so these copies are not needed.

Brief change log

  • source_row returns a one-row slice of the source batch instead of a take copy.
  • A key with a single change returns it as is, without concat_batches.
  • The reader copies its pending changelog rows into one batch whenever it releases source batches, and before handing them on, so a slice never keeps a released batch alive or reaches the writer.

PK table, changelog-producer = lookup, compaction time, in-memory FileIO, release build. Measured in one binary with the old path switched back on by env vars, over 3 alternating rounds (parent → this PR):

  • 2M keys, first compaction (every key emits a changelog row): 4028–4051 → 2745–2752 ms
  • 2M keys, a third of them updated, second compaction: 8787–8861 → 7997–8011 ms
  • 64 more BIGINT columns, 500k keys: 12197–12296 → 8059–8119 ms (first), 17211–17442 → 15200–15356 ms (second)
  • 4 KiB values, one changed key per 1,024-row input batch (row-deduplicate, ignore-update-before): 846–851 → 741–764 ms, peak RSS 83–86 → 78–85 MB
  • deletion vectors without a changelog producer, which does not use this path: 5103–5118 vs 5091–5128 ms

Tests

  • test_changelog_rows_do_not_keep_released_source_batches: merged source batches are released while changelog rows are pending, and a lone changelog row does not share its source buffer.
  • lookup_changelog_charges_a_lone_row_for_its_own_value: lookup compaction under a 64 KiB memory limit, Parquet and ORC.

Both fail without the copy. Ran the ci.yml steps (fmt, clippy, msrv, unit, rest-server, integration, datafusion, python, go).

API and Format

None.

Documentation

None.

@JingsongLi JingsongLi 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 reported throughput improvement has end-to-end value. All 108 primary-key compaction integration tests passed locally. The sparse-update probe below found a memory regression.

.map_err(|error| invalid(format!("Failed to materialize lookup value: {error}")))
batches[batch_idx]
.column_for_output(index, columns)
.slice(row_idx, 1)

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.

[P1] Bound the source buffers held by pending lookup changelog rows

With lookup changelog, row deduplication and ignore-update-before enabled, an update can enqueue one row that pins the entire input batch. The data output releases its references after each flush, but pending_changelog waits for read.batch-size emitted changes. A sorted rewrite with one changed key per input batch can therefore retain nearly read.batch-size full input batches.

I reproduced this in the compaction merge stream with 128-row batches and 4 KiB binary values. After 40 data outputs, the PR still held 41 L0 payload buffers (20.5 MiB). Its parent held one (0.5 MiB). At the default 1,024-row batch size the same pattern can retain about 4 GiB in payloads alone. Please bound this queue by retained bytes or materialize its rows before releasing a source batch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks @JingsongLi, you're right. The slices kept every input batch with a pending change alive until the changelog reached read.batch-size. In 89ef69f the reader copies its pending changelog rows into one batch whenever it releases source batches, so they never hold more than the reader already does. With 4 KiB values and one change per 1,024-row input batch, peak RSS of that compaction is 83–86 MB on the parent, 325–326 MB on the previous commit and 78–85 MB now. test_changelog_rows_do_not_keep_released_source_batches fails on the previous commit.

Some(
let changelog = match changes.len() {
0 => None,
1 => changes.pop(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Materialize a lone changelog row before handing it to the writer. When the merge emits exactly one change, changelog_batch calls Arrow’s concat_batches with one input. Arrow slices each column again, so ResourceFormatWriter charges the full source allocation through get_array_memory_size().

I reproduced this with read.batch-size=128, 127 superseded 4 KiB strings, and one new key with a one-byte value in the same ORC batch. With a 64 KiB memory limit, the parent completes with a peak reservation of 4,115 bytes. This commit requests 523,262 bytes for that single changelog row and fails with ResourceExhausted. Could we detach the row on a singleton flush and cover this case with a memory-limited compaction test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks @SteNicholas for the repro. A lone row is now copied before it goes to the writer too. lookup_changelog_charges_a_lone_row_for_its_own_value runs a compaction under a 64 KiB limit where the only changelog row comes from a batch of 4 KiB values, for Parquet and ORC. It passes on the parent and now, and on the previous commit it asks for 526,749 bytes.

.flat_map(|(batch, rows)| (0..rows.num_rows()).map(move |row| (batch, row)))
.collect::<Vec<_>>();
let chunk =
arrow_select::interleave::interleave_record_batch(&batches, &indices).map_err(|error| {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Avoid allocating an index pair for every nested element when detaching changelog rows. Arrow 59.3.2 expands each selected MAP row into a Vec<(usize, usize)> with one entry per map item. This adds 16 bytes per item on 64-bit builds before the child buffers are copied. The same expansion applies to lists with non-primitive children. The scratch allocation happens before the batch reaches the writer’s memory reservation.

In an Arrow-level reproduction of these two materialization paths, a singleton MAP<INT, INT> with 500,000 entries produced the same ~4 MB output, but peak additional allocation grew from 5.0 MB with take + concat to 12.0 MB with interleave. Could we use a range-based copy for nested columns, or retain the previous take path for them, and cover a large map in the regression tests?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants