Browse Source

- Add a minimum size threshold for array pool usage

- Add a null memory manager that doesn't do any actual memory management
pull/431/head
Lauri Kotilainen 8 years ago
parent
commit
a73283f0e6
  1. 36
      src/ImageSharp/Memory/ArrayPoolMemoryManager.cs
  2. 2
      src/ImageSharp/Memory/MemoryManager.cs
  3. 19
      src/ImageSharp/Memory/NullMemoryManager.cs

36
src/ImageSharp/Memory/ArrayPoolMemoryManager.cs

@ -1,4 +1,5 @@
using System.Buffers;
using System.Runtime.InteropServices;
namespace SixLabors.ImageSharp.Memory
{
@ -7,9 +8,30 @@ namespace SixLabors.ImageSharp.Memory
/// </summary>
public class ArrayPoolMemoryManager : MemoryManager
{
private readonly int minSizeBytes;
/// <summary>
/// Initializes a new instance of the <see cref="ArrayPoolMemoryManager"/> class.
/// By passing an integer greater than 0 as <paramref name="minSizeBytes"/>, a
/// minimum threshold for pooled allocations is set. Any allocation requests that
/// would require less size than the threshold will not be managed within the array pool.
/// </summary>
/// <param name="minSizeBytes">
/// Minimum size, in bytes, before an array pool is used to satisfy the request.
/// </param>
public ArrayPoolMemoryManager(int minSizeBytes = 0)
{
this.minSizeBytes = minSizeBytes;
}
/// <inheritdoc />
internal override Buffer<T> Allocate<T>(int size, bool clear = false)
{
if (this.minSizeBytes > 0 && size < this.minSizeBytes * SizeHelper<T>.Size)
{
return new Buffer<T>(new T[size], size);
}
var buffer = new Buffer<T>(PixelDataPool<T>.Rent(size), size, this);
if (clear)
{
@ -24,5 +46,19 @@ namespace SixLabors.ImageSharp.Memory
{
PixelDataPool<T>.Return(buffer.Array);
}
internal static class SizeHelper<T>
{
static SizeHelper()
{
#if NETSTANDARD1_1
Size = Marshal.SizeOf(typeof(T));
#else
Size = Marshal.SizeOf<T>();
#endif
}
public static int Size { get; }
}
}
}

2
src/ImageSharp/Memory/MemoryManager.cs

@ -14,7 +14,7 @@ namespace SixLabors.ImageSharp.Memory
/// <summary>
/// Gets or sets the <see cref="MemoryManager"/> that is currently in use.
/// </summary>
public static MemoryManager Current { get; set; } = new ArrayPoolMemoryManager();
public static MemoryManager Current { get; set; } = new ArrayPoolMemoryManager(1024 * 80);
/// <summary>
/// Allocates a <see cref="Buffer{T}"/> of size <paramref name="size"/>, optionally

19
src/ImageSharp/Memory/NullMemoryManager.cs

@ -0,0 +1,19 @@
namespace SixLabors.ImageSharp.Memory
{
/// <summary>
/// Implements <see cref="MemoryManager"/> by allocating new buffers on every call.
/// </summary>
public class NullMemoryManager : MemoryManager
{
/// <inheritdoc />
internal override Buffer<T> Allocate<T>(int size, bool clear = false)
{
return new Buffer<T>(new T[size], size);
}
/// <inheritdoc />
internal override void Release<T>(Buffer<T> buffer)
{
}
}
}
Loading…
Cancel
Save