Skip to content

Commit 1769d47

Browse files
authored
fix: Removes Standart XxHash in favor of the built in one (#1598)
1 parent e9642d6 commit 1769d47

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

Projects/Server/Server.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<ItemGroup>
3535
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.2.2" />
3636
<PackageReference Include="PollGroup" Version="1.4.3" />
37-
<PackageReference Include="Standart.Hash.xxHash.Signed" Version="4.0.5" />
37+
<PackageReference Include="System.IO.Hashing" Version="8.0.0" />
3838
<PackageReference Include="Zlib.Bindings" Version="1.11.0" />
3939

4040
<PackageReference Include="ModernUO.Serialization.Annotations" Version="2.8.0" />

Projects/Server/Utilities/HashUtility.cs

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,12 @@
1414
*************************************************************************/
1515

1616
using System;
17+
using System.IO.Hashing;
1718
using System.Numerics;
1819
using System.Runtime.CompilerServices;
19-
using Standart.Hash.xxHash;
2020

2121
namespace Server;
2222

23-
/// <summary>
24-
/// Represents supported non-cryptographic fast hash algorithms.
25-
/// </summary>
26-
public enum FastHashAlgorithm
27-
{
28-
None, // Used for collisions where full-data is serialized instead
29-
XxHash3_64, // xxHash3 64bit
30-
XxHash_32, // xxHash 32bit
31-
}
32-
3323
public static class HashUtility
3424
{
3525
// *************** DO NOT CHANGE THIS NUMBER ****************
@@ -38,27 +28,51 @@ public static class HashUtility
3828
private const ulong xxHash3Seed = 9609125370673258709ul; // Randomly generated 64-bit prime number
3929
private const uint xxHash1Seed = 665738807u; // Randomly generated 32-bit prime number
4030

41-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
42-
public static ulong ComputeHash64(string? data, FastHashAlgorithm algorithm = FastHashAlgorithm.XxHash3_64) =>
43-
algorithm switch
31+
[ThreadStatic]
32+
private static XxHash3 _xxHash3;
33+
34+
[ThreadStatic]
35+
private static XxHash32 _xxHash32;
36+
37+
public static unsafe ulong ComputeHash64(string? str)
38+
{
39+
if (str == null)
4440
{
45-
FastHashAlgorithm.XxHash3_64 => ComputeXXHash3_64(data),
46-
_ => throw new NotSupportedException($"Hash {algorithm} is not supported.")
47-
};
41+
return 0;
42+
}
4843

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

52-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
53-
public static uint ComputeHash32(string? data, FastHashAlgorithm algorithm = FastHashAlgorithm.XxHash_32) =>
54-
algorithm switch
46+
fixed (char* src = &str.GetPinnableReference())
5547
{
56-
FastHashAlgorithm.XxHash_32 => ComputeXXHash_32(data),
57-
_ => throw new NotSupportedException($"Hash {algorithm} is not supported.")
58-
};
48+
hasher.Append(new ReadOnlySpan<byte>(src, str.Length * 2));
49+
}
50+
51+
var result = hasher.GetCurrentHashAsUInt64();
52+
hasher.Reset();
53+
54+
return result;
55+
}
5956

60-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
61-
public static uint ComputeXXHash_32(string? data) => data == null ? 0 : xxHash32.ComputeHash(data, xxHash1Seed);
57+
public static unsafe uint ComputeHash32(string? str)
58+
{
59+
if (str == null)
60+
{
61+
return 0;
62+
}
63+
64+
var hasher = _xxHash32 ??= new XxHash32(unchecked((int)xxHash1Seed));
65+
66+
fixed (char* src = &str.GetPinnableReference())
67+
{
68+
hasher.Append(new ReadOnlySpan<byte>(src, str.Length * 2));
69+
}
70+
71+
var result = hasher.GetCurrentHashAsUInt32();
72+
hasher.Reset();
73+
74+
return result;
75+
}
6276

6377
public static unsafe int GetNetFrameworkHashCode(this string? str)
6478
{

0 commit comments

Comments
 (0)