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);
return s_TypeToAttributesCache.ContainsKey(type)
? s_TypeToAttributesCache[type].ToArray()
IcdHashSet<Attribute> attributes;
return s_TypeToAttributesCache.TryGetValue(type, out attributes)
? attributes.ToArray(attributes.Count)
: Enumerable.Empty<Attribute>();
}

View File

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

View File

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

View File

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

View File

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