14
14
*************************************************************************/
15
15
16
16
using System ;
17
+ using System . IO . Hashing ;
17
18
using System . Numerics ;
18
19
using System . Runtime . CompilerServices ;
19
- using Standart . Hash . xxHash ;
20
20
21
21
namespace Server ;
22
22
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
-
33
23
public static class HashUtility
34
24
{
35
25
// *************** DO NOT CHANGE THIS NUMBER ****************
@@ -38,27 +28,51 @@ public static class HashUtility
38
28
private const ulong xxHash3Seed = 9609125370673258709ul ; // Randomly generated 64-bit prime number
39
29
private const uint xxHash1Seed = 665738807u ; // Randomly generated 32-bit prime number
40
30
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 )
44
40
{
45
- FastHashAlgorithm . XxHash3_64 => ComputeXXHash3_64 ( data ) ,
46
- _ => throw new NotSupportedException ( $ "Hash { algorithm } is not supported.")
47
- } ;
41
+ return 0 ;
42
+ }
48
43
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 ) ) ;
51
45
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 ( ) )
55
47
{
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
+ }
59
56
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
+ }
62
76
63
77
public static unsafe int GetNetFrameworkHashCode ( this string ? str )
64
78
{
0 commit comments