From e806b7c4c242181fb1f6fa3e2ba7facaa5e6b0b5 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Mon, 12 Mar 2018 21:36:36 -0400 Subject: [PATCH] Optimization --- ICD.Common.Utils/EnumUtils.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/ICD.Common.Utils/EnumUtils.cs b/ICD.Common.Utils/EnumUtils.cs index e27261d..b0a7d70 100644 --- a/ICD.Common.Utils/EnumUtils.cs +++ b/ICD.Common.Utils/EnumUtils.cs @@ -15,6 +15,7 @@ namespace ICD.Common.Utils public static class EnumUtils { private static readonly Dictionary s_EnumValuesCache; + private static readonly Dictionary> s_EnumFlagsCache; /// /// Static constructor. @@ -22,6 +23,7 @@ namespace ICD.Common.Utils static EnumUtils() { s_EnumValuesCache = new Dictionary(); + s_EnumFlagsCache = new Dictionary>(); } /// @@ -276,7 +278,15 @@ namespace ICD.Common.Utils // ReSharper disable once CompareNonConstrainedGenericWithNull throw new ArgumentException(string.Format("{0} is not an enum", value == null ? "NULL" : value.GetType().Name), "value"); - return GetValues().Where(e => HasFlag(value, e)); + Type type = typeof(T); + + if (!s_EnumFlagsCache.ContainsKey(type)) + s_EnumFlagsCache.Add(type, new Dictionary()); + + if (!s_EnumFlagsCache[type].ContainsKey(value)) + s_EnumFlagsCache[type][value] = GetValues().Where(e => HasFlag(value, e)).Cast().ToArray(); + + return s_EnumFlagsCache[type][value].Cast(); } ///