Skip to content

Adaptive codec selection: sampling - #124882

Merged
rienath merged 37 commits into
masterfrom
adaptive-codecs-sampling
Nov 9, 2026
Merged

rienath merged 37 commits into
masterfrom
adaptive-codecs-sampling

Conversation

@rienath

@rienath rienath commented Oct 9, 2026 •

Copy link
Copy Markdown
Member
Screenshot 2026-10-09 at 22 47 22

Adaptive codec selection compressed every block with every candidate codec, keeping the smallest output.

Now one block per window of adaptive_codec_selection_period blocks (a new MT setting, default 8) is priced and the blocks in between reuse its winner. A codec may keep per-stream memory in a CompressionState owned by its CompressedWriteBuffer, and CompressionCodecAdaptive delegates which candidates run on a block to an AdaptiveCodec::ICodecSelector (currently only CodecSelectorSampled). A few rules:

  • The sample block (the ones every candidate compresses) sits at an offset hashed from the window number, so replicas write identical bytes.
  • A block reusing the winner is re-priced when the winner writes more per source byte than the runner-up would.
  • A priced block nothing shrank leaves no winner, so the next block is priced too.
  • Compact parts always price every block.

Five new profile events describe an adaptive merge:

  • AdaptiveCodecBlocksPriced, AdaptiveCodecBlocksRepriced and AdaptiveCodecBlocksReused split its blocks by treatment.
  • AdaptiveCodecBytesDefault and AdaptiveCodecBytesWritten sum the default codec's size and the written size on the sample blocks so their ratio is the saving against the default codec.

Judging by hits, TPC-H SF10 lineitem table, NYC taxi and cell towers datasets, merges on integer data now have the same speed as without adaptive codec selection. Merges on floats are up to 35% slower, but wins are large (about 20% smaller files).

Changelog category (leave one):

  • Performance Improvement

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

Adaptive codec selection tries every codec on one block in adaptive_codec_selection_period (default 8) and reuses the winner on the rest until it stops winning. Merges with enable_adaptive_codec_selection now cost about the same as without it.


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

Version info

  • Merged into: 26.11.1.1716-master (included in 26.11 and later)

Make it a baseline codec selection starts from s.t. we don't have to keep track of it in many places
A candidate without a chain that could report its size through tryGetCompressedSize was measured rather than compressed, and compressed at the end only if it won. Since chains were merged, the path was never taken there. It stayed live for a default that is not LZ4/ZSTD. This is a very rare scenario and significantly complicates API for sampling. Thus, removing. If it happens we need it later, we can reintroduce taking current infra into account.
CodecSelectorSampled prices one block per selection_period, reuses the winner in between, re-prices when it loses ground. Period 1 for now (so sampling all)
A chain whose first codec is the best in previous plan runs off the previously saved best buffer instead of compressing the base again
@clickhouse-gh

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9f4ce4c]

Summary: ✅


AI Review

Summary

The PR samples adaptive codec selection once per configurable window, preserves compression state per stream, and reprices when the remembered winner degrades. Reviewed head 9f4ce4cfc9edbcf7c223bf7cd5c35a92a37dabab, the full diff, current callers, and all prior discussion. No actionable findings remain. The focused sampling and repricing regressions pass in the head's Fast test report, and the supplied benchmark records faster adaptive merges on both architectures.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements comp-codecs Compression/encoding codecs (LZ4/ZSTD/etc.), archive formats (tar/zip/7z), codec selection and IO... labels Oct 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9f4ce4cfc with master aa2da7eb6 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of d7640f32b; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

82 object files changed (+139.29 KiB total), 2 added.

Object file Master PR Δ
src/CMakeFiles/clickhouse_compression.dir/Compression/Adaptive/CompressionCodecAdaptive.cpp.o new 79.62 KiB +79.62 KiB
src/CMakeFiles/clickhouse_compression.dir/Compression/Adaptive/CodecSelectorSampled.cpp.o new 32.50 KiB +32.50 KiB

720 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

782 translation units recompiled, 6667 s compile time in total, 779 of them have a recent master baseline.

Translation units without a recent master baseline:

  • programs/keeper/Keeper.cpp (clickhouse-keeper): 8.3 s
Symbol sizes
Binary Symbol Master PR Δ
programs/clickhouse-keeper ProfileEvents::global_counters_storage 14.25 MiB 14.31 MiB +64.00 KiB (+0.44%)
programs/clickhouse ProfileEvents::global_counters_storage 14.25 MiB 14.31 MiB +64.00 KiB (+0.44%)

Job report

@clickhouse-gh

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 31 queries analysed

This PR adds sampled per-block codec selection behind the experimental enable_adaptive_codec_selection MergeTree setting, which is off by default. Outside that path, the only change is passing a per-stream compression state into compress() on the write side, and regular codecs leave that state empty. One query was flagged: clickbench Q30 at -12.2% (222 ms to 195 ms). I downgraded it to not_sure because the query only reads data and never runs the changed code, its CPU time didn't move (2350 vs 2372 ms), and 195 ms sits just under master's recent 201-287 ms range, which looks like run-to-run variance rather than an effect of this PR.

clickbench

⚠️ 1 inconclusive

Flagged queries (1 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 30 not_sure 222 195 -12.2% <0.0001 The PR only changes the write path for adaptive codec selection, which is experimental and off by default, so this read-only SELECT does not run any changed code. CPU time is flat (2350 vs 2372 ms).

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: f7142aea-3247-49e0-aaf5-4e0acafd84fa
  • MIRAI run: cb98a2a3-dde2-49ed-8884-59d6beb23436
  • PR check IDs:
    • clickbench_875370_1791725269
    • clickbench_875376_1791725269
    • clickbench_875382_1791725269
    • tpch_adapted_1_official_875388_1791725269
    • tpch_adapted_1_official_875404_1791725269
    • tpch_adapted_1_official_875440_1791725269

@Michicosun Michicosun self-assigned this Oct 12, 2026
The trigger compared the best size so far, which is capped at the raw block. When the raw block was the runner-up of the priced block and the winner then grew past it, the two sides were equal and the stream kept writing raw blocks

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

Thanks @Michicosun, I addressed your feedback

@rienath
rienath added this pull request to the merge queue Nov 6, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 6, 2026
@rienath
rienath enabled auto-merge November 8, 2026 22:24
@rienath
rienath added this pull request to the merge queue Nov 8, 2026
Merged via the queue into master with commit 07d75dc Nov 9, 2026
369 of 370 checks passed
@rienath
rienath deleted the adaptive-codecs-sampling branch November 9, 2026 00:51
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 9, 2026
qoega added a commit to qoega/ClickHouse that referenced this pull request Nov 10, 2026
Retain the per-stream state introduced for adaptive compression in
`CompressedWriteBuffer`. Pass that state to the configured codec and
`nullptr` to bypassed `NONE` frames, preserving state between larger
frames. Keep the 128-byte network default and protocol revision 54497.

Related: ClickHouse#120191
Related: ClickHouse#124882
zoomxi pushed a commit to zoomxi/ClickHouse that referenced this pull request Nov 19, 2026
…a home of their own

Since ClickHouse#116042, clickhouse-local without --tmp or --path works in a
designated directory in the home ($XDG_DATA_HOME/clickhouse-local or
~/.local/share/clickhouse-local; ~/Library/Application Support on macOS)
and holds a lock on its `status` file for the whole run. That change gave
$CLICKHOUSE_LOCAL a --tmp, but these three tests start clickhouse-local
through the bare ${CLICKHOUSE_BINARY}, so all their local-mode runs in a
CI job share the job's one default directory. When two of them overlap
(two of these tests at the same time in a parallel job, or two copies of
one test in the flaky check), the second fails with
"Code: 76 ... Cannot lock file .../clickhouse-local/status".
Seen on PR ClickHouse#124882 (03267, Stateless tests (amd_debug, parallel)) and
PR ClickHouse#127774 (03536, Fast test).

Prefix each local-mode line with a per-test HOME under CLICKHOUSE_TMP and
an empty XDG_DATA_HOME (an absolute one wins over HOME on Linux), the same
form ClickHouse#116042 uses in its own default-directory tests. The argv of every
invocation is unchanged, so the dispatch rules of programs/main.cpp that
these tests pin stay covered, including the no-argument rule that only
03267 exercises (`clickhouse < file`); --tmp would have turned that line
into the dash-option rule. Client-mode lines and $CLICKHOUSE_LOCAL lines
are untouched.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-codecs Compression/encoding codecs (LZ4/ZSTD/etc.), archive formats (tar/zip/7z), codec selection and IO... pr-performance Pull request with some performance improvements 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