Skip to content

Cache resolved streams in wide part writer - #130784

Merged
CurtizJ merged 7 commits into
masterfrom
kshokhin/wide-writer-stream-names
Nov 16, 2026
Merged

CurtizJ merged 7 commits into
masterfrom
kshokhin/wide-writer-stream-names

Conversation

@kirillshokhin

@kirillshokhin kirillshokhin commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

MergeTreeDataPartWriterWide used to resolve stream names from the substream path per block (e.g. in writeColumn) and once per granule (e.g. in getCurrentMarksForColumn and writeSingleGranule), which is unnecessary and produces measurable overhead for writes with small granules and many substreams. This is the case for the proposed native histograms implementation (#122051) for the TimeSeries engine.

This PR resolves every stream once when it is added in MergeTreeDataPartWriterWide::addStreams, thus avoiding stream name building and stream enumeration in the per-granule and per-block passes.
The stream getter and the mark getter look up the stream by its full name in the resolved collection, which removes the short name building and a std::map lookup per call.
min_compress_block_size is resolved once in MergeTreeDataPartWriterOnDisk to avoid per-granule copying of the column description.

The remaining piece of duplicated work is the conversion of the substream path to the full stream name, which can be eliminated as a follow-up.

On a c8g.8xlarge machine the added perf test shows 1.44x speedup with index_granularity = 128 and 1.05x speedup with index_granularity = 8192.

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):

...


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

Version info

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

1. Calculate `min_compress_block_size` once in the
   `MergeTreeDataPartWriterOnDisk` constructor. This avoids per-granule
   copy of the columns description.
2. Cache resolved streams to avoid streams enumeration and streams name
   rebuilding in the per-granule and per-block passes of the writer.
3. Add a perf test that writes many streams in small granules to a Wide part.
Refactor the stream getter and the mark getter to use the resolved stream
collections built in `addStreams`.

This saves building the short stream name and one `std::map` lookup
on the hot path.
@kirillshokhin kirillshokhin added the pr-performance Pull request with some performance improvements label Nov 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7be6d79]

Summary: ✅


AI Review

Summary

The PR caches resolved streams for Wide parts and per-column min_compress_block_size, adds a write benchmark, and rejects colliding stream filenames before sharing a stream. Reviewed all seven changed files at 7be6d79d0ce3b62ef0f8442efbf4006a16ed9eff and the prior discussion. No actionable findings.

Missing context / blind spots
  • ⚠️ Full CI for the latest head is still pending. The focused filename-collision regression passed 30 runs under ASan/UBSan at 78fb9bbfe35e68e054ed7bef7cf7c87992e81b3a; completion of the current-head jobs will confirm the merged version.
  • ⚠️ No local build or tests were run because this checkout has no build directory or executable. Performance evidence consists of the added benchmark and the author's reported 1.44×/1.05× speedups; independent extraction of the CI performance metrics requires a clickhouse executable.
Final Verdict

✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 7be6d79d0 with master 68d5b2e0a (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

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.

Compile time of recompiled translation units

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

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

Job report

kirillshokhin and others added 2 commits November 13, 2026 12:25
# Conflicts:
#	src/Storages/MergeTree/MergeTreeDataPartWriterOnDisk.h
#	src/Storages/MergeTree/MergeTreeDataPartWriterWide.h

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

LGTM

@CurtizJ

CurtizJ commented Nov 16, 2026

Copy link
Copy Markdown
Member

Perf tests:
Screenshot 2026-11-16 at 17 12 38

@CurtizJ
CurtizJ added this pull request to the merge queue Nov 16, 2026
Merged via the queue into master with commit d45f558 Nov 16, 2026
193 checks passed
@CurtizJ
CurtizJ deleted the kshokhin/wide-writer-stream-names branch November 16, 2026 21:02
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 16, 2026
shankar-iyer added a commit to shankar-iyer/ClickHouse that referenced this pull request Nov 19, 2026
Resolves a conflict with ClickHouse#131027 writer code after
ClickHouse#130784, which changed how `MergeTreeDataPartWriterWide` finds the streams of
a column. `addStreams` now registers the streams of every column once, in `resolved_streams_by_column`, and the functions
called for every granule, `getCurrentMarksForColumn` and `writeColumn`, iterate that list instead of calling
`enumerateStreams` and building the stream names again.

- `writeColumn`: both sides added a line at the same place, the count of the rows of a column with the setting
  `uncompressed_fixed_array_size` and the lookup of the resolved streams. Both are kept.
- `getCurrentMarksForColumn`: the check that flushes the buffer of the elements of such a column after every row used the
  `substream_path` of the callback of `enumerateStreams`, which the loop over the resolved streams does not have. The
  merge had no textual conflict there, but did not compile. The stream of the elements is now recognized by its name,
  which `addStreams` records in `FixedArrayColumn::stream_name` and is the `escaped_column_name` of the stream.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-part-formats Formats of MergeTree data parts in the public repository, that is how a part is laid out on disk ... and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-part-formats Formats of MergeTree data parts in the public repository, that is how a part is laid out on disk ... pr-not-for-changelog This PR should not be mentioned in the changelog 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