Skip to content

Commit

Permalink
Revert "Arrays retrieved from memory pool class are of fixed size now…
Browse files Browse the repository at this point in the history
…. Dynamic size was error prone and hurt performance."

This reverts commit dfee309132775543cec5d2b8cf4109ee7261da5e.

Reverted, because it breaks existing code (not production code, but still...).
  • Loading branch information
azeno committed Aug 27, 2012
1 parent d8144d3 commit 71fdf86
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions common/src/core/Utils/Streams/MemoryPool.cs
Expand Up @@ -7,28 +7,41 @@ namespace VVVV.Utils.Streams
[ComVisible(false)]
public static class MemoryPool<T>
{
private static readonly Stack<T[]> FStack = new Stack<T[]>();
private static readonly Dictionary<int, Stack<T[]>> FPool = new Dictionary<int, Stack<T[]>>();

public static T[] GetArray()
public static T[] GetArray(int length = StreamUtils.BUFFER_SIZE)
{
lock (FStack)
lock (FPool)
{
if (FStack.Count == 0)
Stack<T[]> stack = null;
if (!FPool.TryGetValue(length, out stack))
{
return new T[StreamUtils.BUFFER_SIZE];
stack = new Stack<T[]>();
FPool[length] = stack;
}

if (stack.Count == 0)
{
return new T[length];
}
else
{
return FStack.Pop();
return stack.Pop();
}
}
}

public static void PutArray(T[] array)
{
lock (FStack)
lock (FPool)
{
FStack.Push(array);
Stack<T[]> stack = null;
if (!FPool.TryGetValue(array.Length, out stack))
{
stack = new Stack<T[]>();
FPool[array.Length] = stack;
}
stack.Push(array);
}
}
}
Expand Down

0 comments on commit 71fdf86

Please sign in to comment.