Skip to content

Commit 28a8e78

Browse files
Simplify GetHashCode() (#2177)
* Add package for netstandart2.0 * Simplify HashCode * Fix a bug: job.Infrastructure.Arguments is an array, so compare its values * Remove outdated suppressor * Revert "Fix a bug: job.Infrastructure.Arguments is an array, so compare its values" This reverts commit 2b5b6b4. * Revert "Simplify HashCode" This reverts commit 681ceab. * Revert "Add package for netstandart2.0" This reverts commit 8f0e2e4. * Mimic System.HashCode * Simplify GetHashCode() * Remove redundant * Fix bugs * Reorder * fix a bug: compare Argument by TextRepresentation, not by reference * Simplify * Add missing HashCode API * Suppress inspection
1 parent e75bdd8 commit 28a8e78

23 files changed

+222
-150
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/Analysers/HideColumnsAnalyser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected override IEnumerable<Conclusion> AnalyseSummary(Summary summary)
2121
var columnNames = string.Join(", ", hiddenColumns.Select(c => c.OriginalColumn.ColumnName));
2222

2323
var message = $"Hidden columns: {columnNames}";
24-
yield return Conclusion.CreateHint(Id, message);
24+
yield return CreateHint(message);
2525
}
2626
}
2727
}

src/BenchmarkDotNet/Columns/SizeUnit.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,10 @@ 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

8273
public static bool operator !=(SizeUnit left, SizeUnit right) => !Equals(left, right);
8374
}
84-
}
75+
}

src/BenchmarkDotNet/Disassemblers/ClrMdV2Disassembler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public bool Equals(Sharp x, Sharp y)
327327
return x.FilePath == y.FilePath && x.LineNumber == y.LineNumber;
328328
}
329329

330-
public int GetHashCode(Sharp obj) => obj.FilePath.GetHashCode() ^ obj.LineNumber;
330+
public int GetHashCode(Sharp obj) => HashCode.Combine(obj.FilePath, obj.LineNumber);
331331
}
332332
}
333-
}
333+
}

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/BenchmarkEnvironmentInfo.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ internal string GetRuntimeInfo()
7070
return $"{RuntimeVersion}, {Architecture} {jitInfo}";
7171
}
7272

73-
[SuppressMessage("ReSharper", "UnusedMember.Global")] // TODO: should be used or removed
7473
public static IEnumerable<ValidationError> Validate(Job job)
7574
{
7675
if (job.Environment.Jit == Jit.RyuJit && !RuntimeInformation.HasRyuJit())

src/BenchmarkDotNet/Environments/Runtimes/ClrRuntime.cs

Lines changed: 2 additions & 2 deletions
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
{
@@ -69,4 +69,4 @@ internal static ClrRuntime GetCurrentVersion()
6969
}
7070
}
7171
}
72-
}
72+
}

src/BenchmarkDotNet/Environments/Runtimes/MonoAotLLVMRuntime.cs

Lines changed: 2 additions & 2 deletions
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
}
49-
}
49+
}

src/BenchmarkDotNet/Environments/Runtimes/MonoRuntime.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public bool Equals(MonoRuntime other)
4141
=> base.Equals(other) && Name == other?.Name && CustomPath == other?.CustomPath && AotArgs == other?.AotArgs && MonoBclPath == other?.MonoBclPath;
4242

4343
public override int GetHashCode()
44-
=> base.GetHashCode() ^ Name.GetHashCode() ^ (CustomPath?.GetHashCode() ?? 0) ^ (AotArgs?.GetHashCode() ?? 0) ^ (MonoBclPath?.GetHashCode() ?? 0);
44+
=> HashCode.Combine(base.GetHashCode(), Name, CustomPath, AotArgs, MonoBclPath);
4545
}
46-
}
46+
}

0 commit comments

Comments
 (0)