Skip to content

Commit

Permalink
perf(extOSC): OSCUtilities.CompareAddresses optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
Iam1337 committed Jul 19, 2021
1 parent 66e0745 commit 54cc544
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions Assets/extOSC/Scripts/OSCUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<string>());
if (!_cachedAddress.TryGetValue(bindAddress, out var cachedAddresses))
{
cachedAddresses = new List<string>();

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;
}

Expand Down

0 comments on commit 54cc544

Please sign in to comment.