Repository navigation
Polygon dictionary memory accounting and load-path improvements - #104231
Algunenano wants to merge 1 commit into
Conversation
|
Workflow [PR], commit [c8e8682] AI ReviewSummaryThis PR improves polygon dictionary memory accounting and reduces load-path peak allocations, and it adds a new Findings❌ Blockers
Tests
Final VerdictStatus:
|
| size_t p = 0; | ||
| while (p < offset.polygon_offsets.size()) | ||
| { | ||
| const bool new_mp = offset.isFirstPolygonOfMultiPolygon(p); |
There was a problem hiding this comment.
This is out of the scope of the PR. If the PR does not change the code it's not responsible to fix all possible issues you find or adding tests to prove they are or aren't there. That will happen in a separate PR
`clickhouse_fuzzer` runs the full `clickhouse-local` binary (see
`programs/local/fuzzers/clickhouse_fuzzer.cpp`) which spins up a complete
clickhouse-server-grade runtime: JeMalloc arenas, OpenSSL initializer,
static initializers, and full query execution. Baseline RSS is close to
the libFuzzer default `rss_limit_mb=2048`, so fuzzed inputs that trigger
even moderate query-time allocations push the process past the limit.
CIDB shows 7 unrelated PRs in 7 days all hitting OOM at 2049-2056 Mb —
just barely over the 2048 Mb default. Failure mode is uniform:
libFuzzer out-of-memory (used: 2049-2056Mb; limit: 2048Mb) oom-<hash>
Bumping to 4096 Mb follows existing precedent for fuzzers with similar
workloads:
- `execute_query_fuzzer.options`: rss_limit_mb = 4096 (also runs query
execution; same author bumped this in commit bafb7d7)
- `data_type_deserialization_fuzzer.options`: rss_limit_mb = 4096
- `create_parser_fuzzer.options`: rss_limit_mb = 6144
The limit still bounds the target (it does not disable the check via
`rss_limit_mb=0`), so genuine memory regressions above 4 GB will still
be caught.
CIDB evidence (last 30 days, all 10 hits — 0 on master because libFuzzer
does not run on master regularly):
PR ClickHouse#99740 (4 hits, Apr 19-21)
PR ClickHouse#104231, ClickHouse#104849, ClickHouse#104956, ClickHouse#96844, ClickHouse#104510, ClickHouse#104492 (1 hit each,
May 13-15)
LLVM Coverage Report
Changed lines: 98.17% (107/109) · Uncovered code |
`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]>
81cbce8 to
c8e8682
Compare
|
Closing to reopen with a clear history |
|
Superseded by #105431 — the branch was squashed to a single commit containing only the memory-related changes; the reverted extract-side reserve / zero-ring handling is no longer in scope here. |
Polygon dictionaries (
polygon_index_cell/polygon_index_each) underreport their footprintin
system.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) plusthe polygons vector capacity and the polygon-id-to-attribute-index map. New stateless test
04204_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 via
std::move. Peak load-time memory drops from ~3x the polygon storage to ~1x.GridRoot::makeCellno longer constructs a fresh 128-threadThreadPoolon every recursivecall; one pool is built in the
GridRootconstructor 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.Documentation entry for user-facing changes