Skip to content

Commit

Permalink
Changed IHasher ComputeHash to ulong
Browse files Browse the repository at this point in the history
  • Loading branch information
Wsm2110 committed Nov 12, 2024
1 parent 5cb2de7 commit 8ce3c3e
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 198 deletions.
45 changes: 22 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,26 +228,25 @@ BenchmarkDotNet v0.13.12, Windows 11 (10.0.22631.3880/23H2/2023Update/SunValley3
| RobinhoodMap | 1000000 | 84.237352 ms | 1.6354160 ms | 2.2926251 ms | 123 B |
| Dictionary | 1000000 | 45.689701 ms | 0.9896159 ms | 2.8867563 ms | 67 B |

### Create StringWrapperBenchmark (cached hashcode)

| Method | Length | Mean | Error | StdDev | Allocated |
|--------------|---------|----------------|---------------|---------------|-----------|
| DenseMap | 1000 | 0.005590 ms | 0.0000622 ms | 0.0000582 ms | - |
| RobinhoodMap | 1000 | 0.004822 ms | 0.0000862 ms | 0.0000807 ms | - |
| Dictionary | 1000 | 0.006721 ms | 0.0001277 ms | 0.0001311 ms | - |
| DenseMap | 10000 | 0.072046 ms | 0.0005074 ms | 0.0004237 ms | - |
| RobinhoodMap | 10000 | 0.071678 ms | 0.0010047 ms | 0.0008390 ms | - |
| Dictionary | 10000 | 0.134088 ms | 0.0004288 ms | 0.0004011 ms | - |
| DenseMap | 100000 | 1.111280 ms | 0.0174223 ms | 0.0154444 ms | 1 B |
| RobinhoodMap | 100000 | 1.359501 ms | 0.0153216 ms | 0.0143318 ms | 1 B |
| Dictionary | 100000 | 1.866555 ms | 0.0053967 ms | 0.0045064 ms | 1 B |
| DenseMap | 400000 | 13.668025 ms | 0.0525273 ms | 0.0465641 ms | 12 B |
| RobinhoodMap | 400000 | 13.020727 ms | 0.0614468 ms | 0.0479736 ms | 12 B |
| Dictionary | 400000 | 10.846306 ms | 0.0903438 ms | 0.0800874 ms | 12 B |
| DenseMap | 900000 | 34.296921 ms | 0.1181645 ms | 0.0986727 ms | 49 B |
| RobinhoodMap | 900000 | 33.615793 ms | 0.4644336 ms | 0.4344315 ms | 49 B |
| Dictionary | 900000 | 41.753783 ms | 0.8050352 ms | 0.7906522 ms | 4 B |
| DenseMap | 1000000 | 40.145635 ms | 0.7456945 ms | 0.6975231 ms | 57 B |
| RobinhoodMap | 1000000 | 41.060782 ms | 0.5800290 ms | 0.5141806 ms | 5 B |
| Dictionary | 1000000 | 40.949160 ms | 0.3808110 ms | 0.3179945 ms | 5 B |

### Get string benchmark using XXHash3StringHasher

| Method | Length | Mean | Error | StdDev | Allocated |
|------------- |-------- |-------------:|-------------:|-------------:|----------:|
| DenseMap | 1000 | 0.0061 ms | 0.000052 ms | 0.000044 ms | - |
| RobinhoodMap | 1000 | 0.0082 ms | 0.000113 ms | 0.000106 ms | - |
| Dictionary | 1000 | 0.0075 ms | 0.000057 ms | 0.000047 ms | - |
| DenseMap | 10000 | 0.0655 ms | 0.000178 ms | 0.000158 ms | - |
| RobinhoodMap | 10000 | 0.1136 ms | 0.001130 ms | 0.001057 ms | - |
| Dictionary | 10000 | 0.1455 ms | 0.000811 ms | 0.000677 ms | - |
| DenseMap | 100000 | 0.9943 ms | 0.0176 ms | 0.0165 ms | 1 B |
| RobinhoodMap | 100000 | 1.8320 ms | 0.0049 ms | 0.0046 ms | 1 B |
| Dictionary | 100000 | 1.8777 ms | 0.0034 ms | 0.0032 ms | 3 B |
| DenseMap | 400000 | 10.3705 ms | 0.0636 ms | 0.0563 ms | 12 B |
| RobinhoodMap | 400000 | 17.7692 ms | 0.1609 ms | 0.1426 ms | 23 B |
| Dictionary | 400000 | 9.3516 ms | 0.1836 ms | 0.1964 ms | 12 B |
| DenseMap | 900000 | 32.7895 ms | 0.5783 ms | 0.5410 ms | 46 B |
| RobinhoodMap | 900000 | 56.5273 ms | 0.6319 ms | 0.5602 ms | 82 B |
| Dictionary | 900000 | 42.8022 ms | 1.2363 ms | 3.5670 ms | 49 B |
| DenseMap | 1000000 | 35.2224 ms | 0.5863 ms | 0.5484 ms | 57 B |
| RobinhoodMap | 1000000 | 68.3534 ms | 1.1398 ms | 1.0661 ms | 92 B |
| Dictionary | 1000000 | 42.9991 ms | 0.4173 ms | 0.3699 ms | 5 B |
17 changes: 10 additions & 7 deletions benchmarks/Faster.Map.Benchmark/AddBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class AddBenchmark

#region Properties

[Params(1000, 10000, 100000, 400000, 900000, 1000000)]
[Params(10000, 100000, 400000, 800000, 900000)]
public uint Length { get; set; }

#endregion
Expand All @@ -52,7 +52,7 @@ public void Add()
public void Setup()
{
// round of length to power of 2 prevent resizing
uint length = BitOperations.RoundUpToPowerOf2(Length) * 2;
uint length = BitOperations.RoundUpToPowerOf2(Length);
int dicLength = HashHelpers.GetPrime((int)Length);

_dense = new DenseMap<uint, uint>(length);
Expand All @@ -65,26 +65,29 @@ public void Setup()
[Benchmark]
public void DenseMap()
{
foreach (var key in keys.Take((int)Length))
for (int i = 0; i < Length; i++)
{
var key = keys[i];
_dense.Emplace(key, key);
}
}
}

[Benchmark]
public void RobinhoodMap()
{
foreach (var key in keys.Take((int)Length))
for (int i = 0; i < Length; i++)
{
var key = keys[i];
_robinhoodMap.Emplace(key, key);
}
}
}

[Benchmark]
public void Dictionary()
{
foreach (var key in keys.Take((int)Length))
for (int i = 0; i < Length; i++)
{
var key = keys[i];
dic.Add(key, key);
}
}
Expand Down
38 changes: 19 additions & 19 deletions benchmarks/Faster.Map.Benchmark/GetBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,24 @@ public void DenseMap()
}
}

[Benchmark]
public void RobinhoodMap()
{
for (int i = 0; i < Length; ++i)
{
var key = keys[i];
_robinHoodMap.Get(key, out var _);
}
}

[Benchmark(Baseline = true)]
public void Dictionary()
{
for (int i = 0; i < Length; ++i)
{
var key = keys[i];
_dictionary.TryGetValue(key, out var _);
}
}
//[Benchmark]
//public void RobinhoodMap()
//{
// for (int i = 0; i < Length; ++i)
// {
// var key = keys[i];
// _robinHoodMap.Get(key, out var _);
// }
//}

//[Benchmark(Baseline = true)]
//public void Dictionary()
//{
// for (int i = 0; i < Length; ++i)
// {
// var key = keys[i];
// _dictionary.TryGetValue(key, out var _);
// }
//}
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
using BenchmarkDotNet.Attributes;
using Faster.Map.Core;
using Faster.Map.Hasher;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;

namespace Faster.Map.Benchmark
{
[MarkdownExporterAttribute.GitHub]
[MemoryDiagnoser]
public class StringWrapperBenchmark
public class IntBenchmark
{
#region Fields

private DenseMap<StringWrapper, StringWrapper> _denseMap;
private Dictionary<StringWrapper, StringWrapper> _dictionary;
private RobinhoodMap<StringWrapper, StringWrapper> _robinhoodMap;
private DenseMap<uint, uint> _denseMap;
private Dictionary<uint, uint> _dictionary;
private RobinhoodMap<uint, uint> _robinhoodMap;

private string[] keys;
private uint[] keys;

#endregion

#region Properties

[Params(/*1000, 10000, 100000, 400000, 900000,*/ 1000000)]
[Params(/*10000, 100000, 400000, 900000,*/ 1000000)]
public uint Length { get; set; }

#endregion
Expand All @@ -39,20 +38,20 @@ public void Setup()
var output = File.ReadAllText("Numbers.txt");
var splittedOutput = output.Split(',');

keys = new string[Length];
keys = new uint[Length];

for (var index = 0; index < Length; index++)
{
keys[index] = splittedOutput[index];
keys[index] = Convert.ToUInt32(splittedOutput[index]);
}

// round of length to power of 2 prevent resizing
uint length = BitOperations.RoundUpToPowerOf2(Length) * 2;
uint length = BitOperations.RoundUpToPowerOf2(Length);
int dicLength = HashHelpers.GetPrime((int)Length);

_denseMap = new DenseMap<StringWrapper, StringWrapper>(length);
_dictionary = new Dictionary<StringWrapper, StringWrapper>(dicLength);
_robinhoodMap = new RobinhoodMap<StringWrapper, StringWrapper>(length);
_denseMap = new DenseMap<uint, uint>(length, 0.875, new DefaultHasher<uint>());
_dictionary = new Dictionary<uint, uint>(dicLength);
_robinhoodMap = new RobinhoodMap<uint, uint>(length);

foreach (var key in keys)
{
Expand Down Expand Up @@ -80,14 +79,14 @@ public void DenseMap()
// }
//}

//[Benchmark]
//public void Dictionary()
//{
// foreach (var key in keys)
// {
// _dictionary.TryGetValue(key, out var result);
// }
//}
[Benchmark]
public void Dictionary()
{
foreach (var key in keys)
{
_dictionary.TryGetValue(key, out var result);
}
}

}
}
2 changes: 1 addition & 1 deletion benchmarks/Faster.Map.Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<StringBenchmark>();
BenchmarkRunner.Run<IntBenchmark>(new DebugInProcessConfig());
//BenchmarkRunner.Run<AddAndResizeBenchmark>();
//BenchmarkRunner.Run<UpdateBenchmark>();
//BenchmarkRunner.Run<RemoveBenchmark>();
Expand Down
5 changes: 2 additions & 3 deletions benchmarks/Faster.Map.Benchmark/StringBenchmark.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using BenchmarkDotNet.Attributes;
using Faster.Map.Core;
using Faster.Map.Hasher;
using System;
using System.Collections;
Expand All @@ -26,7 +25,7 @@ public class StringBenchmark

#region Properties

[Params(/*1000, 10000, 100000, 400000, 900000,*/ 1000000)]
[Params(10000, 100000, 400000, 900000, 1000000)]
public uint Length { get; set; }

#endregion
Expand All @@ -48,7 +47,7 @@ public void Setup()
}

// round of length to power of 2 prevent resizing
uint length = BitOperations.RoundUpToPowerOf2(Length) * 2;
uint length = BitOperations.RoundUpToPowerOf2(Length);
int dicLength = HashHelpers.GetPrime((int)Length);

_denseMap = new DenseMap<string, string>(length, 0.875, new XxHash3StringHasher());
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/IHasher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public interface IHasher<in TKey>
{
uint ComputeHash(TKey key);
ulong ComputeHash(TKey key);
}
}
43 changes: 0 additions & 43 deletions src/Core/StringWrapper.cs

This file was deleted.

Loading

0 comments on commit 8ce3c3e

Please sign in to comment.