-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
591 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Faster.Map.Hash.Benchmark | ||
{ | ||
public class Benchmark | ||
{ | ||
private FastHash _hash = new FastHash(); | ||
private string source; | ||
|
||
[Params(10, 16, 32, 48, 100)] | ||
public uint StringLength { get; set; } | ||
|
||
[Params(10, 100, 1000, 10000)] | ||
public uint Iterations { get; set; } | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
FastHash.CreateSeed(); | ||
|
||
var data = Enumerable.Range(0, (int)StringLength).Select(i => (char)i).ToArray(); | ||
|
||
source = new string(data); | ||
} | ||
|
||
[Benchmark] | ||
public void RunFastHash() | ||
{ | ||
for (int i = 0; i < Iterations; ++i) | ||
{ | ||
FastHash.HashU64(MemoryMarshal.AsBytes(source.AsSpan())); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void RunMarvinHash() | ||
{ | ||
for (int i = 0; i < Iterations; ++i) | ||
{ | ||
source.GetHashCode(); | ||
} | ||
} | ||
|
||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
Faster.Map.Hash.Benchmark/Faster.Map.Hash.Benchmark.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" /> | ||
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.14.0" /> | ||
<PackageReference Include="ObjectLayoutInspector" Version="0.1.4" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\src\Faster.Map.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
using BenchmarkDotNet.Configs; | ||
using BenchmarkDotNet.Running; | ||
using Faster.Map.Hash.Benchmark; | ||
|
||
BenchmarkRunner.Run<Benchmark>(new DebugInProcessConfig()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
using Xunit; | ||
|
||
namespace Faster.Map.Hash; | ||
public class FastHashLargeStringTests | ||
{ | ||
private const int LargeSize = 10_000_000; // 10 million characters | ||
|
||
|
||
[Fact] | ||
public void HashU64_LargeString_AllSameCharacters() | ||
{ | ||
// Arrange | ||
string input = new string('A', LargeSize); | ||
ReadOnlySpan<byte> bytes = Encoding.UTF8.GetBytes(input); | ||
|
||
// Act | ||
ulong hash = FastHash.HashU64(bytes); | ||
|
||
// Assert | ||
Assert.NotEqual(0UL, hash); | ||
} | ||
|
||
[Fact] | ||
public void HashU64_LargeString_RepeatingPattern() | ||
{ | ||
// Arrange | ||
string pattern = "1234567890"; | ||
int repeatCount = LargeSize / pattern.Length; | ||
string input = string.Concat(Enumerable.Repeat(pattern, repeatCount)); | ||
ReadOnlySpan<byte> bytes = Encoding.UTF8.GetBytes(input); | ||
|
||
// Act | ||
ulong hash = FastHash.HashU64(bytes); | ||
|
||
// Assert | ||
Assert.NotEqual(0UL, hash); | ||
} | ||
|
||
[Fact] | ||
public void HashU64_LargeString_RandomCharacters() | ||
{ | ||
// Arrange | ||
string input = GenerateRandomString(LargeSize); | ||
ReadOnlySpan<byte> bytes = Encoding.UTF8.GetBytes(input); | ||
|
||
// Act | ||
ulong hash = FastHash.HashU64(bytes); | ||
|
||
// Assert | ||
Assert.NotEqual(0UL, hash); | ||
} | ||
|
||
[Fact] | ||
public void HashU64Secure_LargeString_AllSameCharacters() | ||
{ | ||
// Arrange | ||
string input = new string('Z', LargeSize); | ||
ReadOnlySpan<byte> bytes = Encoding.UTF8.GetBytes(input); | ||
|
||
// Act | ||
ulong hash = FastHash.HashU64Secure(bytes); | ||
|
||
// Assert | ||
Assert.NotEqual(0UL, hash); | ||
} | ||
|
||
[Fact] | ||
public void HashU64Secure_LargeString_RepeatingPattern() | ||
{ | ||
// Arrange | ||
string pattern = "abcdefghij"; | ||
int repeatCount = LargeSize / pattern.Length; | ||
string input = string.Concat(Enumerable.Repeat(pattern, repeatCount)); | ||
ReadOnlySpan<byte> bytes = Encoding.UTF8.GetBytes(input); | ||
|
||
// Act | ||
ulong hash = FastHash.HashU64Secure(bytes); | ||
|
||
// Assert | ||
Assert.NotEqual(0UL, hash); | ||
} | ||
|
||
[Fact] | ||
public void HashU64Secure_LargeString_RandomCharacters() | ||
{ | ||
// Arrange | ||
string input = GenerateRandomString(LargeSize); | ||
ReadOnlySpan<byte> bytes = Encoding.UTF8.GetBytes(input); | ||
|
||
// Act | ||
ulong hash = FastHash.HashU64Secure(bytes); | ||
|
||
// Assert | ||
Assert.NotEqual(0UL, hash); | ||
} | ||
|
||
[Fact] | ||
public void HashU64_LargeString_DifferentStrings_GenerateDifferentHashes() | ||
{ | ||
// Arrange | ||
string input1 = new string('X', LargeSize); | ||
string input2 = new string('Y', LargeSize); | ||
ReadOnlySpan<byte> bytes1 = Encoding.UTF8.GetBytes(input1); | ||
ReadOnlySpan<byte> bytes2 = Encoding.UTF8.GetBytes(input2); | ||
|
||
// Act | ||
ulong hash1 = FastHash.HashU64(bytes1); | ||
ulong hash2 = FastHash.HashU64(bytes2); | ||
|
||
// Assert | ||
Assert.NotEqual(hash1, hash2); | ||
} | ||
|
||
[Fact] | ||
public void HashU64Secure_LargeString_DifferentStrings_GenerateDifferentHashes() | ||
{ | ||
// Arrange | ||
string input1 = GenerateRandomString(LargeSize); | ||
string input2 = GenerateRandomString(LargeSize); | ||
ReadOnlySpan<byte> bytes1 = Encoding.UTF8.GetBytes(input1); | ||
ReadOnlySpan<byte> bytes2 = Encoding.UTF8.GetBytes(input2); | ||
|
||
// Act | ||
ulong hash1 = FastHash.HashU64Secure(bytes1); | ||
ulong hash2 = FastHash.HashU64Secure(bytes2); | ||
|
||
// Assert | ||
Assert.NotEqual(hash1, hash2); | ||
} | ||
|
||
[Fact] | ||
public void HashU64_LargeString_SubstringHashesDiffer() | ||
{ | ||
// Arrange | ||
string input = GenerateRandomString(LargeSize); | ||
ReadOnlySpan<byte> fullBytes = Encoding.UTF8.GetBytes(input); | ||
ReadOnlySpan<byte> substringBytes = Encoding.UTF8.GetBytes(input.Substring(0, LargeSize / 2)); | ||
|
||
// Act | ||
ulong fullHash = FastHash.HashU64(fullBytes); | ||
ulong substringHash = FastHash.HashU64(substringBytes); | ||
|
||
// Assert | ||
Assert.NotEqual(fullHash, substringHash); | ||
} | ||
|
||
[Fact] | ||
public void HashU64Secure_LargeString_SubstringHashesDiffer() | ||
{ | ||
// Arrange | ||
string input = new string('A', LargeSize); | ||
ReadOnlySpan<byte> fullBytes = Encoding.UTF8.GetBytes(input); | ||
ReadOnlySpan<byte> substringBytes = Encoding.UTF8.GetBytes(input.Substring(0, LargeSize / 2)); | ||
|
||
// Act | ||
ulong fullHash = FastHash.HashU64Secure(fullBytes); | ||
ulong substringHash = FastHash.HashU64Secure(substringBytes); | ||
|
||
// Assert | ||
Assert.NotEqual(fullHash, substringHash); | ||
} | ||
|
||
private string GenerateRandomString(int length) | ||
{ | ||
Random random = new Random(); | ||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
char[] result = new char[length]; | ||
|
||
for (int i = 0; i < length; i++) | ||
{ | ||
result[i] = chars[random.Next(chars.Length)]; | ||
} | ||
|
||
return new string(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" /> | ||
<PackageReference Include="xunit" Version="2.9.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\src\Faster.Map.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.