Skip to content

Commit

Permalink
improved performance of XxHash3
Browse files Browse the repository at this point in the history
  • Loading branch information
Wsm2110 committed Nov 1, 2024
1 parent 92a5788 commit 5cb2de7
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 29 deletions.
4 changes: 2 additions & 2 deletions benchmarks/Faster.Map.Benchmark/StringBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class StringBenchmark

#region Properties

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

#endregion
Expand All @@ -51,7 +51,7 @@ public void Setup()
uint length = BitOperations.RoundUpToPowerOf2(Length) * 2;
int dicLength = HashHelpers.GetPrime((int)Length);

_denseMap = new DenseMap<string, string>(length, 0875, new StringHasher());
_denseMap = new DenseMap<string, string>(length, 0.875, new XxHash3StringHasher());
_dictionary = new Dictionary<string, string>(dicLength);
_robinhoodMap = new RobinhoodMap<string, string>(length);

Expand Down
36 changes: 18 additions & 18 deletions benchmarks/Faster.Map.Benchmark/StringWrapperBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class StringWrapperBenchmark

#region Properties

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

#endregion
Expand Down Expand Up @@ -71,23 +71,23 @@ public void DenseMap()
}
}

[Benchmark]
public void RobinhoodMap()
{
foreach (var key in keys)
{
_robinhoodMap.Get(key, out var result);
}
}

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

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

}
}
1 change: 0 additions & 1 deletion src/DenseMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#if NET7_0_OR_GREATER

using Faster.Map.Contracts;
using Faster.Map.Core;
using Faster.Map.Hasher;
using System;
using System.Collections.Generic;
Expand Down
4 changes: 2 additions & 2 deletions src/Hasher/XxHash3Hasher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class XxHash3Hasher<T> : IHasher<T> where T : unmanaged
public uint ComputeHash(T key)
{
var span = MemoryMarshal.AsBytes(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(key), 1));
var result = XxHash3.Hash(MemoryMarshal.AsBytes(span));
return Unsafe.ReadUnaligned<uint>(ref result[0]);
var result = XxHash3.HashToUInt64(MemoryMarshal.AsBytes(span)) >> 32;
return Unsafe.As<ulong, uint>(ref result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

namespace Faster.Map.Hasher
{
public class StringHasher : IHasher<string>
public class XxHash3StringHasher : IHasher<string>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint ComputeHash(string key)
{
var span = key.AsSpan();
var result = XxHash3.Hash(MemoryMarshal.AsBytes(span));
return Unsafe.ReadUnaligned<uint>(ref result[0]);
var result = XxHash3.HashToUInt64(MemoryMarshal.AsBytes(key.AsSpan())) >> 32;
return Unsafe.As<ulong, uint>(ref result);
}
}
}
4 changes: 2 additions & 2 deletions unittests/Faster.Map.DenseMap.Tests/StringHasherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class StringHasherTests

public StringHasherTests()
{
_map = new DenseMap<string, string>(16, 0.875, new StringHasher());
_map = new DenseMap<string, string>(16, 0.875, new XxHash3StringHasher());
}

[Fact]
Expand Down Expand Up @@ -170,7 +170,7 @@ public void Indexer_Set_UpdatesValueForExistingStringKey()
[Fact]
public void LoadFactorLimit_DoesNotExceedForStringKeys()
{
var result = new DenseMap<string, string>(4, 0.75, new StringHasher());
var result = new DenseMap<string, string>(4, 0.75, new XxHash3StringHasher());

result.Emplace("key1", "value1");
result.Emplace("key2", "value2");
Expand Down

0 comments on commit 5cb2de7

Please sign in to comment.