Skip to content

Commit

Permalink
fix: Removes Standart XxHash in favor of the built in one (#1598)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamronbatman authored Nov 18, 2023
1 parent e9642d6 commit 1769d47
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Projects/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<ItemGroup>
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.2.2" />
<PackageReference Include="PollGroup" Version="1.4.3" />
<PackageReference Include="Standart.Hash.xxHash.Signed" Version="4.0.5" />
<PackageReference Include="System.IO.Hashing" Version="8.0.0" />
<PackageReference Include="Zlib.Bindings" Version="1.11.0" />

<PackageReference Include="ModernUO.Serialization.Annotations" Version="2.8.0" />
Expand Down
68 changes: 41 additions & 27 deletions Projects/Server/Utilities/HashUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,12 @@
*************************************************************************/

using System;
using System.IO.Hashing;
using System.Numerics;
using System.Runtime.CompilerServices;
using Standart.Hash.xxHash;

namespace Server;

/// <summary>
/// Represents supported non-cryptographic fast hash algorithms.
/// </summary>
public enum FastHashAlgorithm
{
None, // Used for collisions where full-data is serialized instead
XxHash3_64, // xxHash3 64bit
XxHash_32, // xxHash 32bit
}

public static class HashUtility
{
// *************** DO NOT CHANGE THIS NUMBER ****************
Expand All @@ -38,27 +28,51 @@ public static class HashUtility
private const ulong xxHash3Seed = 9609125370673258709ul; // Randomly generated 64-bit prime number
private const uint xxHash1Seed = 665738807u; // Randomly generated 32-bit prime number

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong ComputeHash64(string? data, FastHashAlgorithm algorithm = FastHashAlgorithm.XxHash3_64) =>
algorithm switch
[ThreadStatic]
private static XxHash3 _xxHash3;

[ThreadStatic]
private static XxHash32 _xxHash32;

public static unsafe ulong ComputeHash64(string? str)
{
if (str == null)
{
FastHashAlgorithm.XxHash3_64 => ComputeXXHash3_64(data),
_ => throw new NotSupportedException($"Hash {algorithm} is not supported.")
};
return 0;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong ComputeXXHash3_64(string? data) => data == null ? 0 : xxHash3.ComputeHash(data, xxHash3Seed);
var hasher = _xxHash3 ??= new XxHash3(unchecked((long)xxHash3Seed));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ComputeHash32(string? data, FastHashAlgorithm algorithm = FastHashAlgorithm.XxHash_32) =>
algorithm switch
fixed (char* src = &str.GetPinnableReference())
{
FastHashAlgorithm.XxHash_32 => ComputeXXHash_32(data),
_ => throw new NotSupportedException($"Hash {algorithm} is not supported.")
};
hasher.Append(new ReadOnlySpan<byte>(src, str.Length * 2));
}

var result = hasher.GetCurrentHashAsUInt64();
hasher.Reset();

return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ComputeXXHash_32(string? data) => data == null ? 0 : xxHash32.ComputeHash(data, xxHash1Seed);
public static unsafe uint ComputeHash32(string? str)
{
if (str == null)
{
return 0;
}

var hasher = _xxHash32 ??= new XxHash32(unchecked((int)xxHash1Seed));

fixed (char* src = &str.GetPinnableReference())
{
hasher.Append(new ReadOnlySpan<byte>(src, str.Length * 2));
}

var result = hasher.GetCurrentHashAsUInt32();
hasher.Reset();

return result;
}

public static unsafe int GetNetFrameworkHashCode(this string? str)
{
Expand Down

0 comments on commit 1769d47

Please sign in to comment.