Skip to content

Reduce polygon dictionary load memory and account for the lookup index - #105431

Merged
Algunenano merged 1 commit into
ClickHouse:masterfrom
Algunenano:polygon-dict-bytes-allocated
May 21, 2026
Merged

Algunenano merged 1 commit into
ClickHouse:masterfrom
Algunenano:polygon-dict-bytes-allocated

Conversation

@Algunenano

@Algunenano Algunenano commented May 20, 2026 •

Copy link
Copy Markdown
Member

Polygon dictionaries (polygon_index_cell / polygon_index_each) underreport their footprint in system.dictionaries.bytes_allocated and their load path has avoidable peak allocations.

Fixes in this PR:

  • bytes_allocated now 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 test 04204_polygon_dict_bytes_allocated asserts the index-bearing layouts (polygon_index_cell, polygon_index_each) report meaningfully more bytes than polygon_simple.

  • IPolygonDictionary::loadData no longer copies every polygon into a pair<Polygon, size_t> to sort by area; it sorts an index permutation and rebuilds the destination vectors via std::move. Peak load-time memory drops from ~3x the polygon storage to ~1x.

  • GridRoot::makeCell no longer constructs a fresh 128-thread ThreadPool on every recursive call; one pool is built in the GridRoot constructor and shared across the recursion.

  • SlabsPolygonIndex::uniqueX pre-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):

  • Improvement

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 make system.dictionaries.bytes_allocated correctly reflect the lookup index they build.

Version info

  • Merged into: 26.5.1.946

`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]>
@clickhouse-gh

clickhouse-gh Bot commented May 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c8e8682]

Summary: ❌

job_name test_name status info comment
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/check_report_messages.py FAIL IGNORED

AI Review

Summary

This PR improves PolygonDictionary memory accounting and load-time memory behavior: it defers calculateBytesAllocated to concrete constructors to include index bytes, avoids polygon deep copies during area-based sorting, reuses a single ThreadPool through grid construction, and adds a stateless regression test for system.dictionaries.bytes_allocated. After reviewing the current diff and code paths, I did not find unresolved correctness, safety, or performance regressions.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label May 20, 2026
@Avogar Avogar self-assigned this May 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 98.17% (107/109) · Uncovered code

Full report · Diff report

@Algunenano
Algunenano added this pull request to the merge queue May 21, 2026
Merged via the queue into ClickHouse:master with commit ac774d2 May 21, 2026
184 of 206 checks passed
@Algunenano
Algunenano deleted the polygon-dict-bytes-allocated branch May 21, 2026 13:55
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 21, 2026
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
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.

3 participants