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:
@@ -107,4 +107,21 @@ public class RandomImpl : IRandom
|
||||
throw new ArgumentException("List cannot be empty.", nameof(list));
|
||||
return list[GetInt(list.Count)];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a new random <see cref="Guid"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The goal of this method is to create a new unique identifier that can be used for various purposes,
|
||||
/// without having to rely on the system's built-in GUID generation. The built-in GUID generation
|
||||
/// can be slow (see also: https://github.com/dotnet/runtime/issues/13628)
|
||||
/// </remarks>
|
||||
public Guid NewGuid()
|
||||
{
|
||||
var guidBytes = GuidCache.Value.AsSpan();
|
||||
_random.NextBytes(guidBytes);
|
||||
return new Guid(guidBytes);
|
||||
}
|
||||
|
||||
private static readonly ThreadLocal<byte[]> GuidCache = new(() => new byte[16]);
|
||||
}
|
||||
@@ -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