perf: Removing redundant code, logging micro-optimization

This commit is contained in:
Chris Cameron
2019-02-07 10:37:35 -05:00
parent be6a6de65a
commit f8a813c97b

View File

@@ -2,7 +2,6 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using ICD.Common.Properties; using ICD.Common.Properties;
using ICD.Common.Utils.Extensions;
namespace ICD.Common.Utils.Collections namespace ICD.Common.Utils.Collections
{ {
@@ -16,8 +15,6 @@ namespace ICD.Common.Utils.Collections
private readonly LinkedList<TContents> m_Collection; private readonly LinkedList<TContents> m_Collection;
private int m_MaxSize; private int m_MaxSize;
private readonly SafeCriticalSection m_CollectionLock;
#region Properties #region Properties
/// <summary> /// <summary>
@@ -41,13 +38,13 @@ namespace ICD.Common.Utils.Collections
/// <summary> /// <summary>
/// Gets the number of items in the collection. /// Gets the number of items in the collection.
/// </summary> /// </summary>
public int Count { get { return m_CollectionLock.Execute(() => m_Collection.Count); } } public int Count { get { return m_Collection.Count; } }
/// <summary> /// <summary>
/// The IsSynchronized Boolean property returns True if the /// The IsSynchronized Boolean property returns True if the
/// collection is designed to be thread safe; otherwise, it returns False. /// collection is designed to be thread safe; otherwise, it returns False.
/// </summary> /// </summary>
public bool IsSynchronized { get { return true; } } public bool IsSynchronized { get { return false; } }
/// <summary> /// <summary>
/// The SyncRoot property returns an object, which is used for synchronizing /// The SyncRoot property returns an object, which is used for synchronizing
@@ -64,7 +61,6 @@ namespace ICD.Common.Utils.Collections
/// <param name="maxSize"></param> /// <param name="maxSize"></param>
public ScrollQueue(int maxSize) public ScrollQueue(int maxSize)
{ {
m_CollectionLock = new SafeCriticalSection();
m_Collection = new LinkedList<TContents>(); m_Collection = new LinkedList<TContents>();
MaxSize = maxSize; MaxSize = maxSize;
} }
@@ -76,7 +72,7 @@ namespace ICD.Common.Utils.Collections
/// </summary> /// </summary>
public void Clear() public void Clear()
{ {
m_CollectionLock.Execute(() => m_Collection.Clear()); m_Collection.Clear();
} }
/// <summary> /// <summary>
@@ -86,17 +82,8 @@ namespace ICD.Common.Utils.Collections
[PublicAPI] [PublicAPI]
public void Enqueue(TContents item) public void Enqueue(TContents item)
{ {
m_CollectionLock.Enter(); m_Collection.AddLast(item);
Trim();
try
{
m_Collection.AddLast(item);
Trim();
}
finally
{
m_CollectionLock.Leave();
}
} }
/// <summary> /// <summary>
@@ -106,18 +93,9 @@ namespace ICD.Common.Utils.Collections
[PublicAPI] [PublicAPI]
public TContents Dequeue() public TContents Dequeue()
{ {
m_CollectionLock.Enter(); TContents output = Peek();
m_Collection.RemoveFirst();
try return output;
{
TContents output = m_Collection.First.Value;
m_Collection.RemoveFirst();
return output;
}
finally
{
m_CollectionLock.Leave();
}
} }
/// <summary> /// <summary>
@@ -127,7 +105,7 @@ namespace ICD.Common.Utils.Collections
[PublicAPI] [PublicAPI]
public TContents Peek() public TContents Peek()
{ {
return m_CollectionLock.Execute(() => m_Collection.First.Value); return m_Collection.First.Value;
} }
#endregion #endregion
@@ -141,24 +119,15 @@ namespace ICD.Common.Utils.Collections
public IEnumerator<TContents> GetEnumerator() public IEnumerator<TContents> GetEnumerator()
{ {
return m_CollectionLock.Execute(() => m_Collection.ToList(Count).GetEnumerator()); return m_Collection.GetEnumerator();
} }
void ICollection.CopyTo(Array myArr, int index) void ICollection.CopyTo(Array myArr, int index)
{ {
m_CollectionLock.Enter(); foreach (TContents item in m_Collection)
try
{ {
foreach (TContents item in m_Collection) myArr.SetValue(item, index);
{ index++;
myArr.SetValue(item, index);
index++;
}
}
finally
{
m_CollectionLock.Leave();
} }
} }
@@ -171,17 +140,8 @@ namespace ICD.Common.Utils.Collections
/// </summary> /// </summary>
private void Trim() private void Trim()
{ {
m_CollectionLock.Enter(); while (Count > MaxSize)
m_Collection.RemoveFirst();
try
{
while (Count > MaxSize)
m_Collection.RemoveFirst();
}
finally
{
m_CollectionLock.Leave();
}
} }
#endregion #endregion