Implements some micro-optimizations
All checks were successful
Build / Build (push) Successful in 51s
All checks were successful
Build / Build (push) Successful in 51s
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace PkmnLib.Static.Utils;
|
||||
@@ -8,9 +9,12 @@ namespace PkmnLib.Static.Utils;
|
||||
/// <remarks>
|
||||
/// This is a struct, as it's effectively just a wrapper around a single reference object. Heap allocation would be silly.
|
||||
/// </remarks>
|
||||
public readonly record struct StringKey
|
||||
public readonly struct StringKey : IEquatable<StringKey>, IEquatable<string>
|
||||
{
|
||||
private static readonly ConcurrentDictionary<string, int> HashCodes = new();
|
||||
|
||||
private readonly string _key;
|
||||
private readonly int _hashCode;
|
||||
|
||||
/// <inheritdoc cref="StringKey"/>
|
||||
public StringKey(string key)
|
||||
@@ -18,6 +22,10 @@ public readonly record struct StringKey
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
throw new ArgumentException("Key cannot be null or whitespace.", nameof(key));
|
||||
_key = key;
|
||||
if (!HashCodes.TryGetValue(_key, out _hashCode))
|
||||
{
|
||||
_hashCode = HashCodes[_key] = StringComparer.InvariantCultureIgnoreCase.GetHashCode(_key);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -45,19 +53,24 @@ public readonly record struct StringKey
|
||||
public override string ToString() => _key.ToLowerInvariant();
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool Equals(StringKey other) => string.Equals(_key, other._key, StringComparison.InvariantCultureIgnoreCase);
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return obj switch
|
||||
{
|
||||
StringKey other => Equals(other),
|
||||
string str => Equals(str),
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool Equals(StringKey other) => _hashCode == other._hashCode;
|
||||
|
||||
/// <inheritdoc cref="Equals(StringKey)"/>
|
||||
public bool Equals(string other) => string.Equals(_key, other, StringComparison.InvariantCultureIgnoreCase);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int GetHashCode() => StringComparer.InvariantCultureIgnoreCase.GetHashCode(_key);
|
||||
|
||||
/// <inheritdoc cref="Equals(StringKey)"/>
|
||||
public static bool operator ==(StringKey left, string right) => left.Equals(right);
|
||||
|
||||
/// <inheritdoc cref="Equals(StringKey)"/>
|
||||
public static bool operator !=(StringKey left, string right) => !(left == right);
|
||||
public override int GetHashCode() => _hashCode;
|
||||
|
||||
/// <inheritdoc cref="Equals(StringKey)"/>
|
||||
public static bool operator ==(StringKey? left, string? right) =>
|
||||
|
||||
Reference in New Issue
Block a user