Skip to content

aras-p/UnitySmolRapidhash

Repository files navigation

Rapidhash V3 port to C#/Unity/Burst

Port of rapidhash to C# (Unity+Burst). rapidhash is MIT license, and so is this port.

I made this as a package under Packages/org.nesnausk.smol-rapidhash in this repository, but really it is just the Packages/org.nesnausk.smol-rapidhash/Scripts/SmolRapidhash3.cs file that is barely over 100 lines long. You can just take it and use it (it needs Burst for the 128-bit multiply function, and enabling unsafe C# code in project settings).

API (similar to Unity.Collections package xxHash3 class:

  • static ulong Hash64<T>(ref T key) where T : unmanaged
  • static ulong Hash64<T>(T[] key) where T : unmanaged
  • static ulong Hash64<T>(Span<T> key) where T : unmanaged
  • static ulong Hash64<T>(NativeArray<T> key) where T : unmanaged
  • static ulong Hash64(void* key, long length)

Performance

Notes:

  • Burst approaches native (C) performance of rapidhash at larger input sizes. The calling benchmark program is just a C# (not Burst) script tested in the editor; might be cause of some overhead.
  • Rapidhash is always faster than xxhash (XXH3); the difference is more pronounced on arm64 (or at least on Apple CPUs).
  • Curiously enough, C#/Burst port of XXH3 (as provided by Unity.Collections package) is 30-40% slower than native (C) implementation. This slowdown is not there for rapidhash.

Ryzen 5950X / Windows / Visual Studio 2022 (17.14.23): rapidhash reaches about 38GB/s. Screenshot

Apple M4 Max / macOS / Xcode 16.1: rapidhash reaches about 67GB/s. Screenshot

About

Rapidhash port to Unity/C#/Burst

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages