Skip to content

Commit 91466e1

Browse files
committed
GetHashCode
1 parent e9d37c6 commit 91466e1

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

src/StackExchange.Redis/RedisValue.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ private static int GetHashCode(RedisValue x)
382382
return x.Type switch
383383
{
384384
StorageType.Null => -1,
385+
StorageType.MemoryManager or StorageType.ByteArray => GetHashCode(x.RawSpan()),
386+
StorageType.Sequence => GetHashCode(x.RawSequence()),
385387
StorageType.Double => x.OverlappedValueDouble.GetHashCode(),
386388
StorageType.Int64 or StorageType.UInt64 => x._valueInt64.GetHashCode(),
387389
StorageType.String => x.RawString().GetHashCode(),
@@ -418,14 +420,12 @@ internal static unsafe bool Equals(byte[]? x, byte[]? y)
418420
return true;
419421
}
420422

421-
internal static unsafe int GetHashCode(ReadOnlySpan<byte> span)
423+
private static int AddHashCode(ReadOnlySpan<byte> span, int acc)
422424
{
423425
unchecked
424426
{
425427
int len = span.Length;
426-
if (len == 0) return 0;
427-
428-
int acc = 728271210;
428+
Debug.Assert(len > 0);
429429

430430
var span64 = MemoryMarshal.Cast<byte, long>(span);
431431
for (int i = 0; i < span64.Length; i++)
@@ -443,6 +443,30 @@ internal static unsafe int GetHashCode(ReadOnlySpan<byte> span)
443443
}
444444
}
445445

446+
internal static int GetHashCode(ReadOnlySpan<byte> span)
447+
{
448+
if (span.Length == 0) return 0;
449+
450+
return AddHashCode(span, HashCodeStart);
451+
}
452+
453+
private const int HashCodeStart = 728271210;
454+
455+
private static int GetHashCode(ReadOnlySequence<byte> seq)
456+
{
457+
if (seq.Length == 0) return 0;
458+
459+
int acc = HashCodeStart;
460+
461+
foreach (var memory in seq)
462+
{
463+
if (!memory.IsEmpty)
464+
acc = AddHashCode(memory.Span, acc);
465+
}
466+
467+
return acc;
468+
}
469+
446470
internal void AssertNotNull()
447471
{
448472
if (IsNull) throw new ArgumentException("A null value is not valid in this context");

0 commit comments

Comments
 (0)