From 54cc544eb2c37291c0b482663133f36be0d15712 Mon Sep 17 00:00:00 2001 From: Vladimir Sigalkin Date: Mon, 19 Jul 2021 16:23:46 +0300 Subject: [PATCH] perf(extOSC): OSCUtilities.CompareAddresses optimize --- Assets/extOSC/Scripts/OSCUtilities.cs | 35 +++++++++++++++++++-------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/Assets/extOSC/Scripts/OSCUtilities.cs b/Assets/extOSC/Scripts/OSCUtilities.cs index 4a8d754..9bd1a29 100644 --- a/Assets/extOSC/Scripts/OSCUtilities.cs +++ b/Assets/extOSC/Scripts/OSCUtilities.cs @@ -8,12 +8,13 @@ using System.Collections.Generic; using System.Text.RegularExpressions; using System.Runtime.InteropServices; -using extOSC.Core; #if UNITY_WSA && !UNITY_EDITOR using System.Reflection; #endif +using extOSC.Core; + namespace extOSC { public static class OSCUtilities @@ -64,27 +65,41 @@ public static float Map(float value, float inputMin, float inputMax, float outpu return outputMax < outputMin ? Mathf.Clamp(outputValue, outputMax, outputMin) : Mathf.Clamp(outputValue, outputMin, outputMax); } + // TODO: More optimize. public static bool CompareAddresses(string bindAddress, string messageAddress) { if (bindAddress == "*") return true; - if (!bindAddress.Contains("*")) - return bindAddress == messageAddress; - - if (!_cachedAddress.ContainsKey(bindAddress)) - _cachedAddress.Add(bindAddress, new List()); + if (!_cachedAddress.TryGetValue(bindAddress, out var cachedAddresses)) + { + cachedAddresses = new List(); - if (_cachedAddress[bindAddress].Contains(messageAddress)) + _cachedAddress.Add(bindAddress, cachedAddresses); + } + else if (cachedAddresses.Contains(messageAddress)) + { return true; + } - var regular = new Regex("^" + bindAddress.Replace("*", "(.+)") + "$"); - if (regular.IsMatch(messageAddress)) + if (bindAddress == messageAddress) { - _cachedAddress[bindAddress].Add(messageAddress); + cachedAddresses.Add(messageAddress); + return true; } + if (bindAddress.Contains("*")) + { + var regular = new Regex("^" + bindAddress.Replace("*", "(.+)") + "$"); + if (regular.IsMatch(messageAddress)) + { + cachedAddresses.Add(messageAddress); + + return true; + } + } + return false; }