Repository navigation
Remove the per-call metadata copy in debug and sanitizer builds - #125697
Algunenano merged 3 commits into
Conversation
Copying a `ColumnDefault` deep-clones its expression AST, and copying a `ColumnDescription` also clones its codec and TTL ASTs. With the bucketed `system.metric_log` schema (the default since ClickHouse#124016) the table has ~2200 `ALIAS` columns, and every metadata copy clones and later destroys all of their ASTs. A `system.dashboards` query copies that metadata many times: in debug and sanitizer builds `MergeTreeData::getInMemoryMetadataPtr` returns a fresh copy on every call (to catch lifetime bugs), and `StorageMerge::getInMemoryMetadataPtr` / `StorageMaterializedView::getInMemoryMetadataPtr` copy it on every call through `withVirtuals`. Under ASan the clones were 39% of the CPU of the test's queries, and `03540_system_dashboards` doubled its runtime on every debug and sanitizer lane and hit the 600 s timeout on amd_tsan with s3 storage. Add `StorageInMemoryMetadata::copySharingColumnASTs` (backed by `ColumnsDescription::copySharingASTs`, a tagged `ColumnDescription` constructor and `ColumnDefault::copySharingExpression`): the copy has its own metadata, columns description and column descriptions, and shares only the immutable ASTs. Use it at the three read-only per-call sites. It never goes through `ColumnsDescription::add`, whose function name normalizer modifies the AST in place. The copy constructor and assignment still deep-copy, so every mutable copy (ALTER, CREATE AS, ...) keeps owning its ASTs. In debug builds the lifetime check still returns a distinct metadata object per call; only a dangling reference into a column AST is no longer caught by it. Also add an rvalue `IStorage::setInMemoryMetadata` overload and move the local metadata in the `Merge`, `Distributed` and `Dummy` constructors. The `Distributed` constructor reads the columns for the sharding key after setting the metadata, so it now reads them from its `columns_` argument, which is what the metadata was built from. Measured on the test's 498 dashboard queries in `clickhouse local` (debug build): bucketed schema 25.2 s -> 20.6 s, wide schema unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
read-only per-call copy, and extend the unit test to the codec, TTL and metadata-level copies. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; round 1: 3 findings, round 2 re-review of
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261013-214400 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261013-210200 |
|
Workflow [PR], commit [2ce3b29] Summary: ✅
AI ReviewSummaryPR #125697, reviewed at Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested. |
CI finish ledger - ccd019bEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Three Session id: cron:our-pr-ci-monitor:20261014-030009 |
| /// The column ASTs are shared, the metadata and column descriptions are still distinct objects. | ||
| #if defined(DEBUG_OR_SANITIZER_BUILD) | ||
| return std::make_shared<StorageInMemoryMetadata>(*base); | ||
| return std::make_shared<StorageInMemoryMetadata>(base->copySharingColumnASTs()); |
There was a problem hiding this comment.
Isn't it simpler to remove this instead if it's no longer useful? Adding 150 lines of code to avoid a perf issue in sanitizers isn't really something I feel inclined to do
There was a problem hiding this comment.
Agreed, and removing it is also faster. Same base, debug clickhouse local, the test's 498 dashboard queries (sum of durations, min of 2 runs):
- bucketed schema: 24.6 s on master, 21.5 s with the AST sharing, 20.1 s with the copy removed
- wide schema: 13.4 s, 13.2 s, 12.0 s
I reduced the PR to removing the copy in 2ce3b29, which reverts the AST sharing; the diff is now MergeTreeData.cpp only.
On whether it is still useful: it was added in #106200 to find metadata lifetime misuse, together with StorageMetadataHandle, which already rejects the common case (dereferencing a temporary handle) at compile time. I did not find a later fix that came from the copy. @Michicosun, are you OK with dropping it?
`MergeTreeData::getInMemoryMetadataPtr` deep-copied the whole `StorageInMemoryMetadata` on every call in debug and sanitizer builds, so that a reference kept after its metadata pointer was dropped became a use-after-free under ASan. With the bucketed `system.metric_log` schema every copy clones thousands of `ALIAS` default ASTs, a large part of why `03540_system_dashboards` is slow on the sanitizer lanes. The copy was added together with `StorageMetadataHandle` in ClickHouse#106200, and the handle already rejects the common misuse, a reference taken through a temporary handle, at compile time. Removing the copy is simpler than making it cheaper, so this drops it and reverts the AST-sharing copies of the previous two commits, as asked in review. Dashboards probe (debug, 166 dashboards x 3 variants, sum of query durations, min of 2, same base): - bucketed: base 24.57 s, AST sharing 21.50 s, copy removed 20.06 s - wide: base 13.36 s, AST sharing 13.23 s, copy removed 11.99 s Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 44 s compile time in total, 7 of them have a recent master baseline. |
Related: #124016
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Debug and sanitizer builds no longer copy the table metadata on every
MergeTreeData::getInMemoryMetadataPtrcall, which makes thesystem.dashboardsqueries over the bucketedsystem.metric_logabout 18% faster in a debug build.Description
03540_system_dashboardshit the 600 s per-test timeout on master inStateless tests (amd_tsan, s3 storage, sequential, 1/2)(report). Since the bucketedsystem.metric_logschema became the default (#124016), the test is about 2x slower on every debug and sanitizer lane.In debug and sanitizer builds
MergeTreeData::getInMemoryMetadataPtrreturned a deep copy of the metadata on every call, so that a reference kept after its metadata pointer was dropped became a use-after-free under ASan. The bucketed schema has ~2200ALIAScolumns, so every copy cloned and destroyed thousands of default expression ASTs. This removes the copy, so debug and sanitizer builds behave like release here. The copy was added together withStorageMetadataHandlein #106200; the handle still rejects the common misuse, a reference taken through a temporary handle, at compile time.Validation: the test's 498 dashboard queries in
clickhouse local(debug, min of 2 runs, same base): bucketed schema 24.6 s -> 20.1 s, wide schema 13.4 s -> 12.0 s, no errors. The separate cost ofclusterAllReplicasdashboards reading the whole metrics map on S3 lanes (#114174) is not addressed here.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/125697]
Version info
26.10.1.2458-master(included in26.10and later)