Home

Awesome

wyhash-dotnet

NuGet Build Status

Zero-allocation C# implementation of Wang Yi's 64-bit wyhash hash algorithm and wyrand PRNG.

wyhash is an extremely fast, portable hashing algorithm, and passes all of the SMHasher tests (which evaluates collision, dispersion and randomness qualities of hash functions).

Note wyhash-dotnet currently implements wyhash v1. v2 will be implemented once considered "stable".

Getting Started

Install the WyHash package from NuGet:

Install-Package WyHash

WyHash implements HashAlgorithm, so can be integrated into existing projects easily:

var seed = 42;
var hasher = WyHash64.Create(seed);

var result = hasher.ComputeHash(myData);

A common use case is getting the resulting hash as a 64-bit unsigned integer (ulong), so a static convenience method is also provided:

// Note that if no seed is specified, it default to 0
var seed = 42;
var result = WyHash64.ComputeHash64(myData, seed);

The wyrand PRNG is also implemented:

var rng = new WyRng(42);

// Generate pseudorandom values
ulong a64BitVal = rng.NextLong();
uint a32BitVal = rng.Next();

// Fill a buffer with pseudorandom bytes
var buffer = new byte[256];
rng.NextBytes(buffer);

// Span is also supported
Span<byte> spanBuffer = stackalloc byte[128];
rng.NextBytes(spanBuffer);

Performance & Future Work

Note wyhash-dotnet currently implements wyhash v1. v2 will be implemented once considered "stable".

Future improvements include support for Span and incremental hashing (useful for hashing streams).

At present (July 2019), wyhash is the fastest algorithm in the SMHasher benchmark.

On a dev laptop with 64GB RAM and an Intel Xeon CPU E3-1545M v5 2.90GHz CPU, this implementation can process data at a rate of around 5.5GB/s on .NET Core 3, or 3.3GB/s on .NET Core 2 or the .NET Framework - this is very fast.

The reason for the performance improvement on .NET Core 3+ is that it supports hardware intrinsics, and wyhash-dotnet uses the BMI2 MULX instruction to achieve faster 64-bit integer multiplication (on systems where BMI2 is available). Support for intrinsics won't make it into the .NET Framework, but will also be in the newly announced .NET 5.

Note that PInvokeing into a native DLL built using the reference C code (see the WyHash.Native project) achieves around 10.8GB/s (more or less RAM SPEED), so there is still work to do to bridge the performance gap between C# and native - I'm very much open to suggestions here!

Latest benchmarks (DataSize is the size of data hashed, in bytes):


BenchmarkDotNet=v0.13.5, OS=Windows 10 (10.0.19045.3086/22H2/2022Update)
AMD Ryzen 5 2600, 1 CPU, 12 logical and 6 physical cores
.NET SDK=7.0.107
  [Host]            : .NET 6.0.18 (6.0.1823.26907), X64 RyuJIT AVX2
  ShortRun-.NET 6.0 : .NET 6.0.18 (6.0.1823.26907), X64 RyuJIT AVX2

Job=ShortRun-.NET 6.0  Runtime=.NET 6.0  IterationCount=3  
LaunchCount=1  WarmupCount=3  

MethodDataSizeMeanErrorStdDevMinMaxRatioRankAllocatedAlloc Ratio
TestXxHash10017.77 ns1.179 ns0.065 ns17.71 ns17.84 ns0.972-NA
TestXxHashNative10021.89 ns4.087 ns0.224 ns21.75 ns22.14 ns1.194-NA
TestWyHashNative10014.58 ns1.336 ns0.073 ns14.53 ns14.67 ns0.791-NA
TestWyHash10018.40 ns2.422 ns0.133 ns18.30 ns18.55 ns1.003-NA
TestXxHash102489.38 ns10.387 ns0.569 ns88.95 ns90.02 ns0.733-NA
TestXxHashNative102481.17 ns3.474 ns0.190 ns80.98 ns81.36 ns0.672-NA
TestWyHashNative102464.38 ns2.001 ns0.110 ns64.27 ns64.49 ns0.531-NA
TestWyHash1024121.77 ns23.792 ns1.304 ns120.53 ns123.13 ns1.004-NA