Skip to content
Browse files

Revert "Arrays retrieved from memory pool class are of fixed size now…

…. 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...
1 parent d8144d3 commit 71fdf8676c80a2a90884cfe6ea9f3c281c3740a6 @azeno azeno committed Aug 27, 2012
Showing with 21 additions and 8 deletions.
  1. +21 −8 common/src/core/Utils/Streams/MemoryPool.cs
View
29 common/src/core/Utils/Streams/MemoryPool.cs
@@ -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);
}
}
}

0 comments on commit 71fdf86

Please sign in to comment.
Something went wrong with that request. Please try again.