Mryange opened a new pull request, #61730:
URL: https://github.com/apache/doris/pull/61730
### What problem does this PR solve?
Problem Summary:
In `AggregateFunctionUniq::add_batch_single_place` and `add_batch` (the hot
path
for `SELECT count(distinct col)`), every key's hash is computed **twice**:
1. `set.prefetch(keys[i + HASH_MAP_PREFETCH_DIST])` internally calls
`this->hash(key)` to locate the slot for CPU prefetch.
2. `set.insert(keys[i])` goes through `emplace` → `EmplaceDecomposable` →
`s.hash(key)` again to find or prepare the insert position.
The same codebase already has a correct "precompute hash + reuse" pattern in
the
`DistinctStreamingAgg` operator path (`hash_map_context.h`), where
`init_hash_values()` computes hash once, and subsequent `prefetch` /
`lazy_emplace_key` calls reuse the precomputed value. This PR applies the
same
optimization to `AggregateFunctionUniq`.
### How does this PR solve it?
For both `add_batch` and `add_batch_single_place` in
`aggregate_function_uniq.h`
and `aggregate_function_uniq_distribute_key.h`:
1. **Precompute hash values** into a `std::vector<size_t>` before the main
loop,
using `set.hash(keys[i])` — this is the only hash computation per key.
2. **Replace `set.prefetch(key)`** with
`set.prefetch_hash(hash_values[...])` —
reuses the precomputed hash, avoids recalculation and the unnecessary
memory
access to `keys[i + HASH_MAP_PREFETCH_DIST]` at prefetch time.
3. **Replace `set.insert(key)`** with `set.emplace_with_hash(hash_values[i],
key)`
— passes the precomputed hash directly, skipping the internal hash call.
Both `prefetch_hash` and `emplace_with_hash` are existing APIs in phmap
(`parallel_hashmap/phmap.h`), no third-party changes needed.
**Expected improvements:**
- Hash computation reduced from 2× to 1× per key
- The precompute loop is a pure sequential scan over `keys[]`, which is more
cache-friendly than interleaving hash computation with hash-table probing
- Better prefetch effectiveness: prefetch no longer needs to access
`keys[i + HASH_MAP_PREFETCH_DIST]` memory just to compute its hash
### Release note
None
### Check List (For Author)
- Test <!-- At least one of them must be included. -->
- [ ] Regression test
- [ ] Unit Test
- [ ] Manual test (add detailed scripts or steps below)
- [ ] No need to test or manual test. Explain why:
- [ ] This is a refactor/code format and no logic has been changed.
- [ ] Previous test can cover this change.
- [ ] No code files have been changed.
- [ ] Other reason <!-- Add your reason? -->
- Behavior changed:
- [ ] No.
- [ ] Yes. <!-- Explain the behavior change -->
- Does this need documentation?
- [ ] No.
- [ ] Yes. <!-- Add document PR link here. eg:
https://github.com/apache/doris-website/pull/1214 -->
### Check List (For Reviewer who merge this PR)
- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label <!-- Add branch pick label that this PR should
merge into -->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]