perf: Micro-optimizations

This commit is contained in:
Chris Cameron
2018-07-20 16:41:44 -04:00
parent 6fb1e53776
commit 74c59bd7f3
5 changed files with 46 additions and 41 deletions

View File

@@ -232,8 +232,10 @@ namespace ICD.Common.Utils
CacheType(type); CacheType(type);
return s_TypeToAttributesCache.ContainsKey(type) IcdHashSet<Attribute> attributes;
? s_TypeToAttributesCache[type].ToArray()
return s_TypeToAttributesCache.TryGetValue(type, out attributes)
? attributes.ToArray(attributes.Count)
: Enumerable.Empty<Attribute>(); : Enumerable.Empty<Attribute>();
} }

View File

@@ -246,15 +246,7 @@ namespace ICD.Common.Utils.Collections
throw new ArgumentNullException("items"); throw new ArgumentNullException("items");
foreach (T item in items) foreach (T item in items)
{ Add(item);
// ReSharper disable CompareNonConstrainedGenericWithNull
if (item == null)
// ReSharper restore CompareNonConstrainedGenericWithNull
throw new InvalidOperationException("item");
if (!m_Dict.ContainsKey(item))
m_Dict[item] = null;
}
} }
/// <summary> /// <summary>

View File

@@ -73,10 +73,14 @@ namespace ICD.Common.Utils.Collections
[PublicAPI] [PublicAPI]
public void Enqueue(T item, int priority) public void Enqueue(T item, int priority)
{ {
if (!m_PriorityToQueue.ContainsKey(priority)) List<T> queue;
m_PriorityToQueue.Add(priority, new List<T>()); if (!m_PriorityToQueue.TryGetValue(priority, out queue))
{
queue = new List<T>();
m_PriorityToQueue[priority] = queue;
}
m_PriorityToQueue[priority].Add(item); queue.Add(item);
m_Count++; m_Count++;
} }
@@ -89,10 +93,14 @@ namespace ICD.Common.Utils.Collections
{ {
const int priority = int.MinValue; const int priority = int.MinValue;
if (!m_PriorityToQueue.ContainsKey(priority)) List<T> queue;
m_PriorityToQueue.Add(priority, new List<T>()); if (!m_PriorityToQueue.TryGetValue(priority, out queue))
{
queue = new List<T>();
m_PriorityToQueue[priority] = queue;
}
m_PriorityToQueue[priority].Insert(0, item); queue.Insert(0, item);
m_Count++; m_Count++;
} }

View File

@@ -185,18 +185,18 @@ namespace ICD.Common.Utils.Extensions
if (extends == null) if (extends == null)
throw new ArgumentNullException("extends"); throw new ArgumentNullException("extends");
if (!s_TypeAllTypes.ContainsKey(extends)) Type[] types;
if (!s_TypeAllTypes.TryGetValue(extends, out types))
{ {
Type[] types = types = extends.GetBaseTypes()
extends.GetBaseTypes() .Concat(extends.GetInterfaces())
.Concat(extends.GetInterfaces()) .Prepend(extends)
.Prepend(extends) .ToArray();
.ToArray();
s_TypeAllTypes.Add(extends, types); s_TypeAllTypes[extends] = types;
} }
return s_TypeAllTypes[extends]; return types;
} }
/// <summary> /// <summary>
@@ -209,13 +209,14 @@ namespace ICD.Common.Utils.Extensions
if (extends == null) if (extends == null)
throw new ArgumentNullException("extends"); throw new ArgumentNullException("extends");
if (!s_TypeBaseTypes.ContainsKey(extends)) Type[] types;
if (!s_TypeBaseTypes.TryGetValue(extends, out types))
{ {
Type[] types = GetBaseTypesIterator(extends).ToArray(); types = GetBaseTypesIterator(extends).ToArray();
s_TypeBaseTypes.Add(extends, types); s_TypeBaseTypes[extends] = types;
} }
return s_TypeBaseTypes[extends]; return types;
} }
private static IEnumerable<Type> GetBaseTypesIterator(Type type) private static IEnumerable<Type> GetBaseTypesIterator(Type type)
@@ -244,7 +245,8 @@ namespace ICD.Common.Utils.Extensions
if (extends == null) if (extends == null)
throw new ArgumentNullException("extends"); throw new ArgumentNullException("extends");
if (!s_TypeImmediateInterfaces.ContainsKey(extends)) Type[] immediateInterfaces;
if (!s_TypeImmediateInterfaces.TryGetValue(extends, out immediateInterfaces))
{ {
IEnumerable<Type> allInterfaces = extends.GetInterfaces(); IEnumerable<Type> allInterfaces = extends.GetInterfaces();
@@ -254,12 +256,12 @@ namespace ICD.Common.Utils.Extensions
.SelectMany(t => t.GetImmediateInterfaces()) .SelectMany(t => t.GetImmediateInterfaces())
.Distinct(); .Distinct();
Type[] immediateInterfaces = allInterfaces.Except(childInterfaces).ToArray(); immediateInterfaces = allInterfaces.Except(childInterfaces).ToArray();
s_TypeImmediateInterfaces.Add(extends, immediateInterfaces); s_TypeImmediateInterfaces[extends] = immediateInterfaces;
} }
return s_TypeImmediateInterfaces[extends]; return immediateInterfaces;
} }
/// <summary> /// <summary>
@@ -272,17 +274,17 @@ namespace ICD.Common.Utils.Extensions
if (extends == null) if (extends == null)
throw new ArgumentNullException("extends"); throw new ArgumentNullException("extends");
if (!s_TypeMinimalInterfaces.ContainsKey(extends)) Type[] minimalInterfaces;
if (!s_TypeMinimalInterfaces.TryGetValue(extends, out minimalInterfaces))
{ {
Type[] allInterfaces = extends.GetInterfaces(); Type[] allInterfaces = extends.GetInterfaces();
Type[] minimalInterfaces = minimalInterfaces = allInterfaces.Except(allInterfaces.SelectMany(t => t.GetInterfaces()))
allInterfaces.Except(allInterfaces.SelectMany(t => t.GetInterfaces())) .ToArray();
.ToArray();
s_TypeMinimalInterfaces.Add(extends, minimalInterfaces); s_TypeMinimalInterfaces[extends] = minimalInterfaces;
} }
return s_TypeMinimalInterfaces[extends]; return minimalInterfaces;
} }
/// <summary> /// <summary>

View File

@@ -345,10 +345,11 @@ namespace ICD.Common.Utils.Services
{ {
m_ServicesSection.Enter(); m_ServicesSection.Enter();
if (!m_Services.ContainsKey(tService)) object stored;
if (!m_Services.TryGetValue(tService, out stored))
return false; return false;
return m_Services[tService] == service && m_Services.Remove(tService); return service == stored && m_Services.Remove(tService);
} }
finally finally
{ {