Skip to content

Commit 681ceab

Browse files
committed
Simplify HashCode
1 parent 8f0e2e4 commit 681ceab

File tree

17 files changed

+51
-122
lines changed

17 files changed

+51
-122
lines changed

src/BenchmarkDotNet/Analysers/Conclusion.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,9 @@ public override bool Equals(object obj)
6464

6565
public override int GetHashCode()
6666
{
67-
unchecked
68-
{
69-
int hashCode = AnalyserId.GetHashCode();
70-
hashCode = (hashCode * 397) ^ (int) Kind;
71-
hashCode = Mergeable
72-
? (hashCode * 397) ^ Message.GetHashCode()
73-
: (hashCode * 397) ^ Report?.ToString().GetHashCode() ?? string.Empty.GetHashCode();
74-
return hashCode;
75-
}
67+
return Mergeable
68+
? HashCode.Combine(AnalyserId, Kind, Message)
69+
: HashCode.Combine(AnalyserId, Kind, Report?.ToString());
7670
}
7771
}
7872
}

src/BenchmarkDotNet/Columns/SizeUnit.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,7 @@ public override bool Equals(object obj)
6666
return Equals((SizeUnit) obj);
6767
}
6868

69-
public override int GetHashCode()
70-
{
71-
unchecked
72-
{
73-
int hashCode = (Name != null ? Name.GetHashCode() : 0);
74-
hashCode = (hashCode * 397) ^ (Description != null ? Description.GetHashCode() : 0);
75-
hashCode = (hashCode * 397) ^ ByteAmount.GetHashCode();
76-
return hashCode;
77-
}
78-
}
69+
public override int GetHashCode() => HashCode.Combine(Name, Description, ByteAmount);
7970

8071
public static bool operator ==(SizeUnit left, SizeUnit right) => Equals(left, right);
8172

src/BenchmarkDotNet/Engines/GcStats.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,6 @@ private static long CalculateAllocationQuantumSize()
223223

224224
public override bool Equals(object obj) => obj is GcStats other && Equals(other);
225225

226-
public override int GetHashCode()
227-
{
228-
unchecked
229-
{
230-
int hashCode = Gen0Collections;
231-
hashCode = (hashCode * 397) ^ Gen1Collections;
232-
hashCode = (hashCode * 397) ^ Gen2Collections;
233-
hashCode = (hashCode * 397) ^ AllocatedBytes.GetHashCode();
234-
hashCode = (hashCode * 397) ^ TotalOperations.GetHashCode();
235-
return hashCode;
236-
}
237-
}
226+
public override int GetHashCode() => HashCode.Combine(Gen0Collections, Gen1Collections, Gen2Collections, AllocatedBytes, TotalOperations);
238227
}
239228
}

src/BenchmarkDotNet/Engines/ThreadingStats.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,6 @@ private static Func<long> CreateGetterDelegate(Type type, string propertyName)
8686

8787
public override bool Equals(object obj) => obj is ThreadingStats other && Equals(other);
8888

89-
public override int GetHashCode()
90-
{
91-
unchecked
92-
{
93-
int hashCode = CompletedWorkItemCount.GetHashCode();
94-
hashCode = (hashCode * 397) ^ LockContentionCount.GetHashCode();
95-
hashCode = (hashCode * 397) ^ TotalOperations.GetHashCode();
96-
return hashCode;
97-
}
98-
}
89+
public override int GetHashCode() => HashCode.Combine(CompletedWorkItemCount, LockContentionCount, TotalOperations);
9990
}
10091
}

src/BenchmarkDotNet/Environments/Runtimes/ClrRuntime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static ClrRuntime CreateForLocalFullNetFrameworkBuild(string version)
4040

4141
public bool Equals(ClrRuntime other) => other != null && base.Equals(other) && Version == other.Version;
4242

43-
public override int GetHashCode() => base.GetHashCode() ^ (Version?.GetHashCode() ?? 0);
43+
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), Version);
4444

4545
internal static ClrRuntime GetCurrentVersion()
4646
{

src/BenchmarkDotNet/Environments/Runtimes/MonoAotLLVMRuntime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ public bool Equals(MonoAotLLVMRuntime other)
4444
=> other != null && base.Equals(other) && other.AOTCompilerPath == AOTCompilerPath;
4545

4646
public override int GetHashCode()
47-
=> base.GetHashCode() ^ AOTCompilerPath.GetHashCode();
47+
=> HashCode.Combine(base.GetHashCode(), AOTCompilerPath);
4848
}
4949
}

src/BenchmarkDotNet/Environments/Runtimes/MonoRuntime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ public bool Equals(MonoRuntime other)
3232
=> base.Equals(other) && Name == other?.Name && CustomPath == other?.CustomPath && AotArgs == other?.AotArgs && MonoBclPath == other?.MonoBclPath;
3333

3434
public override int GetHashCode()
35-
=> base.GetHashCode() ^ Name.GetHashCode() ^ (CustomPath?.GetHashCode() ?? 0) ^ (AotArgs?.GetHashCode() ?? 0) ^ (MonoBclPath?.GetHashCode() ?? 0);
35+
=> HashCode.Combine(base.GetHashCode(), Name, CustomPath, AotArgs, MonoBclPath);
3636
}
3737
}

src/BenchmarkDotNet/Environments/Runtimes/Runtime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public bool Equals(Runtime other)
4141

4242
public override bool Equals(object obj) => obj is Runtime other && Equals(other);
4343

44-
public override int GetHashCode() => Name.GetHashCode() ^ (int)RuntimeMoniker ^ MsBuildMoniker.GetHashCode();
44+
public override int GetHashCode() => HashCode.Combine(Name, RuntimeMoniker, MsBuildMoniker);
4545
}
4646
}

src/BenchmarkDotNet/Environments/Runtimes/WasmRuntime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ public bool Equals(WasmRuntime other)
4646
=> other != null && base.Equals(other) && other.JavaScriptEngine == JavaScriptEngine && other.JavaScriptEngineArguments == JavaScriptEngineArguments && other.Aot == Aot;
4747

4848
public override int GetHashCode()
49-
=> base.GetHashCode() ^ (JavaScriptEngine?.GetHashCode() ?? 0) ^ (JavaScriptEngineArguments?.GetHashCode() ?? 0 ^ Aot.GetHashCode());
49+
=> HashCode.Combine(base.GetHashCode(), JavaScriptEngine, JavaScriptEngineArguments, Aot);
5050
}
5151
}

src/BenchmarkDotNet/Jobs/EnvironmentVariable.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ public override bool Equals(object obj)
3333
return Equals((EnvironmentVariable) obj);
3434
}
3535

36-
public override int GetHashCode()
37-
{
38-
unchecked
39-
{
40-
return (Key.GetHashCode() * 397) ^ Value.GetHashCode();
41-
}
42-
}
36+
public override int GetHashCode() => HashCode.Combine(Key, Value);
4337
}
4438
}

0 commit comments

Comments
 (0)