From 8035ed932d0785950410cee348bb2a3d3c41b81d Mon Sep 17 00:00:00 2001 From: Reuben Bond <203839+ReubenBond@users.noreply.github.com> Date: Tue, 9 Jul 2024 21:10:47 -0700 Subject: [PATCH] Replace custom GetHashCode implementations with HashCode.Combine (#9059) --- src/Orleans.Core/Runtime/RingRange.cs | 16 +++++++++------- .../Common/EventSequenceToken.cs | 5 +---- .../Common/EventSequenceTokenV2.cs | 6 +----- src/Orleans.Streaming/Core/StreamIdentity.cs | 9 +-------- .../Internal/StreamHandshakeToken.cs | 8 +------- .../Internal/StreamSubscriptionHandleImpl.cs | 1 + .../PubSub/PubSubPublisherState.cs | 9 +-------- src/Orleans.Streaming/QueueId.cs | 2 +- .../MultiStateTransactionalBitArrayGrain.cs | 5 +---- .../DistributedTM/ParticipantId.cs | 10 +--------- .../CodeGenTests/IRuntimeCodeGenGrain.cs | 12 +----------- .../StreamLifecycleTestGrains.cs | 5 +---- 12 files changed, 20 insertions(+), 68 deletions(-) diff --git a/src/Orleans.Core/Runtime/RingRange.cs b/src/Orleans.Core/Runtime/RingRange.cs index 81934ca01c..8c6fcedef8 100644 --- a/src/Orleans.Core/Runtime/RingRange.cs +++ b/src/Orleans.Core/Runtime/RingRange.cs @@ -55,6 +55,7 @@ internal sealed class SingleRange : IRingRangeInternal, IEquatable, { [Id(0)] private readonly uint begin; + [Id(1)] private readonly uint end; @@ -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}"; @@ -175,7 +176,7 @@ public static class RangeFactory /// /// The ring size. /// - public const long RING_SIZE = ((long)uint.MaxValue) + 1; + public const long RING_SIZE = (long)uint.MaxValue + 1; /// /// Represents an empty range. @@ -240,6 +241,7 @@ internal sealed class GeneralMultiRange : IRingRangeInternal, ISpanFormattable { [Id(0)] private readonly List ranges; + [Id(1)] private readonly long rangeSize; @@ -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) { @@ -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; @@ -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)); } } } diff --git a/src/Orleans.Streaming/Common/EventSequenceToken.cs b/src/Orleans.Streaming/Common/EventSequenceToken.cs index 9c620e6306..8eeb24e65b 100644 --- a/src/Orleans.Streaming/Common/EventSequenceToken.cs +++ b/src/Orleans.Streaming/Common/EventSequenceToken.cs @@ -96,10 +96,7 @@ public override int CompareTo(StreamSequenceToken other) } /// - public override int GetHashCode() - { - return (EventIndex * 397) ^ SequenceNumber.GetHashCode(); - } + public override int GetHashCode() => HashCode.Combine(SequenceNumber, EventIndex); /// public override string ToString() diff --git a/src/Orleans.Streaming/Common/EventSequenceTokenV2.cs b/src/Orleans.Streaming/Common/EventSequenceTokenV2.cs index 94381e6358..516951ac9c 100644 --- a/src/Orleans.Streaming/Common/EventSequenceTokenV2.cs +++ b/src/Orleans.Streaming/Common/EventSequenceTokenV2.cs @@ -96,11 +96,7 @@ public override int CompareTo(StreamSequenceToken other) } /// - public override int GetHashCode() - { - // why 397? - return (EventIndex * 397) ^ SequenceNumber.GetHashCode(); - } + public override int GetHashCode() => HashCode.Combine(SequenceNumber, EventIndex); /// public override string ToString() diff --git a/src/Orleans.Streaming/Core/StreamIdentity.cs b/src/Orleans.Streaming/Core/StreamIdentity.cs index 64c9ceb031..c368233d82 100644 --- a/src/Orleans.Streaming/Core/StreamIdentity.cs +++ b/src/Orleans.Streaming/Core/StreamIdentity.cs @@ -1,4 +1,3 @@ - using System; using System.Collections.Generic; using Orleans.Runtime; @@ -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; /// - public override int GetHashCode() - { - var hashCode = -1455462324; - hashCode = hashCode * -1521134295 + this.Guid.GetHashCode(); - hashCode = hashCode * -1521134295 + EqualityComparer.Default.GetHashCode(this.Namespace); - return hashCode; - } + public override int GetHashCode() => HashCode.Combine(Guid, Namespace); } } diff --git a/src/Orleans.Streaming/Internal/StreamHandshakeToken.cs b/src/Orleans.Streaming/Internal/StreamHandshakeToken.cs index 5343485fb0..e947507a53 100644 --- a/src/Orleans.Streaming/Internal/StreamHandshakeToken.cs +++ b/src/Orleans.Streaming/Internal/StreamHandshakeToken.cs @@ -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] diff --git a/src/Orleans.Streaming/Internal/StreamSubscriptionHandleImpl.cs b/src/Orleans.Streaming/Internal/StreamSubscriptionHandleImpl.cs index 61f0141804..89810f5064 100644 --- a/src/Orleans.Streaming/Internal/StreamSubscriptionHandleImpl.cs +++ b/src/Orleans.Streaming/Internal/StreamSubscriptionHandleImpl.cs @@ -132,6 +132,7 @@ public async Task DeliverBatch(IBatchContainer batch, Stre { this.expectedToken = StreamHandshakeToken.CreateDeliveyToken(batch.SequenceToken); } + return null; } diff --git a/src/Orleans.Streaming/PubSub/PubSubPublisherState.cs b/src/Orleans.Streaming/PubSub/PubSubPublisherState.cs index d9849eb000..596f81a02c 100644 --- a/src/Orleans.Streaming/PubSub/PubSubPublisherState.cs +++ b/src/Orleans.Streaming/PubSub/PubSubPublisherState.cs @@ -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() { diff --git a/src/Orleans.Streaming/QueueId.cs b/src/Orleans.Streaming/QueueId.cs index 1015940893..493120a868 100644 --- a/src/Orleans.Streaming/QueueId.cs +++ b/src/Orleans.Streaming/QueueId.cs @@ -80,7 +80,7 @@ public int CompareTo(QueueId other) public override bool Equals(object? obj) => obj is QueueId queueId && Equals(queueId); /// - 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); diff --git a/src/Orleans.Transactions.TestKit.Base/Grains/MultiStateTransactionalBitArrayGrain.cs b/src/Orleans.Transactions.TestKit.Base/Grains/MultiStateTransactionalBitArrayGrain.cs index d8431929e2..5ae592a71b 100644 --- a/src/Orleans.Transactions.TestKit.Base/Grains/MultiStateTransactionalBitArrayGrain.cs +++ b/src/Orleans.Transactions.TestKit.Base/Grains/MultiStateTransactionalBitArrayGrain.cs @@ -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; diff --git a/src/Orleans.Transactions/DistributedTM/ParticipantId.cs b/src/Orleans.Transactions/DistributedTM/ParticipantId.cs index 70ab9db123..bd69c05b21 100644 --- a/src/Orleans.Transactions/DistributedTM/ParticipantId.cs +++ b/src/Orleans.Transactions/DistributedTM/ParticipantId.cs @@ -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); } } diff --git a/test/DefaultCluster.Tests/CodeGenTests/IRuntimeCodeGenGrain.cs b/test/DefaultCluster.Tests/CodeGenTests/IRuntimeCodeGenGrain.cs index 6d658ccdfc..d1896b27e0 100644 --- a/test/DefaultCluster.Tests/CodeGenTests/IRuntimeCodeGenGrain.cs +++ b/test/DefaultCluster.Tests/CodeGenTests/IRuntimeCodeGenGrain.cs @@ -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) { diff --git a/test/Grains/TestInternalGrains/StreamLifecycleTestGrains.cs b/test/Grains/TestInternalGrains/StreamLifecycleTestGrains.cs index 51bd030708..9ec04377ae 100644 --- a/test/Grains/TestInternalGrains/StreamLifecycleTestGrains.cs +++ b/test/Grains/TestInternalGrains/StreamLifecycleTestGrains.cs @@ -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