Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

HashCode based on xxHash32 #25013

Merged
merged 11 commits into from
Dec 13, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3675,6 +3675,51 @@ public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, S
public void SetTarget(T target) { }
public bool TryGetTarget(out T target) { throw null; }
}

public partial struct HashCode
{
[System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static int Combine<T1>(T1 value1) { throw null; }

[System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static int Combine<T1, T2>(T1 value1, T2 value2) { throw null; }

[System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static int Combine<T1, T2, T3>(T1 value1, T2 value2, T3 value3) { throw null; }

[System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static int Combine<T1, T2, T3, T4>(T1 value1, T2 value2, T3 value3, T4 value4) { throw null; }

[System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static int Combine<T1, T2, T3, T4, T5>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5) { throw null; }

[System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static int Combine<T1, T2, T3, T4, T5, T6>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6) { throw null; }

[System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static int Combine<T1, T2, T3, T4, T5, T6, T7>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7) { throw null; }

[System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static int Combine<T1, T2, T3, T4, T5, T6, T7, T8>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8) { throw null; }

[System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public void Add<T>(T value) { }

[System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public void Add<T>(T value, System.Collections.Generic.IEqualityComparer<T> comparer) { }

[System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public int ToHashCode() { throw null; }

# pragma warning disable 0809

[System.ObsoleteAttribute("Use ToHashCode to retrieve the computed hash code.", error: true)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be the same message as in coreclr.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkotas is this addition even required now that HashCode lives in CLR?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you do need these since these copies are the ones most compilers will see.

[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public override int GetHashCode() { throw null; }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about Equals?

# pragma warning restore 0809

}
}

namespace System.Runtime.ConstrainedExecution
Expand Down
209 changes: 209 additions & 0 deletions src/System.Runtime/tests/Performance/Perf.HashCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.CompilerServices;
using Microsoft.Xunit.Performance;

namespace System.Tests
{
public class Perf_HashCode
{
private static volatile int _valueStorage;

// Prevents the jitter from eliminating code that
// we want to test.

[MethodImpl(MethodImplOptions.NoInlining)]
private static void DontDiscard(int value)
{
_valueStorage = value;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static int DontFold(int value)
{
return value + _valueStorage;
}

[Benchmark]
public void Add()
{
foreach (BenchmarkIteration iteration in Benchmark.Iterations)
{
using (iteration.StartMeasurement())
{
for (int i = 0; i < 100; i++)
{
var hc = new HashCode();
for (int j = 0; j < 100; j++)
{
hc.Add(i); hc.Add(i); hc.Add(i);
hc.Add(i); hc.Add(i); hc.Add(i);
hc.Add(i); hc.Add(i); hc.Add(i);
}
DontDiscard(hc.ToHashCode());
}
}
}
}


[Benchmark]
public void Combine_1()
{
foreach (BenchmarkIteration iteration in Benchmark.Iterations)
{
using (iteration.StartMeasurement())
{
for (int i = 0; i < 10000; i++)
{
DontDiscard(HashCode.Combine(
DontFold(i)));
}
}
}
}


[Benchmark]
public void Combine_2()
{
foreach (BenchmarkIteration iteration in Benchmark.Iterations)
{
using (iteration.StartMeasurement())
{
for (int i = 0; i < 10000; i++)
{
DontDiscard(HashCode.Combine(
DontFold(i),
DontFold(i)));
}
}
}
}

[Benchmark]
public void Combine_3()
{
foreach (BenchmarkIteration iteration in Benchmark.Iterations)
{
using (iteration.StartMeasurement())
{
for (int i = 0; i < 10000; i++)
{
DontDiscard(HashCode.Combine(
DontFold(i),
DontFold(i),
DontFold(i)));
}
}
}
}

[Benchmark]
public void Combine_4()
{
foreach (BenchmarkIteration iteration in Benchmark.Iterations)
{
using (iteration.StartMeasurement())
{
for (int i = 0; i < 10000; i++)
{
DontDiscard(HashCode.Combine(
DontFold(i),
DontFold(i),
DontFold(i),
DontFold(i)));
}
}
}
}

[Benchmark]
public void Combine_5()
{
foreach (BenchmarkIteration iteration in Benchmark.Iterations)
{
using (iteration.StartMeasurement())
{
for (int i = 0; i < 10000; i++)
{
DontDiscard(HashCode.Combine(
DontFold(i),
DontFold(i),
DontFold(i),
DontFold(i),
DontFold(i)));
}
}
}
}

[Benchmark]
public void Combine_6()
{
foreach (BenchmarkIteration iteration in Benchmark.Iterations)
{
using (iteration.StartMeasurement())
{
for (int i = 0; i < 10000; i++)
{
DontDiscard(HashCode.Combine(
DontFold(i),
DontFold(i),
DontFold(i),
DontFold(i),
DontFold(i),
DontFold(i)));
}
}
}
}

[Benchmark]
public void Combine_7()
{
foreach (BenchmarkIteration iteration in Benchmark.Iterations)
{
using (iteration.StartMeasurement())
{
for (int i = 0; i < 10000; i++)
{
DontDiscard(HashCode.Combine(
DontFold(i),
DontFold(i),
DontFold(i),
DontFold(i),
DontFold(i),
DontFold(i),
DontFold(i)));
}
}
}
}

[Benchmark]
public void Combine_8()
{
foreach (BenchmarkIteration iteration in Benchmark.Iterations)
{
using (iteration.StartMeasurement())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'd prefer braces for the foreach here.

{
for (int i = 0; i < 10000; i++)
{
DontDiscard(HashCode.Combine(
DontFold(i),
DontFold(i),
DontFold(i),
DontFold(i),
DontFold(i),
DontFold(i),
DontFold(i),
DontFold(i)));
}
}
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to have benchmarks for a couple of the Combine methods since those are hopefully the ones that get used most and you've already done some performance tuning on them. I don't think you need every combine, but maybe Combine with 2, 4 and 8 inputs.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<Compile Include="Perf.Double.cs" />
<Compile Include="Perf.Enum.cs" />
<Compile Include="Perf.Guid.cs" />
<Compile Include="Perf.HashCode.cs" />
<Compile Include="Perf.Object.cs" />
<Compile Include="Perf.String.cs" />
<Compile Include="Perf.TimeSpan.cs" />
Expand Down
3 changes: 2 additions & 1 deletion src/System.Runtime/tests/System.Runtime.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetGroup)' == 'netcoreapp' or '$(TargetGroup)' == 'uapaot'">
<Compile Include="System\HashCodeTests.netcoreapp.cs" />
<Compile Include="Helpers.netcoreapp.cs" />
<Compile Include="System\ActivatorTests.netcoreapp.cs" />
<Compile Include="System\ArrayTests.netcoreapp.cs" />
Expand Down Expand Up @@ -264,4 +265,4 @@
<EmbeddedResource Include="Resources\$(AssemblyName).rd.xml" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
</Project>
Loading