Skip to content

Commit

Permalink
Replace custom GetHashCode implementations with HashCode.Combine (#9059)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReubenBond committed Jul 10, 2024
1 parent e033fbd commit 8035ed9
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 68 deletions.
16 changes: 9 additions & 7 deletions src/Orleans.Core/Runtime/RingRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ internal sealed class SingleRange : IRingRangeInternal, IEquatable<SingleRange>,
{
[Id(0)]
private readonly uint begin;

[Id(1)]
private readonly uint end;

Expand Down Expand Up @@ -105,7 +106,7 @@ public long RangeSize()

public override bool Equals(object? obj) => Equals(obj as SingleRange);

public override int GetHashCode() => (int)(begin ^ end);
public override int GetHashCode() => HashCode.Combine(GetType(), begin, end);

public override string ToString() => begin == 0 && end == 0 ? "<(0 0], Size=x100000000, %Ring=100%>" : $"{this}";

Expand Down Expand Up @@ -175,7 +176,7 @@ public static class RangeFactory
/// <summary>
/// The ring size.
/// </summary>
public const long RING_SIZE = ((long)uint.MaxValue) + 1;
public const long RING_SIZE = (long)uint.MaxValue + 1;

/// <summary>
/// Represents an empty range.
Expand Down Expand Up @@ -240,6 +241,7 @@ internal sealed class GeneralMultiRange : IRingRangeInternal, ISpanFormattable
{
[Id(0)]
private readonly List<SingleRange> ranges;

[Id(1)]
private readonly long rangeSize;

Expand Down Expand Up @@ -319,7 +321,7 @@ private static SingleRange GetEquallyDividedSubRange(SingleRange singleRange, in
for (int i = 0; i < numSubRanges; i++)
{
// (Begin, End]
uint end = (unchecked(start + portion));
uint end = unchecked(start + portion);
// I want it to overflow on purpose. It will do the right thing.
if (remainder > 0)
{
Expand All @@ -333,11 +335,11 @@ private static SingleRange GetEquallyDividedSubRange(SingleRange singleRange, in
throw new ArgumentException(nameof(mySubRangeIndex));
}

// Takes a range and devides it into numSubRanges equal ranges and returns the subrange at mySubRangeIndex.
// Takes a range and divides it into numSubRanges equal ranges and returns the subrange at mySubRangeIndex.
public static IRingRange GetEquallyDividedSubRange(IRingRange range, int numSubRanges, int mySubRangeIndex)
{
if (numSubRanges <= 0) throw new ArgumentException(nameof(numSubRanges));
if ((uint)mySubRangeIndex >= (uint)numSubRanges) throw new ArgumentException(nameof(mySubRangeIndex));
if (numSubRanges <= 0) throw new ArgumentOutOfRangeException(nameof(numSubRanges));
if ((uint)mySubRangeIndex >= (uint)numSubRanges) throw new ArgumentOutOfRangeException(nameof(mySubRangeIndex));

if (numSubRanges == 1) return range;

Expand All @@ -358,7 +360,7 @@ public static IRingRange GetEquallyDividedSubRange(IRingRange range, int numSubR
return new GeneralMultiRange(singlesForThisIndex);
}

default: throw new ArgumentException(nameof(range));
default: throw new ArgumentOutOfRangeException(nameof(range));
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/Orleans.Streaming/Common/EventSequenceToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ public override int CompareTo(StreamSequenceToken other)
}

/// <inheritdoc />
public override int GetHashCode()
{
return (EventIndex * 397) ^ SequenceNumber.GetHashCode();
}
public override int GetHashCode() => HashCode.Combine(SequenceNumber, EventIndex);

/// <inheritdoc />
public override string ToString()
Expand Down
6 changes: 1 addition & 5 deletions src/Orleans.Streaming/Common/EventSequenceTokenV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,7 @@ public override int CompareTo(StreamSequenceToken other)
}

/// <inheritdoc/>
public override int GetHashCode()
{
// why 397?
return (EventIndex * 397) ^ SequenceNumber.GetHashCode();
}
public override int GetHashCode() => HashCode.Combine(SequenceNumber, EventIndex);

/// <inheritdoc/>
public override string ToString()
Expand Down
9 changes: 1 addition & 8 deletions src/Orleans.Streaming/Core/StreamIdentity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

using System;
using System.Collections.Generic;
using Orleans.Runtime;
Expand Down Expand Up @@ -42,12 +41,6 @@ public StreamIdentity(Guid streamGuid, string streamNamespace)
public override bool Equals(object obj) => obj is StreamIdentity identity && this.Guid.Equals(identity.Guid) && this.Namespace == identity.Namespace;

/// <inheritdoc />
public override int GetHashCode()
{
var hashCode = -1455462324;
hashCode = hashCode * -1521134295 + this.Guid.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(this.Namespace);
return hashCode;
}
public override int GetHashCode() => HashCode.Combine(Guid, Namespace);
}
}
8 changes: 1 addition & 7 deletions src/Orleans.Streaming/Internal/StreamHandshakeToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,7 @@ public override bool Equals(object obj)
return Equals((StreamHandshakeToken)obj);
}

public override int GetHashCode()
{
unchecked
{
return (GetType().GetHashCode() * 397) ^ (Token != null ? Token.GetHashCode() : 0);
}
}
public override int GetHashCode() => HashCode.Combine(GetType(), Token);
}

[Serializable]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public async Task<StreamHandshakeToken> DeliverBatch(IBatchContainer batch, Stre
{
this.expectedToken = StreamHandshakeToken.CreateDeliveyToken(batch.SequenceToken);
}

return null;
}

Expand Down
9 changes: 1 addition & 8 deletions src/Orleans.Streaming/PubSub/PubSubPublisherState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,7 @@ public bool Equals(QualifiedStreamId streamId, GrainId streamProducer)
{
return !left.Equals(right);
}
public override int GetHashCode()
{
// This code was auto-generated by ReSharper
unchecked
{
return (Stream.GetHashCode() * 397) ^ (Producer != default ? Producer.GetHashCode() : 0);
}
}
public override int GetHashCode() => HashCode.Combine(Stream, Producer);

public override string ToString()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Orleans.Streaming/QueueId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public int CompareTo(QueueId other)
public override bool Equals(object? obj) => obj is QueueId queueId && Equals(queueId);

/// <inheritdoc/>
public override int GetHashCode() => (int)queueId ^ (int)uniformHashCache ^ (queueNamePrefix?.GetHashCode() ?? 0);
public override int GetHashCode() => HashCode.Combine(queueId, uniformHashCache, queueNamePrefix);

public static bool operator ==(QueueId left, QueueId right) => left.Equals(right);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ public override bool Equals(object obj)
return Equals((BitArrayState) obj);
}

public override int GetHashCode()
{
return (value != null ? value.GetHashCode() : 0);
}
public override int GetHashCode() => HashCode.Combine(value);

private static readonly int BitsInInt = sizeof(int) * 8;

Expand Down
10 changes: 1 addition & 9 deletions src/Orleans.Transactions/DistributedTM/ParticipantId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,7 @@ public bool Equals(ParticipantId x, ParticipantId y)
return string.CompareOrdinal(x.Name, y.Name) == 0 && Equals(x.Reference, y.Reference);
}

public int GetHashCode(ParticipantId obj)
{
unchecked
{
var idHashCode = (obj.Name != null) ? obj.Name.GetHashCode() : 0;
var referenceHashCode = (obj.Reference != null) ? obj.Reference.GetHashCode() : 0;
return (idHashCode * 397) ^ (referenceHashCode);
}
}
public int GetHashCode(ParticipantId obj) => HashCode.Combine(obj.Name, obj.Reference);
}
}

Expand Down
12 changes: 1 addition & 11 deletions test/DefaultCluster.Tests/CodeGenTests/IRuntimeCodeGenGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,7 @@ public override bool Equals(object obj)
return this.Equals((@event)obj);
}

public override int GetHashCode()
{
unchecked
{
var hashCode = (this.@if != null ? this.@if.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (this.@public != null ? this.@public.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ this.privateId.GetHashCode();
hashCode = (hashCode * 397) ^ this.Id.GetHashCode();
return hashCode;
}
}
public override int GetHashCode() => HashCode.Combine(@if, @public, privateId, Id, Enum);

public bool Equals(@event other)
{
Expand Down
5 changes: 1 addition & 4 deletions test/Grains/TestInternalGrains/StreamLifecycleTestGrains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ public override bool Equals(object obj)
return A.Equals(item.A) && B.Equals(item.B);
}

public override int GetHashCode()
{
return (B * 397) ^ (A != null ? A.GetHashCode() : 0);
}
public override int GetHashCode() => HashCode.Combine(A, B);
}

public class AsyncObserverArg : GenericArg
Expand Down

0 comments on commit 8035ed9

Please sign in to comment.