Skip to content

Try to speedup HashTable deserialization - #104523

Closed
nickitat wants to merge 7 commits into
masterfrom
nickitat/hashtable-read-twolevel-size
Closed

nickitat wants to merge 7 commits into
masterfrom
nickitat/hashtable-read-twolevel-size

Conversation

@nickitat

@nickitat nickitat commented May 10, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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

todo

nickitat and others added 4 commits May 10, 2026 15:37
During deserialization all keys in the stream are unique by construction,
so the full emplace path (findCell, which checks keyEquals on every probe
step) is wasteful. Switch to insertForDeserialization, a new helper that
calls insertUniqueNonZero for non-zero keys (findEmptyCell only checks
isZero, not keyEquals) and copies directly into zeroValue storage for
the zero key.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
size() previously summed all 256 bucket sizes on every call; empty()
scanned until it found a non-empty bucket. Add a tracked m_size member:
- incremented in emplace when inserted = true
- recounted after constructors and read/readText that call insertUniqueNonZero
  directly, bypassing emplace

size() and empty() are now O(1).

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
The two insertIfNotPresent overloads were default-constructing Mapped
and then copy-assigning the actual value, performing two operations where
one suffices. Replace with a single placement-new that constructs Mapped
directly from the argument. For non-trivial Mapped types this avoids the
unnecessary default construction.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@nickitat nickitat added the ci-performance performance only label May 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ed28371]

Summary: ✅


AI Review

Summary

This PR optimizes hash table deserialization by avoiding key-equality checks on insert and by constructing Mapped values directly in insertIfNotPresent. The previously reported saved-hash deserialization correctness issue is fixed in current HEAD, and I did not find any remaining code-level findings in the patched lines.

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 ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label May 10, 2026
@nickitat nickitat removed the ci-performance performance only label May 10, 2026
Comment thread src/Common/HashTable/HashTable.h Resolved
HashMapCellWithSavedHash::read does not deserialize saved_hash, so
calling x.getHash(*this) on a freshly deserialized cell reads an
uninitialized field - undefined behaviour and wrong bucket placement.

Fix: take the cell by value, compute the hash from the key via
Hash::operator(), and call setHash before copying the cell into the
buffer. This ensures saved_hash is valid for both the keyEquals
fast-path and reinsert during any subsequent resize.
For cells without a saved hash, setHash is a no-op so there is no
behaviour change.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@groeneai groeneai mentioned this pull request May 10, 2026
1 task done
@nickitat nickitat added pr-performance Pull request with some performance improvements and removed pr-not-for-changelog This PR should not be mentioned in the changelog labels May 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 11, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 100.00% (20/20) · Uncovered code

Full report · Diff report

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

Labels

pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant