Repository navigation
perf: speed up non-null dictionary hash scatter - #27025
Rich-T-kid wants to merge 12 commits into
Conversation
Skip clearing the reusable hash buffer when a dictionary with non-null keys and values will overwrite every row. Gather dictionary hashes in independent lane groups so the CPU can overlap the key-indexed loads.
|
run benchmark with_hashes |
|
🤖 Benchmark running (GKE) | trigger CPU Details (lscpu)Comparing rich-T-kid/non-null-dictionary-hash-scatter (8c0a4ee) to 198ab43 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"Results will be posted here when complete File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing rich-T-kid/non-null-dictionary-hash-scatter (8c0a4ee) to 198ab43 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"CPU Details (lscpu)Details
Resource Usagewith_hashes — base (merge-base)
with_hashes — branch
File an issue against this benchmark runner |
|
run benchmark with_hashes |
|
Hi @Rich-T-kid, your benchmark configuration could not be parsed (#27025 (comment)). Error: Usage: Any benchmark name is accepted: Per-side configuration ( env:
# shared env is inherited by BOTH the build and the run, so build
# flags go here. Builds default to no debuginfo for speed; opt back
# in for hung-job gdb dumps and cap jobs to stay within memory:
CARGO_PROFILE_RELEASE_DEBUG: "1"
CARGO_BUILD_JOBS: "1"
baseline:
ref: v45.0.0
env:
# per-side env only reaches the benchmark run, not the build
DATAFUSION_RUNTIME_MEMORY_LIMIT: 1G
changed:
ref: v46.0.0
env:
DATAFUSION_RUNTIME_MEMORY_LIMIT: 2GPod size and CPU architecture (each key is optional and keeps the default when absent; requests and limits are set equal): resources:
cpu: "16"
memory: "128Gi"
arch: arm64 # arm64 or amd64Independent benchmark shards (optional; 1–8, default 1): run benchmark arrow_writer
shards: 4Each shard builds both revisions, measures its assigned cases, and posts its own report. Resources and the job deadline apply per shard. DataFusion requests specifying a shard count greater than 1 fail with an error. Omit shards or use shards: 1 for DataFusion. File an issue against this benchmark runner |
|
run benchmark with_hashes |
|
🤖 Benchmark running (GKE) | trigger CPU Details (lscpu)Comparing rich-T-kid/non-null-dictionary-hash-scatter (8c0a4ee) to 198ab43 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"Results will be posted here when complete File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing rich-T-kid/non-null-dictionary-hash-scatter (8c0a4ee) to 198ab43 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"CPU Details (lscpu)Details
Resource Usagewith_hashes — base (merge-base)
with_hashes — branch
File an issue against this benchmark runner |
Existing hash utility tests cover dictionary hashing; retain a concise comment explaining the lane-gather fast path.
|
@kumarUjjawal could you take a look 🙇 |
|
run benchmark with_hashes |
2 similar comments
|
run benchmark with_hashes |
|
run benchmark with_hashes |
kumarUjjawal
left a comment
There was a problem hiding this comment.
Thank you @Rich-T-kid
Left few comments please take a look.
|
🤖 Benchmark running (GKE) | trigger CPU Details (lscpu)Comparing rich-T-kid/non-null-dictionary-hash-scatter (cb54f47) to 198ab43 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"Results will be posted here when complete File an issue against this benchmark runner |
Keep the buffer-reuse and scatter dispatch checks in one place, validate the unchecked lookup invariant, and cover chunk, tail, multi-column, and buffer-reuse behavior.
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing rich-T-kid/non-null-dictionary-hash-scatter (cb54f47) to 198ab43 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"CPU Details (lscpu)Details
Resource Usagewith_hashes — base (merge-base)
with_hashes — branch
File an issue against this benchmark runner |
|
run benchmark with_hashes |
|
🤖 Benchmark running (GKE) | trigger CPU Details (lscpu)Comparing rich-T-kid/non-null-dictionary-hash-scatter (c7d9b3a) to 198ab43 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"Results will be posted here when complete File an issue against this benchmark runner |
|
🤖 Benchmark running (GKE) | trigger CPU Details (lscpu)Comparing rich-T-kid/non-null-dictionary-hash-scatter (c7d9b3a) to 198ab43 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"Results will be posted here when complete File an issue against this benchmark runner |
|
thank you for the in depth review @kumarUjjawal, I addressed all your comments! added the test back in as well if you could take another look. re-running the benchmakrs as well |
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing rich-T-kid/non-null-dictionary-hash-scatter (c7d9b3a) to 198ab43 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"CPU Details (lscpu)Details
Resource Usagewith_hashes — base (merge-base)
with_hashes — branch
File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing rich-T-kid/non-null-dictionary-hash-scatter (c7d9b3a) to 198ab43 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"CPU Details (lscpu)Details
Resource Usagewith_hashes — base (merge-base)
with_hashes — branch
File an issue against this benchmark runner |
|
🤖 Benchmark running (GKE) | trigger CPU Details (lscpu)Comparing rich-T-kid/non-null-dictionary-hash-scatter (c7d9b3a) to 198ab43 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"Results will be posted here when complete File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing rich-T-kid/non-null-dictionary-hash-scatter (c7d9b3a) to 198ab43 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"CPU Details (lscpu)Details
Resource Usagewith_hashes — base (merge-base)
with_hashes — branch
File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing rich-T-kid/non-null-dictionary-hash-scatter (90f551c) to 198ab43 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"CPU Details (lscpu)Details
Resource Usagewith_hashes — base (merge-base)
with_hashes — branch
File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing rich-T-kid/non-null-dictionary-hash-scatter (90f551c) to 198ab43 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"CPU Details (lscpu)Details
Resource Usagewith_hashes — base (merge-base)
with_hashes — branch
File an issue against this benchmark runner |
|
this is ready for review again @kumarUjjawal 🙇 |
Use one physical-null predicate for buffer reuse and scatter dispatch, keep collision builds conservative, enforce the unchecked gather bounds in release builds, and cover sliced u8/u16/i64 dictionaries.
|
run benchmark with_hashes |
|
🤖 Benchmark running (GKE) | trigger CPU Details (lscpu)Comparing rich-T-kid/non-null-dictionary-hash-scatter (1150cfc) to 4c7b307 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"Results will be posted here when complete File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing rich-T-kid/non-null-dictionary-hash-scatter (1150cfc) to 4c7b307 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"CPU Details (lscpu)Details
Resource Usagewith_hashes — base (merge-base)
with_hashes — branch
File an issue against this benchmark runner |
|
run benchmark with_hashes |
|
show benchmark queue |
|
Hi @Rich-T-kid, you asked to view the benchmark queue (#27025 (comment)).
File an issue against this benchmark runner |
|
🤖 Benchmark running (GKE) | trigger CPU Details (lscpu)Comparing rich-T-kid/non-null-dictionary-hash-scatter (1150cfc) to 4c7b307 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"Results will be posted here when complete File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing rich-T-kid/non-null-dictionary-hash-scatter (1150cfc) to 4c7b307 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"CPU Details (lscpu)Details
Resource Usagewith_hashes — base (merge-base)
with_hashes — branch
File an issue against this benchmark runner |
|
run benchmark with_hashes |
|
🤖 Benchmark running (GKE) | trigger CPU Details (lscpu)Comparing rich-T-kid/non-null-dictionary-hash-scatter (286f893) to cdc3bf7 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"Results will be posted here when complete File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing rich-T-kid/non-null-dictionary-hash-scatter (286f893) to cdc3bf7 (merge-base) diff Run configurationrun benchmark with_hashes
env:
BENCH_FILTER: "dictionary_utf8_int32.*no nulls"CPU Details (lscpu)Details
Resource Usagewith_hashes — base (merge-base)
with_hashes — branch
File an issue against this benchmark runner |
|
|
||
| // Oversized dictionaries are compacted before scattering. Only inspect | ||
| // logical nulls when compaction can remove all unreferenced null values. | ||
| array.values().len() > MAX_DICTIONARY_VALUES_PER_ROW * array.len() |
There was a problem hiding this comment.
No test covers the new logical_null_count() == 0 guard on the oversized-dictionary reuse branch. Deleting it, which lets with_hashes skip clear() when a key references a null value, would not fail any test.**
Failure scenario: Edit line 125 to drop && array.logical_null_count() == 0, then run with_hashes on a 1-row dictionary with key [0] and values [None, ...65 strings] after dirty_hash_buffer. The gate returns true and the buffer is not cleared. The compacted dictionary still has a null value, so the null-values scatter skips that row, and the callback receives u64::MAX instead of the null hash. The only oversized test (test_dictionary_with_unreferenced_null_reuses_buffer) references a non-null value. The null_values case in assert_matches is not oversized (3 values <= 64*11 rows), so the && short-circuits before reaching the check. Nothing fails.
| // Oversized dictionaries are compacted before scattering. Only inspect | ||
| // logical nulls when compaction can remove all unreferenced null values. | ||
| array.values().len() > MAX_DICTIONARY_VALUES_PER_ROW * array.len() | ||
| && array.logical_null_count() == 0 |
There was a problem hiding this comment.
The reuse gate again uses a logical null test (array.logical_null_count()), while the scatter dispatch uses physical null_count (dictionary_nulls).
Failure scenario: Whether with_hashes may skip clear() again depends on two different null predicates staying consistent: logical nulls of the uncompacted dictionary versus physical null_count of the compacted one. A later change to arrow's logical_nulls for some value type, or to compact_dictionary, can make the gate return true while dispatch takes a null path that leaves slots unwritten. Callers would then get the previous batch's hashes. This is the same coupling kumarUjjawal flagged.
| // Oversized dictionaries are compacted before scattering. Only inspect | ||
| // logical nulls when compaction can remove all unreferenced null values. | ||
| array.values().len() > MAX_DICTIONARY_VALUES_PER_ROW * array.len() | ||
| && array.logical_null_count() == 0 |
There was a problem hiding this comment.
logical_null_count() builds a full validity bitmap for the dictionary on every with_hashes call just to get one boolean, and for nested-dictionary values it walks every value.
Failure scenario: Take a sliced 8192-row dictionary whose values array has nulls and holds about 1M entries, which is the oversized case this branch targets. DictionaryArray::logical_nulls allocates a BooleanBufferBuilder for all rows and probes every key, and compact_oversized_dictionary then repeats an O(rows) pass. If the values are themselves a dictionary with null keys, values.logical_nulls() iterates all ~1M inner keys per batch, the very work compaction exists to avoid. This can cost more than the 64KB memset that skipping clear() saves.
| let has_null_keys = array.keys().null_count() != 0; | ||
| let has_null_values = array.values().null_count() != 0; | ||
| // `create_hashes` hashes the prefix selected by the output buffer length. | ||
| assert!( |
There was a problem hiding this comment.
The dispatcher still asserts only <=, so dictionaries alone accept a shorter buffer and hash just a prefix.
Failure scenario: create_hashes([&dict], rs, &mut vec![0; dict.len() - 1]) succeeds and hashes a prefix. The same call with an Int32 or Utf8 column panics in hash_array_primitive/hash_array. The test at the short buffer case (check_non_null_dictionary_hash_fast_path) locks in this dictionary-only leniency, so the inconsistent contract the reviewer flagged is still there.
| } | ||
| } | ||
|
|
||
| for (hash, key) in hash_tail.iter_mut().zip(key_tail) { |
There was a problem hiding this comment.
The tail loop again copy-pastes the if MULTI_COL { combine_hashes(dict_hash, *hash) } else { dict_hash } rule, and scatter_row holds a third copy (lines 649-653).
Failure scenario: The combine rule now lives in three places: the chunk body, the tail, and scatter_row. If one copy changes, for example by swapping combine_hashes argument order, rows in the 8-lane chunks, the tail rows, and dictionaries with nulls would hash equal values differently. Hash joins and repartitioning would silently mismatch rows.
|
|
||
| // Oversized dictionaries are compacted before scattering. Only inspect | ||
| // logical nulls when compaction can remove all unreferenced null values. | ||
| array.values().len() > MAX_DICTIONARY_VALUES_PER_ROW * array.len() |
There was a problem hiding this comment.
The gate copies compact_oversized_dictionary's threshold (values.len() > MAX_DICTIONARY_VALUES_PER_ROW * len) even though correctness does not need it.
Failure scenario: When there are no null keys and logical_null_count() == 0, the null-values scatter writes every row anyway, because every referenced value is valid. Compaction is therefore not what makes reuse safe, and the comment on lines 122-123 is misleading. Meanwhile a filtered dictionary with 100 rows and 200 values, one unreferenced null, is not oversized, so it is still cleared and takes the slower null-values path. The copied threshold also breaks silently if compact_oversized_dictionary changes, for example to the saturating_mul form used in multi_group_by.
| // Get the required size from the first array | ||
| let required_size = match iter.peek() { | ||
| Some(arr) => arr.as_dyn_array().len(), | ||
| // Get the required size and initialization policy from the first array |
There was a problem hiding this comment.
we can trim the comments
There was a problem hiding this comment.
scatter_dictionary_hashes, a public internal API that returns Result, now panics in release builds through the new dispatcher assert! when hashes_buffer is longer than the array. Before this change it silently stopped at the last key, and it already returns an internal error for a wrong dict_hashes length.
Failure scenario: A workspace caller passes a hashes slice sized to a batch longer than the dictionary column. Before this PR the zip stopped at keys.len(); now the whole aggregation thread panics with 'dictionary scatter requires at least one key per hash slot' instead of getting an _internal_err! it could propagate. This is a behavior change to a pub item that the PR description does not mention.
Which issue does this PR close?
Part of #26848. This is the non-null path; #26966 handles nullable keys.
Why
For a dictionary with non-null keys and values, scattering hashes overwrites every output row.
with_hasheswas still zeroing its reusable 8,192-row buffer first, and the scatter loop loaded and stored each row before starting the next key-indexed load.What changed
with_hashesnow keeps the initialized buffer contents when the first column is a non-null dictionary, since that column replaces every slot. The non-null scatter loop gathers eight dictionary hashes before updating the output, which gives the CPU independent loads to overlap.Nullable keys or values continue through the existing path. Short hash buffers also keep the existing truncation behavior.
Results
Run 1 · Run 2