Browse Source

ArrayPoolMemoryManager factory methods

pull/475/head
Anton Firszov 8 years ago
parent
commit
2fa099410f
  1. 2
      src/ImageSharp/Configuration.cs
  2. 46
      src/ImageSharp/Memory/ArrayPoolMemoryManager.CommonFactoryMethods.cs
  3. 13
      src/ImageSharp/Memory/ArrayPoolMemoryManager.cs
  4. 28
      tests/ImageSharp.Tests/Memory/ArrayPoolMemoryManagerTests.cs

2
src/ImageSharp/Configuration.cs

@ -86,7 +86,7 @@ namespace SixLabors.ImageSharp
/// <summary>
/// Gets or sets the <see cref="MemoryManager"/> that is currently in use.
/// </summary>
public MemoryManager MemoryManager { get; set; } = new ArrayPoolMemoryManager();
public MemoryManager MemoryManager { get; set; } = ArrayPoolMemoryManager.CreateWithNormalPooling();
/// <summary>
/// Gets the maximum header size of all the formats.

46
src/ImageSharp/Memory/ArrayPoolMemoryManager.CommonFactoryMethods.cs

@ -0,0 +1,46 @@
namespace SixLabors.ImageSharp.Memory
{
/// <summary>
/// Contains common factory methods and configuration constants.
/// </summary>
public partial class ArrayPoolMemoryManager
{
/// <summary>
/// The default value for: maximum size of pooled arrays in bytes.
/// Currently set to 32MB, which is equivalent to 8 megapixels of raw <see cref="Rgba32"/> data.
/// </summary>
internal const int DefaultMaxPooledBufferSizeInBytes = 32 * 1024 * 1024;
/// <summary>
/// The value for: The threshold to pool arrays in <see cref="largeArrayPool"/> which has less buckets for memory safety.
/// </summary>
private const int DefaultBufferSelectorThresholdInBytes = 8 * 1024 * 1024;
/// <summary>
/// This is the default. Should be good for most use cases.
/// </summary>
/// <returns>The memory manager</returns>
public static ArrayPoolMemoryManager CreateWithNormalPooling()
{
return new ArrayPoolMemoryManager(DefaultMaxPooledBufferSizeInBytes, DefaultBufferSelectorThresholdInBytes, 8, 24);
}
/// <summary>
/// For environments with limited memory capabilities. Only small images are pooled, which can result in reduced througput.
/// </summary>
/// <returns>The memory manager</returns>
public static ArrayPoolMemoryManager CreateWithModeratePooling()
{
return new ArrayPoolMemoryManager(1024 * 1024, 1024 * 16, 16, 24);
}
/// <summary>
/// RAM is not an issue for me, gimme maximum througput!
/// </summary>
/// <returns>The memory manager</returns>
public static ArrayPoolMemoryManager CreateWithAggressivePooling()
{
return new ArrayPoolMemoryManager(128 * 1024 * 1024, 32 * 1024 * 1024, 16, 32);
}
}
}

13
src/ImageSharp/Memory/ArrayPoolMemoryManager.cs

@ -11,17 +11,6 @@ namespace SixLabors.ImageSharp.Memory
/// </summary>
public partial class ArrayPoolMemoryManager : MemoryManager
{
/// <summary>
/// The default value for: maximum size of pooled arrays in bytes.
/// Currently set to 32MB, which is equivalent to 8 megapixels of raw <see cref="Rgba32"/> data.
/// </summary>
internal const int DefaultMaxPooledBufferSizeInBytes = 32 * 1024 * 1024;
/// <summary>
/// The value for: The threshold to pool arrays in <see cref="largeArrayPool"/> which has less buckets for memory safety.
/// </summary>
private const int DefaultLargeBufferThresholdInBytes = 8 * 1024 * 1024;
/// <summary>
/// The <see cref="ArrayPool{T}"/> for small-to-medium buffers which is not kept clean.
/// </summary>
@ -40,7 +29,7 @@ namespace SixLabors.ImageSharp.Memory
/// Initializes a new instance of the <see cref="ArrayPoolMemoryManager"/> class.
/// </summary>
public ArrayPoolMemoryManager()
: this(DefaultMaxPooledBufferSizeInBytes, DefaultLargeBufferThresholdInBytes)
: this(DefaultMaxPooledBufferSizeInBytes, DefaultBufferSelectorThresholdInBytes)
{
}

28
tests/ImageSharp.Tests/Memory/ArrayPoolMemoryManagerTests.cs

@ -19,7 +19,7 @@ namespace SixLabors.ImageSharp.Tests.Memory
private const int PoolSelectorThresholdInBytes = MaxPooledBufferSizeInBytes / 2;
private MemoryManager MemoryManager { get; } = new ArrayPoolMemoryManager(MaxPooledBufferSizeInBytes, PoolSelectorThresholdInBytes);
private MemoryManager MemoryManager { get; set; } = new ArrayPoolMemoryManager(MaxPooledBufferSizeInBytes, PoolSelectorThresholdInBytes);
/// <summary>
/// Rent a buffer -> return it -> re-rent -> verify if it's span points to the previous location
@ -161,5 +161,31 @@ namespace SixLabors.ImageSharp.Tests.Memory
Assert.False(Unsafe.AreSame(ref ptr2Small, ref large.DangerousGetPinnableReference()));
}
[Fact]
public void CreateWithAggressivePooling()
{
this.MemoryManager = ArrayPoolMemoryManager.CreateWithAggressivePooling();
Assert.True(this.CheckIsRentingPooledBuffer<Rgba32>(4096 * 4096));
}
[Fact]
public void CreateWithNormalPooling()
{
this.MemoryManager = ArrayPoolMemoryManager.CreateWithNormalPooling();
Assert.False(this.CheckIsRentingPooledBuffer<Rgba32>(2 * 4096 * 4096));
Assert.True(this.CheckIsRentingPooledBuffer<Rgba32>(2048 * 2048));
}
[Fact]
public void CreateWithModeratePooling()
{
this.MemoryManager = ArrayPoolMemoryManager.CreateWithModeratePooling();
Assert.False(this.CheckIsRentingPooledBuffer<Rgba32>(2048 * 2048));
Assert.True(this.CheckIsRentingPooledBuffer<Rgba32>(1024 * 16));
}
}
}
Loading…
Cancel
Save