Repository navigation
Reduce polygon dictionary load memory and account for the lookup index - #105431
Merged
Algunenano merged 1 commit intoMay 21, 2026
Merged
Conversation
`IPolygonDictionary::calculateBytesAllocated` only counted the polygon
key column, attribute columns, and raw polygon points. The lookup index
(`SlabsPolygonIndex` segment trees, `GridRoot` cells) was deliberately
omitted with a comment claiming it was a small share of the total.
For dense workloads (parcels, address polygons) the index dominates: a
200k-polygon `polygon_index_cell` reports 8.6 MiB while
`jemalloc.allocated` grows by ~412 MiB during load. On small server
pods this drives cold-start OOM kills that are invisible to dictionary
level memory tracking.
Changes:
- Add a virtual `getIndexBytesAllocated` and walk the cell tree so
`SlabsPolygonIndex`, `FinalCell`, `FinalCellWithSlabs`,
`DividedCell`, and `GridRoot` each report the bytes they own.
`calculateBytesAllocated` also includes the `polygons` vector and
the polygon-id-to-attribute-index map. Reported `bytes_allocated`
on the 200k-parcel workload becomes 433.78 MiB, matching jemalloc
within ~5%.
- `IPolygonDictionary::loadData` now sorts an index permutation
instead of deep-copying every polygon into a `pair<Polygon,
size_t>`. Final `polygons` and
`polygon_index_to_attribute_value_index` are rebuilt with
`std::move`, dropping peak extra memory during sort from O(polygon
bytes) to O(N * sizeof(Polygon)).
- `GridRoot` constructs a single shared `ThreadPool` and passes it to
every `makeCell` invocation. The previous code created a fresh
128-thread pool on every recursive call past
`kMultiProcessingDepth`, even though those levels never enqueued
work.
- `SlabsPolygonIndex::uniqueX` pre-reserves the gather buffer to
`sum(bg::num_points)` so the inner gather loop performs one
allocation instead of `log2(N)` reallocations.
Add `04204_polygon_dict_bytes_allocated` to assert that
`bytes_allocated` is meaningfully larger than `polygon_simple` for both
`polygon_index_cell` and `polygon_index_each`.
Note: `calculateBytesAllocated` had to move from the base constructor
into each concrete subclass. Calling it from the base would dispatch
through the base vtable and skip the derived `getIndexBytesAllocated`
override.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Contributor
|
Workflow [PR], commit [c8e8682] Summary: ❌
AI ReviewSummaryThis PR improves Final Verdict
|
1 task done
Avogar
approved these changes
May 20, 2026
Contributor
LLVM Coverage Report
Changed lines: 98.17% (107/109) · Uncovered code |
Merged
via the queue into
ClickHouse:master
with commit May 21, 2026
ac774d2
184 of 206 checks passed
1 task
DavidHe-2008
pushed a commit
to DavidHe-2008/ClickHouse
that referenced
this pull request
Jun 1, 2026
…es-allocated Reduce polygon dictionary load memory and account for the lookup index
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Polygon dictionaries (
polygon_index_cell/polygon_index_each) underreport their footprint insystem.dictionaries.bytes_allocatedand their load path has avoidable peak allocations.Fixes in this PR:
bytes_allocatednow accounts for the lookup index (slab segment trees + grid cells) plus the polygons vector capacity and the polygon-id-to-attribute-index map. New stateless test04204_polygon_dict_bytes_allocatedasserts the index-bearing layouts (polygon_index_cell,polygon_index_each) report meaningfully more bytes thanpolygon_simple.IPolygonDictionary::loadDatano longer copies every polygon into apair<Polygon, size_t>to sort by area; it sorts an index permutation and rebuilds the destination vectors viastd::move. Peak load-time memory drops from ~3x the polygon storage to ~1x.GridRoot::makeCellno longer constructs a fresh 128-threadThreadPoolon every recursive call; one pool is built in theGridRootconstructor and shared across the recursion.SlabsPolygonIndex::uniqueXpre-reserves the gather buffer to its known upper bound (bg::num_points) so it doesn't grow by capacity doubling.Closes ClickHouse/clickhouse-private#57161
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Reduce memory usage of polygon dictionaries (
polygon_index_cell,polygon_index_each) and makesystem.dictionaries.bytes_allocatedcorrectly reflect the lookup index they build.Version info
26.5.1.946