Skip to content

Restore pooling of non-poolable serializations in DataTypesCache - #117655

Merged
fm4v merged 3 commits into
masterfrom
fix-dynamic-json-text-serialization-perf
Sep 2, 2026
Merged

fm4v merged 3 commits into
masterfrom
fix-dynamic-json-text-serialization-perf

Conversation

@fm4v

@fm4v fm4v commented Sep 2, 2026 •

Copy link
Copy Markdown
Member

Related: #115852

Follow-up to #115852, which merged before a last review comment was addressed: #115852 (comment)

That PR's DataTypesCache fix made getCacheElement skip caching the serialization for types with supportsPooling() == false (e.g. SerializationJSON), to avoid a correctness bug where a query-context-dependent serialization (like DateTime's baked-in session_timezone) could leak across queries on the same long-lived server thread. That was more conservative than necessary, and it reintroduced the exact performance regression the original PR fixed, but narrowed to Dynamic(JSON): SerializationDynamic's text fast path was rebuilding a fresh SerializationJSON on every row again.

supportsPooling()'s contract, and SerializationJSON's own documented failure mode, are specifically about reuse across queries (its documented example is exactly the timezone case). DataTypesCache already prevents that: it's thread-local, and is cleared on every query-context change and on session_timezone mutation (added by the same earlier PR). Reuse within one query is an already-established, trusted pattern for the very same objects, via ColumnDynamic's own per-column serialization_cache.

I considered instead routing the text-serialization path through that per-column cache directly (as one reviewer suggested), but a ColumnDynamic instance is not always exclusive to one query: StorageMemory and the query result cache can hand the very same column instance to concurrently-running queries, and that cache has no invalidation hook at all, so pooling from the read side there would both race and reintroduce cross-query staleness. The thread-local cache with query/timezone-scoped invalidation is the safe place for this.

Also extends the perf test added by the original PR with a Dynamic column holding JSON values (the previously-unproven path), and replaces the gtest asserting non-pooling with one asserting pooling within a query plus invalidation across queries.

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

Internal follow-up correcting an implementation detail from an unreleased change; no user-visible behavior change beyond restoring the intended performance characteristics of #115852 for Dynamic(JSON) text output.


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

Version info

  • Merged into: 26.9.1.560 (included in 26.9 and later)
  • Backported to: 26.8.3.17, 26.7.7.13, 26.6.5.94

Follow-up to #115852, which merged
before a last review comment was addressed:
#115852 (comment)

Making DataTypesCache::getCacheElement skip caching the serialization for
types with supportsPooling() == false (e.g. SerializationJSON) reintroduced
the exact performance regression the original PR fixed, but only for
Dynamic(JSON): a fresh SerializationJSON was rebuilt on every row again in
SerializationDynamic's text fast path.

That restriction was more conservative than necessary. supportsPooling()'s
contract, and SerializationJSON's own documented failure mode, are both
specifically about reuse *across queries* (e.g. a cached DateTimeNode
capturing the wrong session_timezone). The cache already prevents exactly
that: it is thread-local and is cleared on every query-context change and
on session_timezone mutation. Reuse *within* one query is an established,
trusted pattern already relied on elsewhere for the same objects, via
ColumnDynamic's own per-column serialization_cache.

I considered instead routing SerializationDynamic's text path through that
per-column cache directly, but a ColumnDynamic instance is not always
exclusive to one query: StorageMemory and the query result cache can hand
the very same column instance to concurrently-running queries, and it has
no invalidation hook at all, so pooling from the read side there would
both race and reintroduce cross-query staleness. The thread-local cache
with query/timezone-scoped invalidation is the safe place for this.

Extend the added perf test with a Dynamic column holding JSON values, and
replace the gtest asserting non-pooling with one asserting pooling within
a query and invalidation across queries.
@clickhouse-gh

clickhouse-gh Bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c4e6648]

Summary: ❌

job_name test_name status info comment
LLVM Coverage FAIL
Generate LLVM Coverage Diff Report FAIL cidb
Print Uncovered Code FAIL cidb Uncovered code analysis did not run: bash ci/jobs/scripts/generate_diff_coverage_report.sh failed (its output is on the Generate LLVM Coverage Diff Report result).
Parser memory check ERROR
Resolve master binary ERROR

AI Review

Summary

This PR restores query-scoped reuse of non-simple serializations in DataTypesCache to recover the Dynamic(JSON) text-output path that regressed in #115852, and adds focused regression/performance coverage for the reuse and invalidation behavior. After reading the full diff, tracing every current getDataTypesCache().getSerialization caller, and checking the prior discussion against the current code, I did not find a remaining correctness, safety, or contract issue.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

⚠️ No coverage measurement for commit c4e6648: bash ci/jobs/scripts/generate_diff_coverage_report.sh failed (its output is on the Generate LLVM Coverage Diff Report result).

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 2, 2026
getSerialization() returns shared_ptr<const ISerialization>; .get() is
const ISerialization*, not ISerialization*.
@zlareb1

zlareb1 commented Sep 2, 2026

Copy link
Copy Markdown
Member

I audited the pooling change against every getDataTypesCache() call site at master, and it looks sound as-is: no cache-served instance ever reaches deserializeText*/deserializeObject (the call sites in SerializationDynamic.cpp and ColumnObject.cpp are types-only, binary serde, or text-output uses), so the pooled SerializationJSON's parser pool and json_extract_tree caches are dormant on every reachable path, and the timezone-derived state in DateTimeNode is covered by the cache's session_timezone tracking. Two notes, both about the argument in the new header comment rather than the code:

  1. The "semantically equivalent" wording for the allow_simdjson gap is stronger than what the parsers guarantee. RapidJSONParser.h defines RAPIDJSON_PARSE_DEFAULT_FLAGS as kParseIterativeFlag without kParseFullPrecisionFlag, so rapidjson's Float64 parsing is not correctly rounded the way simdjson's is (ULP-level divergence is possible), and simdjson additionally enforces a nesting-depth cap that the iterative rapidjson path doesn't. Today this is moot because of point 2, but if it ever stops being moot, the gap is value-level, not just a matter of which parser does the work.

  2. The safety of pooling rests on the currently-true fact that no DataTypesCache::getSerialization consumer invokes text deserialization — nothing enforces that invariant. A future caller (for example, a Dynamic text-input fast path symmetrical to the existing text-output one) would inherit both the stale parser choice and cross-query json_extract_tree accumulation within one clickhouse-client session, since one client_context spans the session and only query-context identity and session_timezone invalidate the cache. It might be worth one sentence in the DataTypesCache.h comment stating that cached serializations must not be used for text deserialization, so the constraint is visible to the next caller.

…ce claim

- gtest_data_types_cache.cpp: InvalidatesNonPoolableSerializationsAcrossQueries
  compared a raw pointer captured after its owning SerializationPtr could
  already have been destroyed (the query scope it was built in had ended),
  so an unrelated later allocation reusing the same address could make the
  test pass even if cache invalidation were broken. Keep the first
  SerializationPtr alive across both scopes and compare through it instead.

- DataTypesCache.h: the comment claimed allow_simdjson only "selects between
  two semantically equivalent JSON parsers." rapidjson's default flags
  (RAPIDJSON_PARSE_DEFAULT_FLAGS) omit kParseFullPrecisionFlag, so its
  Float64 parsing isn't correctly rounded the way simdjson's is, and it
  enforces no nesting-depth cap - a real value-level gap, not just an
  implementation choice. It's moot today because no getDataTypesCache()
  consumer performs text deserialization through a cached instance (only
  type resolution, binary serialization, and text output do), so the
  extraction tree's mutable caches and the parser choice are never
  exercised. Spell out that invariant explicitly so a future caller (e.g.
  a symmetrical Dynamic text-input fast path) doesn't reintroduce the
  cross-query hazard this cache doesn't protect against for deserialization.

Addresses #117655 (comment)
and #117655 (comment)
@clickhouse-gh

clickhouse-gh Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing c4e6648b2 with master 552cef42a (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

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

Job report

@fm4v
fm4v enabled auto-merge September 2, 2026 15:27
@fm4v
fm4v added this pull request to the merge queue Sep 2, 2026
Merged via the queue into master with commit e912265 Sep 2, 2026
171 of 174 checks passed
@fm4v
fm4v deleted the fix-dynamic-json-text-serialization-perf branch September 2, 2026 19:40
pull Bot pushed a commit to peijunlin2008/ClickHouse that referenced this pull request Sep 2, 2026
Follow-up for ClickHouse#110081, which moved build artifacts under the normalized workflow name. The baseline `llvm_coverage.info` produced by master now lives at `REFs/master/<sha>/masterci/llvm_coverage/llvm_coverage.info`, but `generate_diff_coverage_report.sh` still probed the old path without the `masterci` segment, so `Generate LLVM Coverage Diff Report` exhausted all 30 candidate commits and failed the `LLVM Coverage` job on every pull request, for example https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117655&sha=c4e6648b2ccbbedf97a6d5bab32c8a8c9cf01c9d&name_0=PR&name_1=LLVM%20Coverage (ClickHouse#117655).
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 2, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 2, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 2, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 2, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 2, 2026
Backport #117655 to 26.8: Restore pooling of non-poolable serializations in DataTypesCache
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 2, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 2, 2026
Backport #117655 to 26.7: Restore pooling of non-poolable serializations in DataTypesCache
PedroTadim added a commit that referenced this pull request Sep 10, 2026
Backport #117655 to 26.6: Restore pooling of non-poolable serializations in DataTypesCache
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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 v26.4-must-backport v26.5-must-backport v26.6-must-backport v26.7-must-backport v26.8-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants