jayzhan211 commented on code in PR #27559:
URL: https://github.com/apache/datafusion/pull/27559#discussion_r4727425981
##########
datafusion/common/src/hash_utils.rs:
##########
@@ -305,23 +305,112 @@ fn hash_array_primitive<T>(
"hashes_buffer and array should be of equal length"
);
- if array.null_count() == 0 {
+ match array.nulls().filter(|nulls| nulls.null_count() > 0) {
+ None if rehash => {
+ hash_prim_rehash_dense(array.values(), hashes_buffer, random_state)
+ }
+ None => hash_prim_fresh_dense(array.values(), hashes_buffer,
random_state),
+ // Null rows keep their previous hash, so an all-null array, such as a
+ // column that ROLLUP or GROUPING SETS nulls out, changes nothing
+ Some(nulls) if nulls.null_count() == nulls.len() => {}
+ Some(nulls) => hash_prim_with_nulls(
+ array.values(),
+ nulls,
+ hashes_buffer,
+ random_state,
+ rehash,
+ ),
+ }
+}
+
+/// The positions of the set bits of `bits`, lowest first
+#[cfg(not(feature = "force_hash_collisions"))]
+fn set_bits(mut bits: u64) -> impl Iterator<Item = usize> {
+ std::iter::from_fn(move || {
+ (bits != 0).then(|| {
+ let i = bits.trailing_zeros() as usize;
+ bits &= bits - 1;
+ i
+ })
+ })
+}
+
+/// The fewest valid rows in a 64-row chunk for which hashing every row of the
+/// chunk and restoring its null rows costs less than hashing only its valid
rows
+/// one at a time.
+/// The null densities in the `with_hashes` benchmark cluster around where
random
+/// chunks cross this count, so rerun it when changing the value.
+#[cfg(not(feature = "force_hash_collisions"))]
+const MIN_VALID_FOR_DENSE: u32 = 48;
+
+/// Hashes 64 rows at a time. A chunk with at least [`MIN_VALID_FOR_DENSE`]
+/// valid rows is hashed in place with the dense loops, which have no branch
per
+/// row, and the previous hashes of its null rows are saved before and written
+/// back after. A sparser chunk, and the last `len % 64` rows, hash only their
+/// valid rows.
+#[cfg(not(feature = "force_hash_collisions"))]
+fn hash_prim_with_nulls<N, S>(
+ values: &[N],
+ nulls: &NullBuffer,
+ hashes: &mut [u64],
+ state: &S,
+ rehash: bool,
+) where
+ N: HashValue + Copy,
+ S: HashState,
+{
+ let chunks = nulls.inner().bit_chunks();
+ let (value_chunks, value_tail) = values.as_chunks::<64>();
+ let (hash_chunks, hash_tail) = hashes.as_chunks_mut::<64>();
+ let rows = value_chunks.iter().zip(hash_chunks.iter_mut());
+ for ((values, hashes), valid) in rows.zip(chunks.iter()) {
+ if valid.count_ones() < MIN_VALID_FOR_DENSE {
Review Comment:
@mbutrovich thanks! On the default x86-64 target, 99% random nulls is still
1.10x to 1.32x slower, and those arrays gain nothing from the chunk loop. I
tried the array-level `null_count` gate on an M4 Pro, keeping all-valid chunks
on the dense loops. It was 7% to 24% slower than this PR at 25% to 50% nulls,
so I agree with leaving it out. A cheaper option to try on the bot is skipping
all-null chunks before `count_ones`. At 99% random nulls, 53% of chunks are all
null, and everywhere else it costs one predictable compare. Fine as a follow-up
if it doesn't help.
```rs
if valid == 0 {
continue;
}
```
##########
datafusion/common/src/hash_utils.rs:
##########
@@ -305,23 +305,115 @@ fn hash_array_primitive<T>(
"hashes_buffer and array should be of equal length"
);
- if array.null_count() == 0 {
- if rehash {
- hash_prim_rehash_dense(array.values(), hashes_buffer,
random_state);
+ match array.nulls().filter(|nulls| nulls.null_count() > 0) {
+ None if rehash => {
+ hash_prim_rehash_dense(array.values(), hashes_buffer, random_state)
+ }
+ None => hash_prim_fresh_dense(array.values(), hashes_buffer,
random_state),
+ // Null rows keep their previous hash, so an all-null array, such as a
+ // column that ROLLUP or GROUPING SETS nulls out, changes nothing
+ Some(nulls) if nulls.null_count() == nulls.len() => {}
+ Some(nulls) if rehash => hash_prim_with_nulls::<_, _, true>(
+ array.values(),
+ nulls,
+ hashes_buffer,
+ random_state,
+ ),
+ Some(nulls) => hash_prim_with_nulls::<_, _, false>(
+ array.values(),
+ nulls,
+ hashes_buffer,
+ random_state,
+ ),
+ }
+}
+
+/// The positions of the set bits of `bits`, lowest first
+#[cfg(not(feature = "force_hash_collisions"))]
+fn set_bits(mut bits: u64) -> impl Iterator<Item = usize> {
+ std::iter::from_fn(move || {
+ (bits != 0).then(|| {
+ let i = bits.trailing_zeros() as usize;
+ bits &= bits - 1;
+ i
+ })
+ })
+}
+
+/// The fewest valid rows in a 64-row chunk for which hashing every row of the
+/// chunk and restoring its null rows costs less than hashing only its valid
rows
+/// one at a time.
+/// The null densities in the `with_hashes` benchmark cluster around where
random
+/// chunks cross this count, so rerun it when changing the value.
+#[cfg(not(feature = "force_hash_collisions"))]
+const MIN_VALID_FOR_DENSE: u32 = 48;
+
+/// Hashes 64 rows at a time. A chunk with at least [`MIN_VALID_FOR_DENSE`]
+/// valid rows is hashed in place with the dense loops, which have no branch
per
+/// row, and the previous hashes of its null rows are saved before and written
+/// back after. A sparser chunk, and the last `len % 64` rows, hash only their
+/// valid rows.
+#[cfg(not(feature = "force_hash_collisions"))]
+fn hash_prim_with_nulls<N, S, const REHASH: bool>(
+ values: &[N],
+ nulls: &NullBuffer,
+ hashes: &mut [u64],
+ state: &S,
+) where
+ N: HashValue + Copy,
+ S: HashState,
+{
+ let chunks = nulls.inner().bit_chunks();
+ let (value_chunks, value_tail) = values.as_chunks::<64>();
+ let (hash_chunks, hash_tail) = hashes.as_chunks_mut::<64>();
+ let rows = value_chunks.iter().zip(hash_chunks.iter_mut());
+ for ((values, hashes), valid) in rows.zip(chunks.iter()) {
+ if valid.count_ones() < MIN_VALID_FOR_DENSE {
+ hash_prim_valid::<_, _, REHASH>(values, hashes, valid, state);
+ continue;
+ }
+ // The check above leaves at most `64 - MIN_VALID_FOR_DENSE` null rows,
+ // so `kept` has room for every one of them
+ let mut kept = [0u64; 64 - MIN_VALID_FOR_DENSE as usize];
+ for (kept, i) in kept.iter_mut().zip(set_bits(!valid)) {
+ *kept = hashes[i];
+ }
+ if REHASH {
+ hash_prim_rehash_dense(values, hashes, state);
} else {
- hash_prim_fresh_dense(array.values(), hashes_buffer, random_state);
+ hash_prim_fresh_dense(values, hashes, state);
Review Comment:
@mbutrovich with `x86-64-v3`, chunks that take this branch on the first
column are slower than hashing their valid rows one at a time. That's why 15%
to 25% nulls lose 8% to 16% there, and why `int32` at 3% nulls gains nothing
(6.2 vs 6.4 us). The cause is already on `main`: this loop hashes 8192 `int32`
rows without nulls in 6.3 us with `x86-64-v3` and 4.2 us on the default target,
about the same as `main`'s row-by-row loop at 3% nulls (6.2 us). Could you file
an issue for the slow loop with `x86-64-v3` and link it here? Fixing that loop
would turn these cases into wins too, so it doesn't need to block this PR.
--
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]