Rachelint commented on code in PR #11718:
URL: https://github.com/apache/datafusion/pull/11718#discussion_r1696734828
##########
datafusion/physical-plan/src/aggregates/group_values/row.rs:
##########
@@ -120,12 +120,13 @@ impl GroupValues for GroupValuesRows {
batch_hashes.resize(n_rows, 0);
create_hashes(cols, &self.random_state, batch_hashes)?;
- for (row, &hash) in batch_hashes.iter().enumerate() {
- let entry = self.map.get_mut(hash, |(_hash, group_idx)| {
+ for (row, &target_hash) in batch_hashes.iter().enumerate() {
+ let entry = self.map.get_mut(target_hash, |(exist_hash,
group_idx)| {
// verify that a group that we are inserting with hash is
// actually the same key value as the group in
// existing_idx (aka group_values @ row)
- group_rows.row(row) == group_values.row(*group_idx)
+ target_hash == *exist_hash
Review Comment:
> I am somewhat confused about how this makes things faster -- I thought
that the check for equal hash was done as part of `self.map.get_mut` (aka the
closure is only called when the hashes are equal, so I would expct this
comparison to always be true)
>
> However, if the benchmark results show it is an improvement wonderful.
>
> I'll run the numbers as well to confirm.
This is my thought about why faster.
I read the source code in hashbrown, and the `get_mut` procedure is like
this:
- Use the `hash value` to find the first bucket.
- If the bucket is filled, it will use the `eq` function passed by us to
check if it is the target, for example, the prev `eq` function.
```rust
|(exist_hash, group_idx)| {
// verify that a group that we are inserting with hash is
// actually the same key value as the group in
// existing_idx (aka group_values @ row)
group_rows.row(row) == group_values.row(*group_idx)
})
```
- If `eq` return true, it is the target, otherwise we need to prob next and
check.
In the high cardinality aggr scenario, the entry often actually not exist in
the hash table.
And after the hash table grow too large(much bucket is filled), the prob
will perform much times and finally find nothing...
In this sitution, check the hash first can reduce the random memory accesses
compared to directy check the group value through group index.
--
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]