Skip to content

Commit 2ac83d3

Browse files
committed
Clean up
1 parent 26d290e commit 2ac83d3

File tree

5 files changed

+31
-11
lines changed

5 files changed

+31
-11
lines changed

src/libraries/Common/src/System/IO/Hashing/NonCryptographicHashAlgorithm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace System.IO.Hashing
1515
/// Represents a non-cryptographic hash algorithm.
1616
/// </summary>
1717
#if SYSTEM_PRIVATE_CORELIB
18-
#pragma warning disable CA1512
18+
#pragma warning disable CA1512
1919
internal
2020
#else
2121
public

src/libraries/Common/src/System/IO/Hashing/XxHash3.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ namespace System.IO.Hashing
1616
/// For methods that persist the computed numerical hash value as bytes,
1717
/// the value is written in the Big Endian byte order.
1818
/// </remarks>
19-
#if SYSTEM_PRIVATE_CORELIB
20-
#pragma warning disable CA1512
21-
internal
22-
#else
2319
#if NET
2420
[SkipLocalsInit]
2521
#endif
22+
#if SYSTEM_PRIVATE_CORELIB
23+
internal
24+
#else
2625
public
2726
#endif
2827
sealed unsafe class XxHash3 : NonCryptographicHashAlgorithm

src/libraries/Common/src/System/IO/Hashing/XxHash64.State.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace System.IO.Hashing
1414
{
15-
#if SYSTEM_PRIVATE_CORELIB
15+
#if SYSTEM_PRIVATE_CORELIB
1616
internal
1717
#else
1818
public

src/libraries/Common/src/System/IO/Hashing/XxHash64.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ namespace System.IO.Hashing
1616
/// the value is written in the Big Endian byte order.
1717
/// </remarks>
1818
#if SYSTEM_PRIVATE_CORELIB
19-
#pragma warning disable CA1512
2019
internal
2120
#else
2221
public

src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
#if !MONO && (TARGET_AMD64 || TARGET_ARM64)
5+
#define USE_XXHASH3
6+
#endif
7+
48
using System.Buffers;
59
using System.Buffers.Binary;
610
using System.Diagnostics;
711
using System.Diagnostics.CodeAnalysis;
812
using System.Globalization;
9-
using System.IO.Hashing;
1013
using System.Numerics;
1114
using System.Runtime.CompilerServices;
1215
using System.Runtime.InteropServices;
@@ -813,6 +816,16 @@ internal static int GetHashCodeOrdinalIgnoreCase(ReadOnlySpan<char> value)
813816
return Marvin.ComputeHash32OrdinalIgnoreCase(ref MemoryMarshal.GetReference(value), value.Length /* in chars, not bytes */, (uint)seed, (uint)(seed >> 32));
814817
}
815818

819+
#if USE_XXHASH3
820+
#if TARGET_AMD64
821+
private const int XxHash3Threshold = 64;
822+
#else
823+
// XxHash3 is less efficient on ARM64 due to the lack of 256/512-bit vectors
824+
// We should revisit this if we start using SVE in the hash implementation.
825+
private const int XxHash3Threshold = 128;
826+
#endif
827+
#endif
828+
816829
// Important GetNonRandomizedHashCode{OrdinalIgnoreCase} notes:
817830
//
818831
// Use if and only if 'Denial of Service' attacks are not a concern (i.e. never used for free-form user input),
@@ -832,12 +845,13 @@ internal unsafe int GetNonRandomizedHashCode()
832845
{
833846
fixed (char* src = &_firstChar)
834847
{
835-
// TODO-PR: find optimal threshold
836-
if (_stringLength >= 64)
848+
#if USE_XXHASH3
849+
if (_stringLength >= XxHash3Threshold)
837850
{
838851
uint byteLength = (uint)_stringLength * 2; // never overflows
839-
return unchecked((int)XxHash3.NonRandomizedHashToUInt64((byte*)src, byteLength));
852+
return unchecked((int)System.IO.Hashing.XxHash3.NonRandomizedHashToUInt64((byte*)src, byteLength));
840853
}
854+
#endif
841855

842856
Debug.Assert(src[Length] == '\0', "src[Length] == '\\0'");
843857
Debug.Assert(((int)src) % 4 == 0, "Managed string should start at 4 bytes boundary");
@@ -879,6 +893,14 @@ internal static unsafe int GetNonRandomizedHashCode(ReadOnlySpan<char> span)
879893
switch (length)
880894
{
881895
default:
896+
#if USE_XXHASH3
897+
if (length >= XxHash3Threshold)
898+
{
899+
uint byteLength = (uint)length * 2; // never overflows
900+
return unchecked((int)System.IO.Hashing.XxHash3.NonRandomizedHashToUInt64((byte*)src, byteLength));
901+
}
902+
#endif
903+
882904
do
883905
{
884906
length -= 4;

0 commit comments

Comments
 (0)