Skip to content

Added a serialization object pool - #96563

Merged
nikitamikhaylov merged 86 commits into
masterfrom
serialization-object-pool
Mar 23, 2026
Merged

nikitamikhaylov merged 86 commits into
masterfrom
serialization-object-pool

Conversation

@nikitamikhaylov

@nikitamikhaylov nikitamikhaylov commented Feb 10, 2026 •

Copy link
Copy Markdown
Member

The serialization is passed by and stored in std::shared_ptr<const ISerialization>, so using the pooled object instead of a newly created one should be safe. The only thing to keep in mind and pay attention to -- the definition of the getName() method. This one should be unique per type and distinguish between different instantiations of this type.

This is a part of #93694 which became too big.

Changelog category (leave one):

  • Improvement

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

Reduced the memory footprint from the internal data structures (ISerialization objects) by introducing an object pool.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Note

Medium Risk
Touches core ISerialization construction paths across many data types and introduces global caching with custom lifetime management, so regressions could affect serialization correctness or memory usage under concurrency. Pooling is explicitly disabled for SerializationJSON due to mutable internal caches, mitigating a known correctness risk.

Overview
Adds a global SerializationObjectPool and extends ISerialization with hash/pooling APIs (getHash(), supportsPooling(), allocatedBytes(), and pooled()), enabling reuse of identical serialization objects across the process.

Refactors a broad set of DataType*::doGetSerialization() and Serialization* implementations to use new ::create() factory methods that compute a stable UInt128 hash (via SipHash) and return pooled instances; wrapper serializations propagate/disable pooling based on nested serializations.

Introduces new CurrentMetrics counters (SerializationCacheBytes, SerializationCacheBytesUncorrected, SerializationCacheCount) and explicitly opts out of pooling for SerializationJSON to avoid sharing mutable per-use caches.

Written by Cursor Bugbot for commit d7d4c03. This will update automatically on new commits. Configure here.

Version info

  • Merged into: 26.4.1.143

@clickhouse-gh

clickhouse-gh Bot commented Feb 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9d11274]

Summary: ❌

job_name test_name status info comment
AST fuzzer (arm_asan_ubsan) failure
AddressSanitizer: stack-use-after-scope (STID: 2136-3203) FAIL cidb, issue ISSUE CREATED

AI Review

Summary

This PR introduces a global pool for ISerialization objects and threads hash-based deduplication through many Serialization*::create paths to reduce memory footprint. After reviewing the current PR head and high-risk pooling/hash paths, I did not find new high-confidence correctness, safety, or performance blockers that are not already covered by existing review discussion.

Missing context

  • ⚠️ No CI run logs / benchmark artifacts were provided in this review request, so runtime/memory impact is assessed from code inspection only.

ClickHouse Rules

Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅

Final Verdict

  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Feb 10, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Introduces a global SerializationObjectPool and updates many ISerialization implementations to use pooled SerializationPtrs, aiming to reduce memory footprint by reusing identical serialization objects.

Changes:

  • Added SerializationObjectPool and switched a large set of serializations to ::create() factory methods that reuse objects by getName() key.
  • Made ISerialization require getName() and adjusted multiple call sites to use pooled serializations (e.g., numbers/strings/variant/object paths).
  • Updated a couple of tests/call sites to obtain default serializations via DataType APIs rather than direct make_shared.

Reviewed changes

Copilot reviewed 121 out of 121 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/Storages/MergeTree/MergeTreeIndexText.cpp Switches local string/number serializations to pooled factory creation.
src/Formats/JSONExtractTree.cpp Uses DataTypeDynamic().getDefaultSerialization() for Dynamic serialization.
src/DataTypes/Serializations/tests/gtest_string_serialization.cpp Adjusts test to obtain String serialization via DataTypeString default serialization.
src/DataTypes/Serializations/SerializationVariantElementNullMap.h Adds pooling factory, destructor remove, and getName() for Variant element null-map serialization.
src/DataTypes/Serializations/SerializationVariantElementNullMap.cpp Implements destructor removal from pool; switches discriminator number serialization to pooled creation.
src/DataTypes/Serializations/SerializationVariantElement.h Adds pooling factory, destructor remove, and getName() for Variant element serialization.
src/DataTypes/Serializations/SerializationVariantElement.cpp Implements destructor removal from pool; switches number serialization calls to pooled creation.
src/DataTypes/Serializations/SerializationVariant.h Adds pooling factory + destructor + getName() declarations for Variant serialization.
src/DataTypes/Serializations/SerializationVariant.cpp Implements factory, destructor removal, getName(), and switches nested serializations to pooled creation.
src/DataTypes/Serializations/SerializationUUID.h Adds pooling factory + destructor + getName() for UUID serialization.
src/DataTypes/Serializations/SerializationUUID.cpp Implements getName() and destructor removal from pool.
src/DataTypes/Serializations/SerializationTuple.h Adds pooling factory + destructor + getName() for Tuple serialization.
src/DataTypes/Serializations/SerializationTuple.cpp Implements Tuple getName() and destructor removal from pool.
src/DataTypes/Serializations/SerializationTime64.h Adds pooling factories + destructor + getName() for Time64 serialization.
src/DataTypes/Serializations/SerializationTime64.cpp Implements Time64 getName() and destructor removal from pool.
src/DataTypes/Serializations/SerializationSubObjectSharedData.h Adds pooling factory + destructor + getName() for sub-object shared data serialization.
src/DataTypes/Serializations/SerializationSubObjectSharedData.cpp Implements factory, getName(), and destructor removal from pool.
src/DataTypes/Serializations/SerializationSubObject.h Adds pooling factory + destructor + getName() for sub-object serialization.
src/DataTypes/Serializations/SerializationSubObject.cpp Implements getName() and destructor removal; switches shared-data serialization creation to pooled factory.
src/DataTypes/Serializations/SerializationStringSize.h Adds pooling factory + destructor + getName() and replaces embedded SerializationString with SerializationPtr.
src/DataTypes/Serializations/SerializationStringSize.cpp Uses pooled SerializationString; implements getName() and destructor removal.
src/DataTypes/Serializations/SerializationString.h Adds pooling factory + destructor + getName() for String serialization.
src/DataTypes/Serializations/SerializationString.cpp Implements getName() and destructor removal; switches related sub-serializations to pooled factories.
src/DataTypes/Serializations/SerializationSparse.h Adds pooling factories + destructor + getName() for Sparse and SparseNullMap.
src/DataTypes/Serializations/SerializationSparse.cpp Implements getName()/destructors and switches nested nullable/nullmap/offset serializations to pooled creation.
src/DataTypes/Serializations/SerializationReplicated.h Adds pooling factory + destructor + getName() for Replicated serialization.
src/DataTypes/Serializations/SerializationReplicated.cpp Implements getName()/destructor and switches index serialization to pooled creation.
src/DataTypes/Serializations/SerializationQBit.h Adds pooling factory + destructor + getName() for QBit serialization.
src/DataTypes/Serializations/SerializationQBit.cpp Implements getName() and destructor removal from pool.
src/DataTypes/Serializations/SerializationObjectTypedPath.h Adds pooling factory + destructor for Object typed-path wrapper serialization.
src/DataTypes/Serializations/SerializationObjectTypedPath.cpp Implements destructor removal from pool.
src/DataTypes/Serializations/SerializationObjectSharedDataPath.h Adds pooling factory, destructor, and getName() for shared-data path wrapper serialization.
src/DataTypes/Serializations/SerializationObjectSharedDataPath.cpp Implements destructor removal from pool.
src/DataTypes/Serializations/SerializationObjectSharedData.h Adds pooling factory + destructor + getName() for Object shared-data serialization.
src/DataTypes/Serializations/SerializationObjectSharedData.cpp Implements factory, getName(), destructor removal; switches string serialization calls to pooled creation.
src/DataTypes/Serializations/SerializationObjectPool.h Adds the new global pool implementation (getOrCreate, remove).
src/DataTypes/Serializations/SerializationObjectDynamicPath.h Adds pooling factory, destructor, and getName() for dynamic-path wrapper serialization.
src/DataTypes/Serializations/SerializationObjectDynamicPath.cpp Switches internal Dynamic serialization to pooled create and adds destructor removal; switches shared-data path serialization to pooled factory.
src/DataTypes/Serializations/SerializationObjectDistinctPaths.h Adds pooling factory + destructor + getName() for distinct-paths serialization.
src/DataTypes/Serializations/SerializationObjectDistinctPaths.cpp Implements getName() and destructor removal from pool.
src/DataTypes/Serializations/SerializationObject.cpp Switches object shared-data + typed-path wrapper creation to pooled factories.
src/DataTypes/Serializations/SerializationNumber.h Adds pooling factory + destructor + getName() for numeric serialization.
src/DataTypes/Serializations/SerializationNumber.cpp Implements destructor removal from pool for numeric serialization.
src/DataTypes/Serializations/SerializationNullable.h Adds pooling factory + destructor for Nullable serialization.
src/DataTypes/Serializations/SerializationNullable.cpp Adds getName()/destructor removal; switches to pooled sub-serializations for null map + number.
src/DataTypes/Serializations/SerializationNothing.h Adds pooling factory + destructor + getName() for Nothing serialization.
src/DataTypes/Serializations/SerializationNothing.cpp Implements getName() and destructor removal from pool.
src/DataTypes/Serializations/SerializationNamed.h Adds pooling factory + destructor + getName() for named wrapper serialization; switches subcolumn creator to factory.
src/DataTypes/Serializations/SerializationNamed.cpp Implements destructor removal from pool.
src/DataTypes/Serializations/SerializationMap.h Adds pooling factory + destructor + getName() for Map serialization.
src/DataTypes/Serializations/SerializationMap.cpp Implements getName() and destructor removal from pool.
src/DataTypes/Serializations/SerializationLowCardinality.h Adds pooling factory + destructor + getName() for LowCardinality serialization.
src/DataTypes/Serializations/SerializationLowCardinality.cpp Implements factory, getName(), and destructor removal from pool.
src/DataTypes/Serializations/SerializationJSON.h Adds pooling factory + destructor for JSON serialization wrapper.
src/DataTypes/Serializations/SerializationJSON.cpp Implements destructor removal from pool (templated).
src/DataTypes/Serializations/SerializationInterval.h Adds pooling factory + destructor + getName() for Interval serialization.
src/DataTypes/Serializations/SerializationInterval.cpp Implements getName() and destructor removal from pool.
src/DataTypes/Serializations/SerializationIPv4andIPv6.h Adds pooling factory + destructor for IP serialization.
src/DataTypes/Serializations/SerializationIPv4andIPv6.cpp Implements destructor removal from pool (templated).
src/DataTypes/Serializations/SerializationFixedString.h Adds pooling factory + destructor for FixedString serialization.
src/DataTypes/Serializations/SerializationFixedString.cpp Adds getName() and destructor removal from pool.
src/DataTypes/Serializations/SerializationEnum.h Adds pooling factories + destructor + getName() for Enum serialization.
src/DataTypes/Serializations/SerializationEnum.cpp Implements destructor removal from pool (templated).
src/DataTypes/Serializations/SerializationDynamicElement.h Adds pooling factory + destructor + getName() for Dynamic element wrapper serialization.
src/DataTypes/Serializations/SerializationDynamicElement.cpp Implements destructor removal; switches variant element serializations to pooled factories.
src/DataTypes/Serializations/SerializationDynamic.h Adds pooling factory + destructor + getName() for Dynamic serialization.
src/DataTypes/Serializations/SerializationDynamic.cpp Implements getName() and destructor removal from pool.
src/DataTypes/Serializations/SerializationDetached.h Adds pooling factory + destructor + getName() for Detached serialization.
src/DataTypes/Serializations/SerializationDetached.cpp Implements getName() and destructor removal from pool.
src/DataTypes/Serializations/SerializationDecimalBase.h Adds getName() implementation for decimal base serialization.
src/DataTypes/Serializations/SerializationDecimal.h Adds pooling factory + destructor for Decimal serialization.
src/DataTypes/Serializations/SerializationDecimal.cpp Implements destructor removal from pool (templated).
src/DataTypes/Serializations/SerializationDateTime64.h Adds pooling factory + destructor + getName() for DateTime64 serialization.
src/DataTypes/Serializations/SerializationDateTime64.cpp Implements getName() and destructor removal from pool.
src/DataTypes/Serializations/SerializationDateTime.h Adds pooling factory + destructor + getName() for DateTime and Time serializations.
src/DataTypes/Serializations/SerializationDateTime.cpp Implements getName() and destructor removal from pool for DateTime and Time.
src/DataTypes/Serializations/SerializationDate32.h Adds pooling factory + destructor + getName() for Date32 serialization.
src/DataTypes/Serializations/SerializationDate32.cpp Implements getName() and destructor removal from pool.
src/DataTypes/Serializations/SerializationDate.h Adds pooling factory + destructor + getName() for Date serialization.
src/DataTypes/Serializations/SerializationDate.cpp Implements getName() and destructor removal from pool.
src/DataTypes/Serializations/SerializationBool.h Adds pooling factory + destructor + getName() for Bool serialization.
src/DataTypes/Serializations/SerializationBool.cpp Implements getName() and destructor removal from pool.
src/DataTypes/Serializations/SerializationArrayOffsets.h Adds pooling factory + destructor + getName() for Array offsets serialization.
src/DataTypes/Serializations/SerializationArrayOffsets.cpp Implements getName() and destructor removal from pool.
src/DataTypes/Serializations/SerializationArray.h Adds pooling factory + destructor + getName() for Array serialization.
src/DataTypes/Serializations/SerializationArray.cpp Implements getName()/destructor removal and switches offset/number/named serializations to pooled creation.
src/DataTypes/Serializations/SerializationAggregateFunction.h Adds pooling factory + destructor + getName() for AggregateFunction serialization.
src/DataTypes/Serializations/SerializationAggregateFunction.cpp Implements getName() and destructor removal from pool.
src/DataTypes/Serializations/ISerialization.h Makes destructor pure virtual and introduces mandatory getName().
src/DataTypes/Serializations/ISerialization.cpp Defines ISerialization destructor out-of-line and includes pool header.
src/DataTypes/NullableUtils.cpp Switches nullable serialization creation to pooled factory.
src/DataTypes/IDataType.cpp Switches Sparse/Detached/Replicated serialization wrapping to pooled factories.
src/DataTypes/DataTypesNumber.h Switches default number serialization to pooled factory creation.
src/DataTypes/DataTypesDecimal.cpp Switches default decimal serialization to pooled factory creation.
src/DataTypes/DataTypeVariant.cpp Switches default variant serialization to pooled factory creation.
src/DataTypes/DataTypeUUID.cpp Switches default UUID serialization to pooled factory creation.
src/DataTypes/DataTypeTuple.cpp Switches named element + tuple serialization to pooled factories.
src/DataTypes/DataTypeTime64.cpp Switches default Time64 serialization to pooled factory creation.
src/DataTypes/DataTypeTime.cpp Switches default Time serialization to pooled factory creation.
src/DataTypes/DataTypeString.cpp Switches default String serialization (and versioned one) to pooled factory creation.
src/DataTypes/DataTypeQBit.cpp Switches default QBit serialization to pooled factory creation.
src/DataTypes/DataTypeObject.cpp Switches multiple object/dynamic related serializations to pooled factories.
src/DataTypes/DataTypeNumberBase.h Switches default number serialization to pooled factory creation.
src/DataTypes/DataTypeNullable.cpp Switches default Nullable serialization to pooled factory creation.
src/DataTypes/DataTypeNothing.cpp Switches default Nothing serialization to pooled factory creation.
src/DataTypes/DataTypeMap.cpp Switches map nested serializations to pooled factories (named/tuple/array/map).
src/DataTypes/DataTypeLowCardinality.cpp Switches default LowCardinality serialization to pooled factory creation.
src/DataTypes/DataTypeInterval.cpp Switches default Interval serialization to pooled factory creation.
src/DataTypes/DataTypeIPv4andIPv6.h Switches default IPv4/IPv6 serialization to pooled factory creation.
src/DataTypes/DataTypeFixedString.cpp Switches default FixedString serialization to pooled factory creation.
src/DataTypes/DataTypeEnum.cpp Switches default Enum serialization to pooled factory creation.
src/DataTypes/DataTypeDynamic.cpp Switches default Dynamic + element serialization to pooled factory creation.
src/DataTypes/DataTypeDomainBool.cpp Switches domain Bool serialization to pooled factory creation.
src/DataTypes/DataTypeDateTime64.cpp Switches default DateTime64 serialization to pooled factory creation.
src/DataTypes/DataTypeDateTime.cpp Switches default DateTime serialization to pooled factory creation.
src/DataTypes/DataTypeDate32.cpp Switches default Date32 serialization to pooled factory creation.
src/DataTypes/DataTypeDate.cpp Switches default Date serialization to pooled factory creation.
src/DataTypes/DataTypeArray.cpp Switches default Array serialization to pooled factory creation.
src/DataTypes/DataTypeAggregateFunction.cpp Switches default AggregateFunction serialization to pooled factory creation.
src/Columns/tests/gtest_column_object.cpp Uses DataTypeDynamic default serialization instead of direct SerializationDynamic instantiation.
src/Columns/ColumnObject.cpp Replaces thread-local SerializationDynamic shared_ptr with DataTypeDynamic default serialization pointer.

Rename SerializationCacheBytes/SerializationCacheBytesUncorrected to
SerializationCacheBytesInMemoryAllocated/SerializationCacheBytesInMemory
to follow the existing naming pattern (like TotalPrimaryKeyBytesInMemory
and TotalPrimaryKeyBytesInMemoryAllocated).

#96563 (comment)
… overrides

Apply azat's suggestion from PR review: change the default
ISerialization::allocatedBytes() to return sizeof(*this) instead of 0.
This makes the base class provide a sensible default, allowing removal
of all subclass overrides that had the identical trivial body.
Centralizes the hashing of SerializationInfoSettings fields into a
dedicated updateHash method, so that adding a new field to the struct
requires updating only one place instead of every caller.

#96563 (comment)
@nikitamikhaylov

Copy link
Copy Markdown
Member Author

BuzzHouse test failure fix: #99938

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

Just 3 minor comments, overall looks good

@clickhouse-gh

clickhouse-gh Bot commented Mar 21, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.80% +0.00%
Functions 24.50% 24.50% +0.00%
Branches 76.40% 76.40% +0.00%

PR changed lines: PR changed-lines coverage: 91.64% (1271/1387, 7 noise lines excluded)
Diff coverage report
Uncovered code

@nikitamikhaylov
nikitamikhaylov added this pull request to the merge queue Mar 23, 2026
Merged via the queue into master with commit 7713822 Mar 23, 2026
147 of 148 checks passed
@nikitamikhaylov
nikitamikhaylov deleted the serialization-object-pool branch March 23, 2026 12:31
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 23, 2026
CurtizJ added a commit to CurtizJ/ClickHouse that referenced this pull request Mar 25, 2026
…tion-object-pool"

This reverts commit 7713822, reversing
changes made to c62581c.
CurtizJ added a commit to CurtizJ/ClickHouse that referenced this pull request Mar 25, 2026
…erialization-object-pool""

This reverts commit ad3e951.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product 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.

7 participants