Skip to content

Reduce Keeper memory usage with compact children set - #99860

Merged
antonio2368 merged 4 commits into
masterfrom
keeper-children-compact
Mar 19, 2026
Merged

antonio2368 merged 4 commits into
masterfrom
keeper-children-compact

Conversation

@antonio2368

@antonio2368 antonio2368 commented Mar 18, 2026 •

Copy link
Copy Markdown
Member

Reduce memory consumption of Keeper by replacing absl::flat_hash_set with CompactChildrenSet for storing node children. The new container stores 0–1 children inline without heap allocation, which covers the majority of Keeper nodes. This reduces KeeperMemNode size from 144 to 128 bytes.

Changelog category (leave one):

  • Performance Improvement

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

Reduce memory consumption of Keeper by replacing absl::flat_hash_set with CompactChildrenSet for storing node children. The new container stores 0–1 children inline without heap allocation, which covers the majority of Keeper nodes. This reduces KeeperMemNode size from 144 to 128 bytes.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.3.1.846

Introduce `CompactChildrenSet`, a memory-optimized container for Keeper
node children that stores 0-1 children inline (no heap allocation) and
switches to a flat hash set only when there are 2+ children. This reduces
`KeeperMemNode` size from 144 to 128 bytes.

Most Keeper nodes have 0 or 1 children, so this optimization avoids
heap allocation overhead for the common case, significantly reducing
memory consumption in large Keeper deployments.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@antonio2368
antonio2368 force-pushed the keeper-children-compact branch from 98ce87f to 39702a0 Compare March 18, 2026 10:41
@clickhouse-gh

clickhouse-gh Bot commented Mar 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [eb64358]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Mar 18, 2026
@antonio2368

Copy link
Copy Markdown
Member Author

On some snapshots I checked I noticed that 66% of nodes were leaf nodes and 33% had only 1 child.

@Algunenano Algunenano self-assigned this Mar 18, 2026
- `performance-unnecessary-copy-initialization`: suppress with NOLINT
  for intentional copy-constructor tests
- `readability-isolate-declaration`: split multi-variable bool declaration

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=99860&sha=39702a03f5987cb452c10d9f16f9ff30bc31dfbc&name_0=PR&name_1=Build%20%28arm_tidy%29

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Comment thread src/Coordination/CompactChildrenSet.cpp Resolved
Comment thread src/Coordination/CompactChildrenSet.cpp Resolved
ConstIterator end() const;

/// Returns the number of bytes allocated on the heap for this container.
/// Zero for empty/single states (inline storage), approximate for set mode.

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 comment is a bit confusing. I think it'd be better to only mention inline storage and not empty/single states, since you might call reserve and technically you expect the storage to be empty (nothing was inserted) but it's already using set mode and heap memory. I find empty() vs isEmpty() confusing too, but I don't know a better name for isEmpty() that isn't too verbose. Maybe you do

Comment thread src/Coordination/CompactChildrenSet.h Resolved
- Use a union for `sv` and `set_it` in `ConstIterator` since they are
  never active simultaneously, reducing iterator size from 40 to 24 bytes
- Add `chassert` in the set-to-single demotion path to verify the
  remaining child name is non-empty
- Fix `heapSizeInBytes` to use `capacity` instead of `size`

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
`LOGICAL_ERROR` exceptions call `abortOnFailedAssertion` in debug and
sanitizer builds, so `EXPECT_THROW` can never catch them — the process
aborts first. Wrap the test body in `#ifndef DEBUG_OR_SANITIZER_BUILD`,
matching the established pattern in `gtest_memory_resize.cpp`.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=99860&sha=latest&name_0=PR

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.80% +0.00%
Functions 23.90% 23.90% +0.00%
Branches 76.30% 76.30% +0.00%

PR changed lines: PR changed-lines coverage: 98.59% (560/568, 1 noise lines excluded)
Diff coverage report
Uncovered code

@antonio2368
antonio2368 added this pull request to the merge queue Mar 19, 2026
Merged via the queue into master with commit 4686f06 Mar 19, 2026
308 of 318 checks passed
@antonio2368
antonio2368 deleted the keeper-children-compact branch March 19, 2026 10:23
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 19, 2026
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 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