Repository navigation
Speed up uniq over numeric types by batching inserts when possible - #95904
Conversation
|
Workflow [PR], commit [c6b037b] Summary: ❌
|
…e same It isn't necessarily better or worse, just different. And the main change is not intended to alter this
|
Checking the improvement to destroy_aggregate_states:
I'm going to remove this change so the comparison of the batch changes is more precise (less noise for the change here). |
There was a problem hiding this comment.
Pull request overview
This PR optimizes the uniq aggregate function for numeric types by introducing batch processing for hash value computation and insertion operations. The optimization applies when processing non-null numeric columns (excluding IPv6 and String types) without conditional aggregation flags.
Changes:
- Added a new
insertManymethod toUniquesHashSetthat processes elements in batches of 8 - Modified
AggregateFunctionUniqto use the batched insertion path for eligible numeric types - Added boundary condition tests to verify correctness across different input sizes
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/AggregateFunctions/UniquesHashSet.h | Implements batched hash computation and insertion logic with configurable batch size |
| src/AggregateFunctions/AggregateFunctionUniq.h | Routes eligible numeric types through the new batched insertion path |
| tests/queries/0_stateless/03821_uniq_speedup_boundaries.sql | Adds test cases for batch boundaries and correctness verification |
| tests/queries/0_stateless/03821_uniq_speedup_boundaries.reference | Expected output for boundary condition tests |
|
Some perf test failed because play wasn't accessible. I'm rerunning failed jobs |
pamarcos
left a comment
There was a problem hiding this comment.
Couple of minor comments, but LGTM
| SELECT uniq(number) FROM numbers(17); -- Two full batches + 1 | ||
|
|
||
| -- Force resize during batching (depends on UNIQUES_HASH_MAX_SIZE) | ||
| -- Note this returns 100315 due to BJKST thinning (expect ~100000, actual varies). |
There was a problem hiding this comment.
I find the comment misleading, considering we expect a fixed value of 100315
There was a problem hiding this comment.
You are right. The file expects 100315. A person seeing the query expects 1000000. I added the comment to make it more clear but I'm not sure it helps
| while (buf[place_value] && buf[place_value] != x) | ||
| { | ||
| ++place_value; | ||
| place_value &= mask(); | ||
|
|
||
| #ifdef UNIQUES_HASH_SET_COUNT_COLLISIONS | ||
| ++collisions; | ||
| #endif | ||
| } | ||
|
|
||
| if (buf[place_value] == x) | ||
| continue; | ||
|
|
||
| buf[place_value] = x; | ||
| ++m_size; |
There was a problem hiding this comment.
Nitpick: this code is exactly the same as the one in insertImpl. I guess this is on purpose cause refactoring to consolidate into a single method doesn't seem to be much worthy.
7906ebc
|
@Algunenano destroy_aggregate_states have significant slowdown for ARM |
The generated assembly is the same.: #95904 (comment) In fact you can look for recent runs in master and the timings are the same as they were in the reference of this PR: https://s3.amazonaws.com/clickhouse-test-reports/REFs/master/8bf15e5389183849f574b0b90750ed50db66e22b/performance_comparison_arm_release_release_base_5_6/all-queries.html
|













Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Speed up
uniqover numeric types by batching inserts when possible (not null, not -If, no GROUP BY, no IPv6 or String).We have some performance tests already in cpu_synthetic:
For reference, in my PC with release (no-LTO) build,
clickhouse benchmark --port 49000 --query "SELECT uniq(UserID) from test.hits settings max_threads = 1" -i 200did 27.3 QPS, and now it does 33.6 QPS, that is 1.23x as fast.Documentation entry for user-facing changes
Version info
26.2.1.326