Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Power of Two cached arrays only #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 67 additions & 50 deletions CachedQuickLz/ArrayPool.cs
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,50 +1,67 @@
using System;
using System.Collections.Concurrent;

namespace CachedQuickLz
{
public class ArrayPool<T>
{
private static readonly ConcurrentDictionary<int, ConcurrentStack<T[]>> Bins;

static ArrayPool()
{
Bins = new ConcurrentDictionary<int, ConcurrentStack<T[]>>
{
[0] = new ConcurrentStack<T[]>()
};

for (var i = 0; i < 32; i++)
{
Bins[1 << i] = new ConcurrentStack<T[]>();
}
}

internal static T[] Spawn(int minLength)
{
var count = NextPowerOfTwo(minLength);
return Bins[count].TryPop(out var array) ? array : new T[count];
}

internal static void Recycle(T[] array)
{
Array.Clear(array, 0, array.Length);
var binKey = NextPowerOfTwo(array.Length + 1) / 2;

Bins[binKey].Push(array);
}

private static int NextPowerOfTwo(int value)
{
var result = value;

result |= result >> 1;
result |= result >> 2;
result |= result >> 4;
result |= result >> 8;
result |= result >> 16;

return result + 1;
}
}
}
using System;
using System.Collections.Concurrent;

namespace CachedQuickLz
{
public class ArrayPool<T> where T : struct //Value types only!
{
private static readonly ConcurrentDictionary<int, ConcurrentStack<T[]>> Bins;

public static int Size
{
get
{
var result = 0;
foreach (var bin in Bins.Values)
{
foreach (var array in bin)
{
result += array.Length;
}
}
return result;
}
}

static ArrayPool()
{
Bins = new ConcurrentDictionary<int, ConcurrentStack<T[]>>();

for (var i = 0; i < 32; i++)
{
Bins[1 << i] = new ConcurrentStack<T[]>();
}
}

internal static T[] Spawn(int minLength)
{
var count = NextPowerOfTwo(minLength);
return Bins[count].TryPop(out var array) ? array : new T[count];
}

internal static void Recycle(T[] array)
{
if (array.Length != NextPowerOfTwo(array.Length)) throw new InvalidOperationException("Trying to recycle an array that doesn't fit a bin. Memory leak. Please use arrays made with ArrayPool<T>.Spawn(int).");

Array.Clear(array, 0, array.Length);
var binKey = array.Length;

Bins[binKey].Push(array);
}

private static int NextPowerOfTwo(int value)
{
if (value <= 0) return 1;

var result = value - 1;

result |= result >> 1;
result |= result >> 2;
result |= result >> 4;
result |= result >> 8;
result |= result >> 16;

return result + 1;
}
}
}
Loading