Skip to content

perf: vectorize Spark hash loops for arrays without nulls - #7687

Open
mbutrovich wants to merge 2 commits into
apache:mainfrom
mbutrovich:perf-hash-zip-loops
Open

mbutrovich wants to merge 2 commits into
apache:mainfrom
mbutrovich:perf-hash-zip-loops

Conversation

@mbutrovich

@mbutrovich mbutrovich commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

  • None.

Rationale for this change

The loops that hash fixed-width columns without nulls in Comet's murmur3 and xxhash64 index both the values and the hash buffer per row (hash_array_primitive!). Inside create_murmur3_hashes, LLVM reloads the values pointer and checks bounds on every row, and reports loop not vectorized: cannot identify array bounds. In the 1.2.0 jar, create_murmur3_hashes has no vector multiply in either the amd64 or the aarch64 libcomet.so. Murmur3 is the hash for shuffle hash partitioning (multi_partition.rs).

What changes are included in this PR?

The loops for arrays without nulls iterate the hash buffer and the values together, so the primitive, float, boolean, interval, and small decimal loops can vectorize. With this change, the Int32 and Int64 murmur3 loops vectorize in a thin-LTO libcomet built with -Ctarget-cpu=x86-64-v3 and with -Ctarget-cpu=neoverse-n1. Each column's length is now checked against the hash buffer, because a zipped loop would skip rows instead of panicking.

The loops for arrays with nulls are unchanged.

Benchmark results

These are local runs on an Apple M5 Max. They don't predict the Neoverse-N1 and x86-64-v3 builds that Comet releases. Both sides used -Ctarget-cpu=native and thin LTO, and they alternated within one run. The aligned layout adds -C llvm-args=-align-all-functions=7 -C llvm-args=-align-all-nofallthru-blocks=7 to both sides. xxhash64 stays scalar on ARM, so its bars say nothing about x86-64-v3. The datafusion-spark case doesn't reach the changed code.

hash-zip-local

Could a reviewer with an x86 machine benchmark this against main? I don't have x86 hardware, and the xxhash64 case is the open question: on x86-64-v3, LLVM vectorizes it by emulating 64-bit multiplies with vpmuludq, and only a benchmark shows whether that beats the scalar loop. On both sides:

cd native
RUSTFLAGS="-Ctarget-cpu=x86-64-v3" cargo bench -p datafusion-comet-spark-expr \
  --bench xxhash64 --bench murmur3 --bench hash -- \
  'fallback/custom_seed_i64$|compatible/primitive_i64$|^spark_xxhash64/8192/|^spark_murmur3_hash/8192/|^murmur3/int32$'

compatible/primitive_i64 runs through datafusion-spark, which this PR doesn't change, so it shows the noise floor. The same run on a Neoverse-N1 machine with -Ctarget-cpu=neoverse-n1 would cover the other release target. A shuffle-heavy TPC-H or TPC-DS run with an x86-64-v3 release jar would show the effect on shuffle hash partitioning.

How are these changes tested?

The existing hash tests check the hash values. test_hashes_buffer_length_mismatch_is_error in murmur3.rs checks the length check for both hashers.

AI usage disclosure

I wrote this PR with help from Claude but understand and support the core approach, implementation, and test coverage.

@github-actions github-actions Bot added enhancement New feature or request performance area:expressions Expression evaluation labels Nov 19, 2026
@mbutrovich
mbutrovich marked this pull request as ready for review November 19, 2026 18:42

@comphead comphead left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the zipped loops and the up-front length check read well. I traced the recursive callers (the list, map and struct fallbacks, dictionary unpacking, and the batched list gather). Each one passes a buffer as long as its column, so the new check can't fire on a valid plan. One test point inline. I don't have x86 hardware either, so I can't answer the xxhash64 question.

}

#[test]
fn test_hashes_buffer_length_mismatch_is_error() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check lives in the shared create_hashes_internal!, so this test and the one in murmur3.rs run the same code. test_non_canonical_nan_hashes_like_canonical_nan in murmur3.rs already checks both hashers in one test because they expand the same macros. Doing the same here would keep murmur3::tests private and drop length_mismatch_arrays as a cross-module helper.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, merged into one test in murmur3.rs that runs both hashers, and murmur3::tests is private again.

@mbutrovich

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback, @comphead!

@mbutrovich
mbutrovich requested a review from comphead November 19, 2026 22:06

@comphead comphead left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, great PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:expressions Expression evaluation enhancement New feature or request performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants