Skip to content

Avoid double compression in adaptive codec selection - #113511

Merged
rienath merged 9 commits into
masterfrom
double-compression-fix
Aug 18, 2026
Merged

rienath merged 9 commits into
masterfrom
double-compression-fix

Conversation

@rienath

@rienath rienath commented Aug 5, 2026 •

Copy link
Copy Markdown
Member

Adaptive compression used to have two phases: selection and compression. During selection, we compressed every candidate that cannot report its compressed size cheaply into a temporary buffer to measure it. If such a candidate won, the compression phase then compressed it again.

This fuses selection into compression. We do a single pass in which candidates with cheap sizing are only measured, and the rest compress into dest, or into a scratch buffer when dest holds the best result so far. After the pass the winner reaches dest one of three ways: it is already there, it is copied from scratch, or, for a measured-only winner, it is compressed now, for the first and only time. With today's pool this halves the compression work whenever the default codec wins a block.

Not for changelog because adaptive compression is released in 26.8 (#111834).

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Adaptive codec selection now compresses with each candidate codec at most once per block instead of measuring all candidates and then recompressing the winner.


Workflow [PR]
Sync PR [sync-upstream/pr/113511]

Version info

  • Merged into: 26.8.1.1671 (included in 26.8 and later)

rienath added 3 commits August 5, 2026 14:04
Selection used to compress every compress-priced candidate into a temp buffer to measure it, then compress the winner again. Not anymore
@clickhouse-gh

clickhouse-gh Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [200615c]

Summary: ✅


AI Review

Summary

This PR rewrites CompressionCodecAdaptive::compress so codec selection and compression happen in one pass, which should avoid recompressing the winning candidate, and it adds a focused merge benchmark for the common "default codec wins" case. The core compression rewrite looks reasonable, but I do not think the PR is ready to merge yet because the new benchmark is configuration-sensitive and the claimed performance win still has no actual before/after numbers attached.

Findings

⚠️ Majors

  • [tests/performance/adaptive_codec_merge.xml:5] The benchmark contract is "the deployment default codec wins every block", but this table leaves default_compression_codec to the server default. CI randomizes that setting across LZ4, ZSTD(1), and ZSTD(3), and the performance harness executes the XML verbatim, so this test can end up measuring a different path from the one the PR is optimizing. Pin the intended default codec explicitly in the table settings.
Tests
  • ⚠️ The PR body makes a concrete performance claim, but there is still no measured before/after evidence attached to validate it. All six Performance Comparison (amd_release, master_head, N/6) shards were skipped because the PR is not labeled with pr-performance. Please run those jobs or attach equivalent benchmark numbers from this new test.
Final Verdict

⚠️ Changes needed before approval: make the new benchmark deterministic, then attach actual performance numbers for the optimized path.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.80% 86.80% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.20% 79.20% +0.00%

Changed lines: Changed C/C++ lines covered: 81/86 (94.19%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Aug 5, 2026
@rienath rienath changed the title Compress each adaptive codec candidate at most once Avoid double compression in adaptive codec selection Aug 5, 2026
Comment on lines +35 to +37
/// Compressed size of a single block as `compress` would produce it (block header + payload).
static UInt32 getCompressedBlockSize(const ICompressionCodec & codec, const char * src, UInt32 src_size, PODArray<char> & scratch);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No longer needs to be public after selection + compression fuse

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.

Better to move to the free function now

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, moved

return bytes;
}

/// Compress `bytes` with the adaptive codec for `type_name` and return the winner's on-disk method byte.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Small refactor, partly because we no longer can access getCompressedBlockSize

<fill_query>OPTIMIZE TABLE adaptive_codec_merge FINAL</fill_query>

<!-- 1 -> 1 merge -->
<query>OPTIMIZE TABLE adaptive_codec_merge FINAL</query>

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.

This adds the benchmark harness, but the PR still lands without any measured before/after result for the optimization it is claiming. The performance-comparison shards for this PR were skipped, so reviewers cannot verify that the new selection path actually improves the common "default codec wins" merge case in practice. Please run the performance job (or attach an equivalent benchmark result) and point to the numbers here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The performance-comparison shards for this PR were skipped, so reviewers cannot verify that the new selection path actually improves the common "default codec wins" merge case in practice

The CI is still running...

@Michicosun Michicosun self-assigned this Aug 6, 2026

<!-- The default codec wins every block -->
<fill_query>SYSTEM STOP MERGES adaptive_codec_merge</fill_query>
<fill_query>INSERT INTO adaptive_codec_merge SELECT cityHash64(number % 1024) FROM numbers_mt(400000000)</fill_query>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

2.6s in CI – not perfect, but ok. I'll halve it (perhaps in follow-up)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed

@Michicosun Michicosun left a comment

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.

Comment on lines +35 to +37
/// Compressed size of a single block as `compress` would produce it (block header + payload).
static UInt32 getCompressedBlockSize(const ICompressionCodec & codec, const char * src, UInt32 src_size, PODArray<char> & scratch);

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.

Better to move to the free function now

Comment on lines +139 to +140
if (scratch.empty())
scratch.resize_exact(getMaxCompressedDataSize(source_size));

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.

not needed, resize_exact will check the same

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done

@rienath rienath left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thank you. I addressed all the comments and switched to multiplexer approach you proposed. I slightly simplified it

Comment on lines +35 to +37
/// Compressed size of a single block as `compress` would produce it (block header + payload).
static UInt32 getCompressedBlockSize(const ICompressionCodec & codec, const char * src, UInt32 src_size, PODArray<char> & scratch);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, moved


<!-- The default codec wins every block -->
<fill_query>SYSTEM STOP MERGES adaptive_codec_merge</fill_query>
<fill_query>INSERT INTO adaptive_codec_merge SELECT cityHash64(number % 1024) FROM numbers_mt(400000000)</fill_query>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed

Comment on lines +139 to +140
if (scratch.empty())
scratch.resize_exact(getMaxCompressedDataSize(source_size));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done

<create_query>
CREATE TABLE adaptive_codec_merge (x UInt64)
ENGINE = MergeTree ORDER BY tuple()
SETTINGS allow_experimental_adaptive_codec_selection = 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.

This benchmark is supposed to measure the "deployment default codec wins" case, but the table leaves default_compression_codec to the server default. In CI that setting is randomized across LZ4, ZSTD(1), and ZSTD(3) by tests/config/install.sh, and tests/performance/scripts/perf.py just executes the <create_query> verbatim, so performance runs do not get the tests/clickhouse-test LZ4 pin that .sql tests with no-random-merge-tree-settings rely on.

On runs where the default is ZSTD, this stops exercising the path this PR is optimizing and makes the before/after delta configuration-dependent. Please pin default_compression_codec = 'LZ4' (or whichever default you want to study) in the table settings.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We don't randomise .xml tests

@clickhouse-gh

clickhouse-gh Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 200615cac with master 48e35ab80 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 702.05 MiB 699.03 MiB -3.02 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

10 translation units recompiled, 18 s compile time in total, 10 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×1.05 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +1.3 s (+8%) in total before that adjustment.

Job report

@rienath
rienath added this pull request to the merge queue Aug 18, 2026
Merged via the queue into master with commit 9316212 Aug 18, 2026
181 checks passed
@rienath
rienath deleted the double-compression-fix branch August 18, 2026 22:59
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 19, 2026
pull Bot pushed a commit to admariner/ClickHouse that referenced this pull request Aug 30, 2026
Since ClickHouse#113511 selection compresses each candidate at most once, so the isCandidateType saves nothing. Types with no specialized candidate now get NONE / default codec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants