Skip to content

Commit

Permalink
Merge pull request #31 from Nexus-Mods/fix-hashcode-equals
Browse files Browse the repository at this point in the history
Fixed HashCode not being Case Insensitive, Improved HashCode Performance & Use SIMD/Intrinsics Code from External Library (Fixes #25)
  • Loading branch information
halgari authored Nov 23, 2023
2 parents 83b1d4a + 11c9905 commit 9aff411
Show file tree
Hide file tree
Showing 30 changed files with 77 additions and 1,751 deletions.
67 changes: 0 additions & 67 deletions src/NexusMods.Paths.Benchmarks/Benchmarks/ChangeCase.cs

This file was deleted.

This file was deleted.

125 changes: 0 additions & 125 deletions src/NexusMods.Paths.Benchmarks/Benchmarks/VectorisedStringHash.cs

This file was deleted.

14 changes: 8 additions & 6 deletions src/NexusMods.Paths/AbsolutePath.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using NexusMods.Paths.Extensions;
using NexusMods.Paths.Utilities;
using Reloaded.Memory.Extensions;

[assembly: InternalsVisibleTo("NexusMods.Paths.Tests")]
namespace NexusMods.Paths;
Expand Down Expand Up @@ -327,8 +326,9 @@ public override string ToString()
/// <inheritdoc />
public bool Equals(AbsolutePath other)
{
return string.Equals(Directory, other.Directory, StringComparison.OrdinalIgnoreCase) &&
string.Equals(FileName, other.FileName, StringComparison.OrdinalIgnoreCase);
// Do not reorder, FileName is statistically more likely to mismatch than Directory - (Sewer)
return string.Equals(FileName, other.FileName, StringComparison.OrdinalIgnoreCase) &&
string.Equals(Directory, other.Directory, StringComparison.OrdinalIgnoreCase);
}

/// <inheritdoc />
Expand All @@ -340,8 +340,10 @@ public override bool Equals(object? obj)
/// <inheritdoc />
public override int GetHashCode()
{
var a = string.GetHashCode(Directory, StringComparison.OrdinalIgnoreCase);
var b = string.GetHashCode(FileName, StringComparison.OrdinalIgnoreCase);
// A custom HashCode, based on FNV-1 with added Vectorization because the default one is very slow for our use in trees, dictionaries, etc.
// .NET does have a faster hashcode for strings, however it is not exposed (and is 5x slower than custom one anyways).
var a = Directory.GetHashCodeLowerFast();
var b = FileName.GetHashCodeLowerFast();
return HashCode.Combine(a, b);
}
#endregion
Expand Down
Loading

0 comments on commit 9aff411

Please sign in to comment.