From fe27a0577895b0f5e831988ea4485cd2fabff83f Mon Sep 17 00:00:00 2001 From: "COMBINATION\\ano" Date: Wed, 15 Feb 2023 13:38:09 +0100 Subject: [PATCH] Use bytes for hash code generation instead of creating string and calculate hash code from string object --- src/Combination.StringPools/PooledUtf8String.cs | 2 +- src/Combination.StringPools/Utf8StringPool.cs | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Combination.StringPools/PooledUtf8String.cs b/src/Combination.StringPools/PooledUtf8String.cs index 5f878a1..bfaf80f 100644 --- a/src/Combination.StringPools/PooledUtf8String.cs +++ b/src/Combination.StringPools/PooledUtf8String.cs @@ -39,7 +39,7 @@ public override bool Equals(object? obj) } public override int GetHashCode() - => unchecked((int)StringHash.Compute(Utf8StringPool.Get(handle))); + => unchecked((int)StringHash.Compute(Utf8StringPool.GetBytes(handle))); public override string ToString() => Utf8StringPool.Get(handle); diff --git a/src/Combination.StringPools/Utf8StringPool.cs b/src/Combination.StringPools/Utf8StringPool.cs index d486cdf..99f8f75 100644 --- a/src/Combination.StringPools/Utf8StringPool.cs +++ b/src/Combination.StringPools/Utf8StringPool.cs @@ -267,6 +267,16 @@ public static string Get(ulong handle) return string.Empty; } + return Encoding.UTF8.GetString(GetBytes(handle)); + } + + public static ReadOnlySpan GetBytes(ulong handle) + { + if (handle == ulong.MaxValue) + { + return Array.Empty(); + } + var poolIndex = handle >> (64 - PoolIndexBits); if (poolIndex >= (ulong)Pools.Count) { @@ -283,7 +293,7 @@ public static string Get(ulong handle) } [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private string GetFromPool(ulong handle) + private ReadOnlySpan GetFromPool(ulong handle) { using (disposeLock.PreventDispose()) { @@ -296,7 +306,7 @@ private string GetFromPool(ulong handle) throw new InvalidOperationException($"Internal error: Deduplicated string pool mismatch ({index} != {poolIndex})"); } #endif - return Encoding.UTF8.GetString(GetStringBytes(offset)); + return GetStringBytes(offset); } }