Skip to content

Commit

Permalink
Handle PR feedback and switch to xxHash3
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari committed Oct 22, 2024
1 parent fed4b46 commit 065ee2c
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.Hashing.xxHash64;
using NexusMods.Hashing.xxHash3;
using NexusMods.MnemonicDB.TestModel;
using NexusMods.Paths;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Linq;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using NexusMods.Hashing.xxHash64;
using NexusMods.Hashing.xxHash3;
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.MnemonicDB.TestModel;
using NexusMods.Paths;
Expand Down
4 changes: 1 addition & 3 deletions benchmarks/OneBillionDatomsTest/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System;
using System.Diagnostics;
using System.Threading;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NexusMods.MnemonicDB;
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.MnemonicDB.Storage;
using NexusMods.MnemonicDB.TestModel;
using NexusMods.Hashing.xxHash64;
using NexusMods.Hashing.xxHash3;
using NexusMods.Paths;

var host = Host.CreateDefaultBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<ItemGroup>
<PackageReference Include="DynamicData" Version="8.4.1"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1"/>
<PackageReference Include="NexusMods.Hashing.xxHash64" Version="2.0.2" />
<PackageReference Include="NexusMods.Paths" Version="0.9.5" />
<PackageReference Include="R3" Version="1.2.8" />
<PackageReference Include="System.IO.Hashing" Version="8.0.0" />
Expand Down
6 changes: 0 additions & 6 deletions src/NexusMods.MnemonicDB.Abstractions/Serializer.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO.Hashing;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using NexusMods.Hashing.xxHash64;
using NexusMods.MnemonicDB.Abstractions.ElementComparers;
using NexusMods.MnemonicDB.Abstractions.Internals;
using NexusMods.MnemonicDB.Abstractions.ValueSerializers;
using Reloaded.Memory.Extensions;

namespace NexusMods.MnemonicDB.Abstractions;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ public sealed class Tuple2_UShort_Utf8I_Serializer : IValueSerializer<(ushort, s

public static int Compare(ReadOnlySpan<byte> a, ReadOnlySpan<byte> b)
{
throw new NotImplementedException();
var aUShort = MemoryMarshal.Read<ushort>(a);
var bUShort = MemoryMarshal.Read<ushort>(b);
var ushortComparison = aUShort.CompareTo(bUShort);
if (ushortComparison != 0) return ushortComparison;

return Utf8InsensitiveSerializer.Compare(a.SliceFast(sizeof(ushort)), b.SliceFast(sizeof(ushort)));
}

/// <inheritdoc />
Expand All @@ -27,9 +32,7 @@ public static unsafe int Compare(byte* aVal, int aLen, byte* bVal, int bLen)
var ushortComparison = aUShort.CompareTo(bUShort);
if (ushortComparison != 0) return ushortComparison;

var aStr = Encoding.UTF8.GetString(aVal + sizeof(ushort), aLen - sizeof(ushort));
var bStr = Encoding.UTF8.GetString(bVal + sizeof(ushort), bLen - sizeof(ushort));
return string.Compare(aStr, bStr, StringComparison.Ordinal);
return Utf8InsensitiveSerializer.Compare(aVal + sizeof(ushort), aLen - sizeof(ushort), bVal + sizeof(ushort), bLen - sizeof(ushort));
}

/// <inheritdoc />
Expand Down Expand Up @@ -65,9 +68,19 @@ public sealed class Tuple3_Ref_UShort_Utf8I_Serializer : IValueSerializer<(Entit
public static ValueTag ValueTag => ValueTag.Tuple3_Ref_UShort_Utf8I;

/// <inheritdoc />
public static int Compare(ReadOnlySpan<byte> a, ReadOnlySpan<byte> b)
public static unsafe int Compare(ReadOnlySpan<byte> a, ReadOnlySpan<byte> b)
{
throw new NotImplementedException();
var aEntityId = MemoryMarshal.Read<EntityId>(a);
var bEntityId = MemoryMarshal.Read<EntityId>(b);
var entityIdComparison = aEntityId.CompareTo(bEntityId);
if (entityIdComparison != 0) return entityIdComparison;

var aUShort = MemoryMarshal.Read<ushort>(a.SliceFast(sizeof(EntityId)));
var bUShort = MemoryMarshal.Read<ushort>(b.SliceFast(sizeof(EntityId)));
var ushortComparison = aUShort.CompareTo(bUShort);
if (ushortComparison != 0) return ushortComparison;

return Utf8InsensitiveSerializer.Compare(a.SliceFast(sizeof(EntityId) + sizeof(ushort)), b.SliceFast(sizeof(EntityId) + sizeof(ushort)));
}

/// <inheritdoc />
Expand All @@ -83,9 +96,8 @@ public static unsafe int Compare(byte* aVal, int aLen, byte* bVal, int bLen)
var ushortComparison = aUShort.CompareTo(bUShort);
if (ushortComparison != 0) return ushortComparison;

var aStr = Encoding.UTF8.GetString(aVal + sizeof(EntityId) + sizeof(ushort), aLen - sizeof(EntityId) - sizeof(ushort));
var bStr = Encoding.UTF8.GetString(bVal + sizeof(EntityId) + sizeof(ushort), bLen - sizeof(EntityId) - sizeof(ushort));
return string.Compare(aStr, bStr, StringComparison.Ordinal);
var offset = sizeof(EntityId) + sizeof(ushort);
return Utf8InsensitiveSerializer.Compare(aVal + offset, aLen - offset, bVal + offset, bLen - offset);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
 using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.MnemonicDB.Abstractions.ElementComparers;
// AUTOGENERATED CODE - DO NOT MODIFY
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.MnemonicDB.Abstractions.ElementComparers;
namespace NexusMods.MnemonicDB.Abstractions.ValueSerializers;


Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<#@ template language="C#" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.MnemonicDB.Abstractions.ElementComparers;
// AUTOGENERATED CODE - DO NOT MODIFY
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.MnemonicDB.Abstractions.ElementComparers;
namespace NexusMods.MnemonicDB.Abstractions.ValueSerializers;
<#

Expand Down
10 changes: 0 additions & 10 deletions src/NexusMods.MnemonicDB/Storage/DatomStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,6 @@ public DatomStore(
Logger = logger;
_settings = settings;

Backend.DeclareEAVT(IndexType.EAVTCurrent);
Backend.DeclareEAVT(IndexType.EAVTHistory);
Backend.DeclareAEVT(IndexType.AEVTCurrent);
Backend.DeclareAEVT(IndexType.AEVTHistory);
Backend.DeclareVAET(IndexType.VAETCurrent);
Backend.DeclareVAET(IndexType.VAETHistory);
Backend.DeclareAVET(IndexType.AVETCurrent);
Backend.DeclareAVET(IndexType.AVETHistory);
Backend.DeclareTxLog(IndexType.TxLog);

Backend.Init(settings.Path);

if (bootstrap) Bootstrap();
Expand Down
2 changes: 1 addition & 1 deletion tests/NexusMods.MnemonicDB.Storage.Tests/ABackendTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.MnemonicDB.Storage.Abstractions;
using NexusMods.MnemonicDB.TestModel.Helpers;
using NexusMods.Hashing.xxHash64;
using NexusMods.Hashing.xxHash3;
using NexusMods.MnemonicDB.Abstractions.BuiltInEntities;
using NexusMods.MnemonicDB.Abstractions.DatomComparators;
using NexusMods.MnemonicDB.Abstractions.DatomIterators;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using NexusMods.Hashing.xxHash64;
using NexusMods.Hashing.xxHash3;
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.MnemonicDB.Abstractions.Attributes;
using NexusMods.MnemonicDB.Abstractions.ElementComparers;
using NexusMods.MnemonicDB.Abstractions.ValueSerializers;

namespace NexusMods.MnemonicDB.TestModel.Attributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Argon" Version="0.17.0" />
<PackageReference Include="NexusMods.Hashing.xxHash64" Version="2.0.2" />
<PackageReference Include="NexusMods.Hashing.xxHash3" Version="3.0.2" />
<PackageReference Include="Verify.Xunit" Version="25.2.0" />
</ItemGroup>

Expand Down
6 changes: 1 addition & 5 deletions tests/NexusMods.MnemonicDB.Tests/AMnemonicDBTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.MnemonicDB.Abstractions.Models;
using NexusMods.MnemonicDB.Storage;
using NexusMods.MnemonicDB.Storage.RocksDbBackend;
using NexusMods.MnemonicDB.TestModel.Helpers;
using NexusMods.Hashing.xxHash64;
using NexusMods.MnemonicDB.Abstractions.DatomIterators;
using NexusMods.Hashing.xxHash3;
using NexusMods.MnemonicDB.TestModel;
using NexusMods.MnemonicDB.TestModel.Analyzers;
using NexusMods.Paths;
using Xunit.Sdk;
using File = NexusMods.MnemonicDB.TestModel.File;

namespace NexusMods.MnemonicDB.Tests;
Expand Down
12 changes: 7 additions & 5 deletions tests/NexusMods.MnemonicDB.Tests/ComplexModelTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Diagnostics;
using System.Text;
using Microsoft.Extensions.Logging;
using NexusMods.Hashing.xxHash64;
using NexusMods.MnemonicDB.Abstractions.BuiltInEntities;
using NexusMods.Hashing.xxHash3;
using NexusMods.MnemonicDB.TestModel;
using NexusMods.Paths;
using File = NexusMods.MnemonicDB.TestModel.File;
Expand Down Expand Up @@ -52,7 +52,7 @@ public async Task CanCreateLoadout(int modCount, int filesPerMod)
Path = name,
ModId = mod,
Size = Size.FromLong(name.Length),
Hash = Hash.FromLong(name.XxHash64AsUtf8())
Hash = HashAsUtf8(name)
};

files.Add(file);
Expand Down Expand Up @@ -142,7 +142,7 @@ public async Task CanRestartStorage(int modCount, int filesPerMod, int extraFile
Path = name,
ModId = mod,
Size = Size.FromLong(name.Length),
Hash = Hash.FromLong(name.XxHash64AsUtf8())
Hash = HashAsUtf8(name)
};

files.Add(file);
Expand All @@ -164,7 +164,7 @@ public async Task CanRestartStorage(int modCount, int filesPerMod, int extraFile
Path = name,
ModId = firstMod,
Size = Size.FromLong(name.Length),
Hash = Hash.FromLong(name.XxHash64AsUtf8())
Hash = HashAsUtf8(name)
};

files.Add(file);
Expand Down Expand Up @@ -231,4 +231,6 @@ public async Task CanGetFromTransaction()
var remap = result.Remap(archiveFile);
remap.AsFile().Path.Should().Be("bar");
}

private static Hash HashAsUtf8(string value) => Hash.FromLong(Encoding.UTF8.GetBytes(value).xxHash3());
}
3 changes: 1 addition & 2 deletions tests/NexusMods.MnemonicDB.Tests/DbTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System.Diagnostics;
using System.Reactive.Linq;
using System.Security.Cryptography.X509Certificates;
using DynamicData;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.Hashing.xxHash64;
using NexusMods.Hashing.xxHash3;
using NexusMods.MnemonicDB.Abstractions.Attributes;
using NexusMods.MnemonicDB.Abstractions.BuiltInEntities;
using NexusMods.MnemonicDB.Abstractions.DatomIterators;
Expand Down
2 changes: 1 addition & 1 deletion tests/NexusMods.MnemonicDB.Tests/ImportExportTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NexusMods.Hashing.xxHash64;
using NexusMods.Hashing.xxHash3;
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.MnemonicDB.Abstractions.Query;
using NexusMods.MnemonicDB.Storage;
Expand Down
6 changes: 1 addition & 5 deletions tests/NexusMods.MnemonicDB.Tests/MigrationTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System.Net;
using NexusMods.Hashing.xxHash64;
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.Hashing.xxHash3;
using NexusMods.MnemonicDB.Abstractions.Attributes;
using NexusMods.MnemonicDB.Abstractions.IndexSegments;
using NexusMods.MnemonicDB.Abstractions.Query;
using NexusMods.MnemonicDB.TestModel;
using NexusMods.MnemonicDB.TestModel.Attributes;
using NexusMods.Paths;
Expand Down

0 comments on commit 065ee2c

Please sign in to comment.