From cffe850bd77a15a292cb15cb618c3c05f04b44d1 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Fri, 9 Mar 2018 13:48:54 -0500 Subject: [PATCH 1/3] Enum optimizations --- ICD.Common.Utils/EnumUtils.cs | 14 ++++++++++---- ICD.Common.Utils/Extensions/EnumExtensions.cs | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/ICD.Common.Utils/EnumUtils.cs b/ICD.Common.Utils/EnumUtils.cs index 528cd03..38241fe 100644 --- a/ICD.Common.Utils/EnumUtils.cs +++ b/ICD.Common.Utils/EnumUtils.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using ICD.Common.Properties; -using ICD.Common.Utils.Collections; using ICD.Common.Utils.Extensions; #if SIMPLSHARP using System.Globalization; @@ -15,8 +14,15 @@ namespace ICD.Common.Utils { public static class EnumUtils { - private static readonly Dictionary> s_EnumValuesCache = - new Dictionary>(); + private static readonly Dictionary s_EnumValuesCache; + + /// + /// Static constructor. + /// + static EnumUtils() + { + s_EnumValuesCache = new Dictionary(); + } /// /// Returns true if the given type is an enum. @@ -119,7 +125,7 @@ namespace ICD.Common.Utils // Reflection is slow and this method is called a lot, so we cache the results. if (!s_EnumValuesCache.ContainsKey(type)) - s_EnumValuesCache[type] = GetValuesUncached(type).ToIcdHashSet(); + s_EnumValuesCache[type] = GetValuesUncached(type).ToArray(); return s_EnumValuesCache[type]; } diff --git a/ICD.Common.Utils/Extensions/EnumExtensions.cs b/ICD.Common.Utils/Extensions/EnumExtensions.cs index f6de53f..aa44ed3 100644 --- a/ICD.Common.Utils/Extensions/EnumExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumExtensions.cs @@ -49,8 +49,8 @@ namespace ICD.Common.Utils.Extensions throw new ArgumentException(message); } - ulong num = Convert.ToUInt64(value); - return (Convert.ToUInt64(extends) & num) == num; + int num = (int)(object)value; + return ((int)(object)extends & num) == num; } } } From 02ecff187d64aa381695d7a9083695419f519a63 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Fri, 9 Mar 2018 13:49:13 -0500 Subject: [PATCH 2/3] Method for profiling each step in an enumerable --- ICD.Common.Utils/Timers/IcdStopwatch.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ICD.Common.Utils/Timers/IcdStopwatch.cs b/ICD.Common.Utils/Timers/IcdStopwatch.cs index d6d1810..1f9f398 100644 --- a/ICD.Common.Utils/Timers/IcdStopwatch.cs +++ b/ICD.Common.Utils/Timers/IcdStopwatch.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using ICD.Common.Properties; #if SIMPLSHARP using Crestron.SimplSharp; @@ -128,6 +129,22 @@ namespace ICD.Common.Utils.Timers return output; } + /// + /// Profiles getting each item from the enumerable. + /// + /// + /// + /// + /// + public static IEnumerable Profile(IEnumerable enumerable, string name) + { + using (IEnumerator enumerator = enumerable.GetEnumerator()) + { + while (Profile(() => enumerator.MoveNext(), name)) + yield return enumerator.Current; + } + } + private static void PrintProfile(IcdStopwatch stopwatch, string name) { long elapsed = stopwatch.ElapsedMilliseconds; From 825fb55bd8029bd2e19e61b3d562fa518c8ea1ee Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Fri, 9 Mar 2018 14:14:37 -0500 Subject: [PATCH 3/3] Tidying --- ICD.Common.Utils/EnumUtils.cs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/ICD.Common.Utils/EnumUtils.cs b/ICD.Common.Utils/EnumUtils.cs index 38241fe..e27261d 100644 --- a/ICD.Common.Utils/EnumUtils.cs +++ b/ICD.Common.Utils/EnumUtils.cs @@ -533,17 +533,7 @@ 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 ToEnum((object)value); - } - - /// - /// Converts the given enum value to an Enum. - /// - /// - /// - public static Enum ToEnum(object value) - { - return (Enum)value; + return (Enum)(object)value; } #endregion