Skip to content

[mono] Switch simdhash to prime bucket counts; increase load factor #113274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2025

Conversation

kg
Copy link
Member

@kg kg commented Mar 7, 2025

Supercedes (contains) #113241.

The current version of dn_simdhash_ght_compatible has especially bad performance on arm64 vs x64, so I took some steps to redesign it and address some codegen deficiencies. This has the added benefit of making it a bit faster on x64 as well.

There weren't benchmarks for ght_compatible in the benchmark suite originally, so I added a couple. (I was benchmarking end-to-end originally, which makes it harder to compare across platforms.)

The F14 research and my initial research into vectorized hashing both showed that power-of-two bucket counts delivered optimal performance, but later on when doing the Dictionary vectorization work I determined that power-of-two counts have vastly inferior collision resistance compared to prime bucket counts. GHashTable and SCG.Dictionary both use spaced primes for bucket counts, so this PR attempts to do the same for dn_simdhash.

The best-case performance (for an optimal hash) gets a little worse but when I benchmarked power-of-two counts with a synthetic bad hash function the perf regression was over 100x, versus ~2x for prime bucket counts.

I also improved the search suffix selection to match the better one I came up with from the vectorized dictionary research, and added some benchmarks for the bad hash function scenario.

Selected benchmark measurements from x64:

scenario simdhash u32_ptr simdhash_ght ghashtable
find missing keys 18.574ns 20.505ns 62.659ns
find random keys 22.574ns 26.241ns 47.767ns
find sequential keys n/a 42.262ns 38.103ns
find missing key with bad hash n/a 3.465ns 0.972ns
find random key with bad hash n/a 2.383ns 1.021ns

I did measurements on ampere arm64 as well and verified that it's okay there.

@kg kg changed the title [mono] Switch simdhash to prime bucket counts instead of power-of-two bucket counts [mono] Switch simdhash to prime bucket counts; increase load factor Mar 8, 2025
@kotlarmilos

This comment was marked as outdated.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot wasn't able to review any files in this pull request.

Files not reviewed (18)
  • src/mono/mono/metadata/bundled-resources.c: Language not supported
  • src/mono/mono/metadata/class.c: Language not supported
  • src/mono/mono/metadata/metadata.c: Language not supported
  • src/mono/mono/mini/interp/transform.c: Language not supported
  • src/mono/mono/mini/seq-points.c: Language not supported
  • src/native/containers/dn-simdhash-ght-compatible.c: Language not supported
  • src/native/containers/dn-simdhash-ght-compatible.h: Language not supported
  • src/native/containers/dn-simdhash-ptr-ptr.c: Language not supported
  • src/native/containers/dn-simdhash-ptrpair-ptr.c: Language not supported
  • src/native/containers/dn-simdhash-specialization.h: Language not supported
  • src/native/containers/dn-simdhash-string-ptr.c: Language not supported
  • src/native/containers/dn-simdhash-u32-ptr.c: Language not supported
  • src/native/containers/dn-simdhash-utils.h: Language not supported
  • src/native/containers/dn-simdhash.c: Language not supported
  • src/native/containers/dn-simdhash.h: Language not supported
  • src/native/containers/simdhash-benchmark/Makefile: Language not supported
  • src/native/containers/simdhash-benchmark/all-measurements.h: Language not supported
  • src/native/containers/simdhash-benchmark/benchmark.c: Language not supported

@kg
Copy link
Member Author

kg commented Mar 20, 2025

I think we should probably resume using GHashTable for the one table in question (#113287) since its performance is still slightly better, but we want to land these fixes/improvements to benefit the rest of the code that still uses simdhash. I did a quick audit of the rest of our tables and I think only the problem one happens to have a bad hash.

…ms like sometimes they don't get inlined

Add some measurements for simdhash ght

Improve makefile

Introduce scan data that's fetched once per bucket

Targeted simdhash optimizations via new dn_simdhash_ght_get_value_or_default

Fix indentation to satisfy CI build warnings

Fix build warnings

Switch to spaced prime bucket counts instead of power of two bucket counts for better collision resistance

Fix cache alignment of ght compatible and ptr ptr simdhash variants on 64-bit

Higher simdhash load factor (closer to ghashtable's; improves performance for bad hashes)

Improve benchmark overhead calculation

Add sequential keys measurements

Fix build on CI

Revert ght bucket size

Revert "Revert ght bucket size"

This reverts commit 2a225ad.

Bug fixes
Add murmurhash32 variant for use in improved mono hash

Increase load factor to 30%
@kotlarmilos
Copy link
Member

The build-time perf results measured with the test app from #110406 (comment) on a macOS‑arm64 host targeting iOS‑arm64 in Release configuration are below. The build‑time performance are unchanged with this PR compared to both .NET 9 and .NET 8.

AOT-compiling all assemblies for a non-interpreted build (.NET 8)

real	0m42.628s
user	0m34.013s
sys	0m2.602s

AOT-compiling all assemblies for a non-interpreted build (.NET 9)

real	0m40.490s
user	0m36.282s
sys	0m2.469s

AOT-compiling all assemblies for a non-interpreted build (.NET 10) - this PR

real	0m41.176s
user	0m36.441s
sys	0m2.594s

Copy link
Member

@kotlarmilos kotlarmilos left a comment

Choose a reason for hiding this comment

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

LGTM, good job!

@kg kg merged commit ca451bf into dotnet:main Apr 18, 2025
159 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators May 19, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants