Skip to content

Commit 66a901f

Browse files
Rich-T-kidalamb
andauthored
perf: cache dictionary arc pointer (apache#24418)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes apache#23921. - this PR was created on top of apache#24416, so once that is merged in we can rebase this branch ## Rationale for this change append_val is called once per new group on the scalar (streaming) code path. Previously it hashed a single-element slice of the dictionary values array on every call, paying create_hashes fixed overhead 65k times for a high-cardinality batch. Caching the full values array hash keyed on Arc::ptr_eq collapses that to one vectorized hash pass per batch, yielding a 2× speedup on the all-unique case with no measurable regression elsewhere. <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. Please explain the problem you are trying to solve in terms of the user-visible behavior, rather than the implementation. For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of the implementation. "COUNT(DISTINCT) returns wrong results when the column contains nulls" is the user-visible problem. --> ## What changes are included in this PR? - DictionaryGroupValuesColumn gains a cached_values: Option<ArrayRef> field - append_val: on a cache miss (Arc::ptr_eq fails), hashes the entire dict.values() array into val_hashes and stores the Arc; on a hit, reuses val_hashes[val_idx] directly — eliminating the per-call slice(val_idx, 1) allocation and create_hashes call <!-- There is no need to duplicate the description in the issue here, but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? nothing logically changed. <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? no <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. If there are any breaking changes to public APIs, please add the `api change` label. --> --------- Co-authored-by: Andrew Lamb <[email protected]>
1 parent e2e94ae commit 66a901f

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

  • datafusion/physical-plan/src/aggregates/group_values/multi_group_by

‎datafusion/physical-plan/src/aggregates/group_values/multi_group_by/dictionary.rs‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ pub struct DictionaryGroupValuesColumn<K: ArrowDictionaryKeyType + Send + Sync>
5757
val_to_inner: Vec<usize>,
5858
/// Reusable hash buffer for the dictionary values array.
5959
val_hashes: Vec<u64>,
60+
/// The last `dict.values()` Arc hashed in `append_val`. When the incoming
61+
/// values array is `ptr_eq` to this, `val_hashes` can be reused directly.
62+
cached_values: Option<ArrayRef>,
6063
_phantom: PhantomData<K>,
6164
}
6265

@@ -73,6 +76,7 @@ impl<K: ArrowDictionaryKeyType + Send + Sync> DictionaryGroupValuesColumn<K> {
7376
random_state: AGGREGATION_HASH_SEED,
7477
val_to_inner: Vec::default(),
7578
val_hashes: Vec::default(),
79+
cached_values: None,
7680
_phantom: PhantomData,
7781
}
7882
}
@@ -159,6 +163,7 @@ impl<K: ArrowDictionaryKeyType + Send + Sync> DictionaryGroupValuesColumn<K> {
159163
}
160164

161165
fn hash_values(&mut self, values: &ArrayRef) {
166+
self.cached_values = None;
162167
self.val_hashes.clear();
163168
self.val_hashes.resize(values.len(), 0);
164169
create_hashes(
@@ -296,9 +301,25 @@ impl<K: ArrowDictionaryKeyType + Send + Sync> GroupColumn
296301
}
297302
Some(val_idx) => {
298303
let dict_values = dict.values();
299-
let single = dict_values.slice(val_idx, 1);
300-
self.hash_values(&single);
301-
self.find_or_insert_value(dict_values, val_idx, self.val_hashes[0])?
304+
// check if the dictionary values array we are hashing was already seen.
305+
// if its arc was already stored we dont need to rehash the entire array again
306+
// if its new hash the entire array and store an arc ptr for future use
307+
let cache_hit = self
308+
.cached_values
309+
.as_ref()
310+
.is_some_and(|c| Arc::ptr_eq(c, dict_values));
311+
if !cache_hit {
312+
self.val_hashes.clear();
313+
self.val_hashes.resize(dict_values.len(), 0);
314+
create_hashes(
315+
std::slice::from_ref(dict_values),
316+
&self.random_state,
317+
&mut self.val_hashes,
318+
)
319+
.unwrap();
320+
self.cached_values = Some(Arc::clone(dict_values));
321+
}
322+
self.find_or_insert_value(dict_values, val_idx, self.val_hashes[val_idx])?
302323
}
303324
};
304325
self.group_to_inner.push(inner_slot);

0 commit comments

Comments
 (0)