Repository navigation
Optimize aggregate functions uniqHLL12 and uniqCombined - #109831
Conversation
Overallocate the bitset of `CompactArray` by one padding byte and access cells through an unaligned little-endian 16-bit word, which is always in bounds and always covers a whole cell. This removes the one-byte/two-byte branch of `Locus`, which is unpredictable on random buckets (~50/50 for 6-bit cells) and dominated the cost of `HyperLogLogCounter::update` (2.76x on the isolated update loop). The serialized format is unchanged: the new method `getSerializableState` returns a `std::span` over the bitset without the trailing padding byte, and `HyperLogLogCounter` now serializes field-wise (rank store, then denominator, then the number of zeros) instead of dumping the raw object. Verified byte-identical: state dumps from the old and new binaries match, and a cross-binary write/read matrix returns identical results. Remove `Locus` and `Reader` entirely: their last real consumer, `readAndMerge`, was removed in 7521fd2, and since then they were referenced only by the example, which is rewritten to the new interface. `uniqHLL12` speeds up by 12-28% on hits_100m (it has no batch insertion path, so it is reachable only by this change), `uniqCombined` and `uniqCombined64` by up to 23% without GROUP BY and up to 12% with GROUP BY on low-cardinality keys; no regressions. Co-Authored-By: Claude Fable 5 <[email protected]>
uniqHLL12 and uniqCombined
|
Workflow [PR], commit [ea66303] Summary: ❌
AI ReviewSummaryThis PR rewrites Findings❌ Blockers
Tests
Final VerdictStatus: ❌ Block
|
|
📊 Cloud Performance Report ✅ AI verdict: This PR rewrites the uniqCombined aggregate hashing and makes the HyperLogLog CompactArray rank store use branchless 16-bit access, so its effect should land on COUNT(DISTINCT ...) queries. Q4 (COUNT(DISTINCT UserID)) shows a -17.3% improvement that is directly on this hot path and consistent with the change, but it stays within master's current variance band [200,282 ms], so it is reported as no change rather than a confirmed win. Q15 is a plain GROUP BY UserID aggregation the PR does not touch; its flagged -21.3% improvement has no plausible cause here, so it is downgraded to inconclusive as measurement noise. clickbenchFlagged queries (1 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
`hashOne` returned `HashValueType`, truncating `CityHash64` to 32 bits for `String` and `IPv6`, while `Data::Key` is deliberately `UInt64` for these types: plain `uniqCombined` keeps the full 64-bit hash for them for compatibility of aggregate function states. Return `Key` instead, restoring states byte-identical to master; fixes the failures of test 02751_ip_types_aggregate_functions_states. Suggested in: ClickHouse#109831 (comment) Co-Authored-By: Claude Fable 5 <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 90/104 (86.54%) · Uncovered code |
|
Performance tests for isolated change from this PR (after merge of #109794): https://performance.ci.clickhouse.com/runs/pr-109831-ea663038ac44cae4c7ea81c84b7b72a6ebf76616
|
| /// The last byte is padding for the branchless 16-bit access; it stays zero and is not serialized. | ||
| UInt8 bitset[BITSET_SIZE + 1] = { 0 }; |
There was a problem hiding this comment.
If we know BITSET_SIZE in the compile time, then we can calculate the needed array size in the compile time as well. IDK if it was doing to save 1 byte for some cases.
There was a problem hiding this comment.
Do you mean that we can avoid allocating 1 extra byte if we know in advance that we won't hit the tail? I think it's not worth it.
60fb246
Building master with -DENABLE_BENCHMARKS=ON failed to compile src/Storages/benchmarks/benchmark_statistics.cpp with two errors of the form "template argument for template type parameter must be a type" at lines 48 and 50. Two changes landed from branches that never saw each other. ClickHouse#109831 (ea66303) gave AggregateFunctionUniqCombined a ColumnType template parameter in second position, making it template <typename T, typename ColumnType, UInt8 K, typename HashValueType> ClickHouse#111221 (40da35b, "Reduce binary size for microbenchmark") then replaced registerAggregateFunctions() in this benchmark with a benchmark-local factory that hand-instantiates the class using the old three-argument spelling. The literal 12 therefore binds to the ColumnType position, where a type is required. Supply the missing argument at the two call sites, using the spelling the real factory uses for numeric types (AggregateFunctionUniqCombined.h:373, ColumnVector<TYPE>). This is behaviour-preserving rather than a choice among candidates: before ClickHouse#109831 the class hardcoded assert_cast<const ColumnVector<T> &>(*columns[0]) in its body, and that commit only hoisted the hardcoded type into the new parameter. No CI job caught this because ENABLE_BENCHMARKS defaults to OFF (CMakeLists.txt:159) and grep -rn ENABLE_BENCHMARKS ci/ returns no hits, so no build compiles this translation unit. Validated in both directions: on the unmodified tree the target fails with exactly those two errors and no others; with this change it compiles, links, and the four UniqV2 benchmark cases run, covering both the UInt64 and Float64 paths. The other eight benchmark targets compile cleanly, so all nine benchmark executables now build. Closes: ClickHouse#116251
`hashOne` returned `HashValueType`, truncating `CityHash64` to 32 bits for `String` and `IPv6`, while `Data::Key` is deliberately `UInt64` for these types: plain `uniqCombined` keeps the full 64-bit hash for them for compatibility of aggregate function states. Return `Key` instead, restoring states byte-identical to master; fixes the failures of test 02751_ip_types_aggregate_functions_states. Suggested in: ClickHouse#109831 (comment) Co-Authored-By: Claude Fable 5 <[email protected]>
Optimize aggregate functions `uniqHLL12` and `uniqCombined`




Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Optimized aggregate functions
uniqHLL12anduniqCombined.Additional Context
Just rewrite suboptimal code in
HyperLogLogCounter.Based on #109794. I opened it in a separate PR because I want to check the perf tests of this change isolated after #109794 is merged.
Version info
26.7.1.1300(included in26.7and later)