Skip to content

Enable libc++ hardening in all source files - #133805

Merged
Algunenano merged 14 commits into
ClickHouse:masterfrom
Algunenano:remove-libcxx-hardening-exclusions
Nov 24, 2026
Merged

Algunenano merged 14 commits into
ClickHouse:masterfrom
Algunenano:remove-libcxx-hardening-exclusions

Conversation

@Algunenano

@Algunenano Algunenano commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Related: #94757

Remove all per-TU libc++ hardening exclusions (29 files: window functions, Aggregator.cpp, IColumn.cpp, IntervalTree.cpp, array.cpp, hashed/hashed array/range hashed dictionaries). Hot loops that paid for bounds checks are fixed instead, keeping checks on data-dependent indices:

  • WindowArguments::at: no checks on the argument index in the fast path.
  • gatherColumnFromRows, FunctionArray, HashedDictionary, HashedArrayDictionary: check once outside the row loop. In hasKeys the checks stopped the per-key lambda from being inlined (+22% cycles); now sparse dictHas is 8-16% faster than before.
  • IntervalTree::intervalsToPoints: dictionary load was 10% slower because buildTree stopped inlining it.
  • Partition::advance: std::find over the bool vector, 4-6% fewer instructions for row_number/ntile.

Each file is a separate commit. Also restores -g1 for HashedDictionary.cpp and RangeHashedDictionary.cpp, which was tied to the exclusion list.

Changelog category (leave one):

  • Build/Testing/Packaging Improvement

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

libc++ hardening is now enabled for all ClickHouse source files, including the ones where it was disabled for performance.


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

Version info

  • Merged into: 26.12.1.425-master (included in 26.12 and later)

Algunenano and others added 10 commits November 23, 2026 14:08
The window function TUs have no hardening checks in hot loops after the rewrite, except
`WindowArguments::at`. Keep the check on the data-dependent block lookup there and drop
the ones on the argument index, which is checked by `chassert`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`hashjoin_with_large_output` and the row store join tests show no difference now. Check the
row range in `gatherColumnFromRows` once instead of for every row.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Index the per-column vectors through `data` in the row loops of `FunctionArray`. They
have an element for every column, so the checks there only repeated the loop bound.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
It holds only tree building and iteration; `range_hashed_dictionary` shows no difference.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With libc++ hardening `buildTree` no longer inlined this helper, and the per-element
`emplace_back` made loading a `range_hashed` dictionary 10% slower. Now it is 2%.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`range_hashed_dictionary` shows no difference, and lookups have the same instruction count.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Keep the simple key data pointer in `DictionaryKeysExtractor` and hoist the shard container and
nullable set pointers out of the lookup loops. The checks in the loop body stopped the per-key
lambda of `hasKeys` from being inlined. `dictHas` is now 8-16% faster on sparse dictionaries.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Hoist the shard container pointers out of the key lookup loops. Also restore `-g1` for
`HashedDictionary.cpp` and `RangeHashedDictionary.cpp`, which was lost together with
their hardening exclusion.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`destroy_aggregate_states` and the `GROUP BY` tests show no difference when only this TU
changes. This removes the last per-TU hardening exclusion.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
It scans the vector of bools word by word, without a bounds check for every row.
`row_number` and `ntile` over a single partition run 4-6% fewer instructions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Algunenano Algunenano added the pr-performance Pull request with some performance improvements label Nov 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3902161]

Summary: ❌

job_name test_name status info comment
Performance Comparison (arm_release, master_head, 6/8) FAIL Performance dashboard
group_by_limit_dynamic_filter #2 FAIL dashboard

AI Review

Summary

Enables libc++ hardening across ClickHouse source files and adjusts hot loops to control its cost. No actionable findings remain at head 3902161f66fb0577d9b78ce66cd7796edaaf1ee6. The previous singleton-partition regression is addressed by the Partition::beginAt fast path and the before/after measurements recorded in commit 6cb6142c3; its thread remains resolved.

Missing context / blind spots
  • ⚠️ The full current-head CI rerun, including performance comparisons, is still pending. The latest window and top-K fixes have measurements in their commit messages, but independent current-head CI performance results are not yet available. No local tests were run because this review job has no ClickHouse binary and prohibits nested CI runs.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-build Pull request with build/testing/packaging improvement label Nov 23, 2026
@alexey-milovidov alexey-milovidov self-assigned this Nov 23, 2026
clickhouse-gh[bot]
clickhouse-gh Bot previously approved these changes Nov 23, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-build-system Build system (CMake, compilation, platform builds). label Nov 23, 2026
clickhouse-gh[bot]
clickhouse-gh Bot previously approved these changes Nov 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 3902161f66fb0577d9b78ce66cd7796edaaf1ee6 was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception).

See the job log for details.

With libc++ hardening in `Aggregator.cpp` it was no longer inlined into the aggregation loop,
which made `String` key `GROUP BY ... LIMIT` with the top-K heap 1.2x slower on ARM.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh
clickhouse-gh Bot dismissed stale reviews from themself November 24, 2026 10:08

AI review of a later commit does not approve the PR, see the AI Review comment.

Algunenano and others added 2 commits November 24, 2026 13:59
With libc++ hardening, `Partition::advance` calls `std::find` out of line, so it saves and
restores registers on every call. `beginAt` calls it once per row when every partition has one
row, which made `window_function_with_order` 8-15% slower. `beginAt` now finishes the partition
directly when the next row starts a new one; on ARM these queries are now 2-6% faster than master.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=133805&sha=aaafc36b8dd044fc35fd92cecfc69888103a15c6&name_0=PR&name_1=Performance%20Comparison%20%28arm_release%2C%20master_head%2C%201%2F8%29
PR: ClickHouse#133805

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Force-inlining `TopKAggregationHeapBase::shouldSkip` made `shouldSkipTyped` too big to inline,
so `String` key `GROUP BY ... LIMIT` with the top-K heap still called it per row and stayed 1.2x
slower on ARM. For key methods without typed key data `shouldSkipTyped` always falls through to
`shouldSkip`, so the call sites now use `shouldSkip` directly; `optimize_trivial_group_by_limit`
queries 6 and 7 are back to the master timings.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=133805&sha=aaafc36b8dd044fc35fd92cecfc69888103a15c6&name_0=PR
PR: ClickHouse#133805

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Algunenano

Copy link
Copy Markdown
Member Author

🕵 The remaining regression in group_by_limit_dynamic_filter #2 (multi-key GROUP BY ... LIMIT through TopKAggregationHeapBase::sourceAboveBoundaryComposite) is caused by this PR. It is tracked, with the cause and a possible fix, in #134225.

We are merging this PR now to help advance other PRs, like CFI and other hardening work, that hit similar hotspots.

@Algunenano
Algunenano added this pull request to the merge queue Nov 24, 2026
Merged via the queue into ClickHouse:master with commit 23b95d8 Nov 24, 2026
380 of 383 checks passed
@Algunenano
Algunenano deleted the remove-libcxx-hardening-exclusions branch November 24, 2026 19:05
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 24, 2026
@@ -15,6 +17,16 @@ void Partition::beginAt(const SlidingBlocks & blocks, RowNumber first_row)
partition_bounds.rows_count = 1;
partition_bounds.start_visible = true;
partition_bounds.end_visible = false;

/// Single-row partitions call this once per row. Their end is visible right away, so this check keeps `advance`, which

@Michicosun Michicosun Nov 24, 2026 •

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.

This is a hack. Why do we need to do it this way?

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 first hack was to disable hardening. This enables it back without reverting the PR that introduced this

@Michicosun Michicosun Nov 24, 2026 •

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.

It was introduced starting with the development of window functions, so probably 7 years ago

It is not possible to revert

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 25, 2026
`Performance Comparison (arm_release, master_head, 4/8)` on 095e311 marked
`optimize_trivial_group_by_limit` ClickHouse#6 and ClickHouse#7 (`String` key, no `COLLATE`) as
confirmed regressions: 50 -> 59 ms (+18%). The loops of the methods without a
typed key called `shouldSkipTyped` out of line on every row; master fixed that
the same way in ClickHouse#133805 (`shouldSkip` forced inline and called directly), which
the previous merge brings in.

What remains of this PR in those loops is the `has_collators` branch of the
force-inlined `shouldSkip`. It inlined both collated comparators into every
loop. They now live in an out-of-line `shouldSkipCollated` (`NO_INLINE`, so that
ThinLTO does not import it either), so the loops get a branch and a cold call,
the shape the PR had at 1a5e9ec, where arm measured ClickHouse#6/ClickHouse#7 at 52 -> 51 ms.

Local release builds (no ThinLTO/PGO), 8 threads, medians of 42 runs, ms,
master 290fa0d / merge without this / with this:
- ClickHouse#6: 56.5 / 59 / 59
- ClickHouse#7: 58 / 58.5 / 59
- ClickHouse#7 without `count()`: 57.5 / 56.5 / 56
- numeric key: 39 / 37 / 37
Before the merge, 095e311 measured ClickHouse#7 at 64 ms against master's 61.

The collated queries do not change: 1K keys in cycles at 12 threads 115 -> 114 ms,
20M unique keys 38 -> 38 ms; `hits` `URL ... COLLATE 'en' LIMIT 10` at 12 threads,
measured on 095e311 before the merge: 598-608 -> 579-622 ms, 61 MB both.

Related: ClickHouse#126735

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-build-system Build system (CMake, compilation, platform builds). pr-build Pull request with build/testing/packaging improvement 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.

4 participants