From 98212c56171c250967239d445ec299041e20a375 Mon Sep 17 00:00:00 2001 From: MrGadget1024 <9826063+MrGadget1024@users.noreply.github.com> Date: Mon, 13 Feb 2023 04:44:56 -0500 Subject: [PATCH 1/2] RunUnityTests - unityVersion 2022.2.6f1 --- .github/workflows/RunUnityTests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/RunUnityTests.yml b/.github/workflows/RunUnityTests.yml index 8d469bac2..44911ff96 100644 --- a/.github/workflows/RunUnityTests.yml +++ b/.github/workflows/RunUnityTests.yml @@ -14,7 +14,7 @@ jobs: - 2019.4.40f1 - 2020.3.44f1 - 2021.3.18f1 - - 2022.2.5f1 + - 2022.2.6f1 steps: - name: Checkout repository From 59dc88c981b1612cbf62a2a4f739d6475e1bb338 Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Mon, 13 Feb 2023 20:45:36 -0500 Subject: [PATCH 2/2] perf: Cache Stable Hashes (#3377) * perf: Cache Stable Hashes - Static dictionary of message hashes - ResetStatics * Added debug log for future debugging. * Update Extensions.cs --------- Co-authored-by: mischa <16416509+vis2k@users.noreply.github.com> --- Assets/Mirror/Core/Tools/Extensions.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Assets/Mirror/Core/Tools/Extensions.cs b/Assets/Mirror/Core/Tools/Extensions.cs index 3654e4b8c..28c91e560 100644 --- a/Assets/Mirror/Core/Tools/Extensions.cs +++ b/Assets/Mirror/Core/Tools/Extensions.cs @@ -9,15 +9,33 @@ public static class Extensions public static string ToHexString(this ArraySegment segment) => BitConverter.ToString(segment.Array, segment.Offset, segment.Count); + // GetStableHashCode is O(N). + // the longer the string, the more it needs to compute: + // https://github.com/MirrorNetworking/Mirror/pull/3377 + // cache results for O(1) lookups. + static readonly Dictionary StableHashes = new Dictionary(); + + [UnityEngine.RuntimeInitializeOnLoadMethod] + public static void ResetStatics() + { + StableHashes.Clear(); + } + // string.GetHashCode is not guaranteed to be the same on all machines, but // we need one that is the same on all machines. simple and stupid: public static int GetStableHashCode(this string text) { + if (StableHashes.TryGetValue(text, out int cachedHash)) + return cachedHash; + unchecked { int hash = 23; foreach (char c in text) hash = hash * 31 + c; + + //UnityEngine.Debug.Log($"Caching stable hash {(ushort)hash} for {text}"); + StableHashes[text] = hash; return hash; } }