diff --git a/src/ImageSharp/Configuration.cs b/src/ImageSharp/Configuration.cs index 2fe3c26e2..9a627eeb7 100644 --- a/src/ImageSharp/Configuration.cs +++ b/src/ImageSharp/Configuration.cs @@ -86,7 +86,7 @@ namespace SixLabors.ImageSharp /// /// Gets or sets the that is currently in use. /// - public MemoryManager MemoryManager { get; set; } = ArrayPoolMemoryManager.CreateWithNormalPooling(); + public MemoryManager MemoryManager { get; set; } = ArrayPoolMemoryManager.CreateDefault(); /// /// Gets the maximum header size of all the formats. diff --git a/src/ImageSharp/Memory/ArrayPoolMemoryManager.Buffer{T}.cs b/src/ImageSharp/Memory/ArrayPoolMemoryManager.Buffer{T}.cs index 65a91bfdf..a00ee8c30 100644 --- a/src/ImageSharp/Memory/ArrayPoolMemoryManager.Buffer{T}.cs +++ b/src/ImageSharp/Memory/ArrayPoolMemoryManager.Buffer{T}.cs @@ -11,11 +11,20 @@ namespace SixLabors.ImageSharp.Memory /// public partial class ArrayPoolMemoryManager { + /// + /// The buffer implementation of + /// private class Buffer : IBuffer where T : struct { + /// + /// The length of the buffer + /// private readonly int length; + /// + /// A weak reference to the source pool. + /// private WeakReference> sourcePoolReference; public Buffer(byte[] data, int length, ArrayPool sourcePool) @@ -25,10 +34,15 @@ namespace SixLabors.ImageSharp.Memory this.sourcePoolReference = new WeakReference>(sourcePool); } + /// + /// Gets the buffer as a byte array. + /// protected byte[] Data { get; private set; } + /// public Span Span => this.Data.AsSpan().NonPortableCast().Slice(0, this.length); + /// public void Dispose() { if (this.Data == null || this.sourcePoolReference == null) @@ -46,6 +60,9 @@ namespace SixLabors.ImageSharp.Memory } } + /// + /// The implementation of . + /// private class ManagedByteBuffer : Buffer, IManagedByteBuffer { public ManagedByteBuffer(byte[] data, int length, ArrayPool sourcePool) diff --git a/src/ImageSharp/Memory/ArrayPoolMemoryManager.CommonFactoryMethods.cs b/src/ImageSharp/Memory/ArrayPoolMemoryManager.CommonFactoryMethods.cs index 918c5d41a..d1424870d 100644 --- a/src/ImageSharp/Memory/ArrayPoolMemoryManager.CommonFactoryMethods.cs +++ b/src/ImageSharp/Memory/ArrayPoolMemoryManager.CommonFactoryMethods.cs @@ -7,22 +7,36 @@ { /// /// The default value for: maximum size of pooled arrays in bytes. - /// Currently set to 32MB, which is equivalent to 8 megapixels of raw data. + /// Currently set to 24MB, which is equivalent to 8 megapixels of raw data. /// - internal const int DefaultMaxPooledBufferSizeInBytes = 32 * 1024 * 1024; + internal const int DefaultMaxPooledBufferSizeInBytes = 24 * 1024 * 1024; /// /// The value for: The threshold to pool arrays in which has less buckets for memory safety. /// private const int DefaultBufferSelectorThresholdInBytes = 8 * 1024 * 1024; + /// + /// The default bucket count for . + /// + private const int DefaultLargePoolBucketCount = 6; + + /// + /// The default bucket count for . + /// + private const int DefaultNormalPoolBucketCount = 16; + /// /// This is the default. Should be good for most use cases. /// /// The memory manager - public static ArrayPoolMemoryManager CreateWithNormalPooling() + public static ArrayPoolMemoryManager CreateDefault() { - return new ArrayPoolMemoryManager(DefaultMaxPooledBufferSizeInBytes, DefaultBufferSelectorThresholdInBytes, 8, 24); + return new ArrayPoolMemoryManager( + DefaultMaxPooledBufferSizeInBytes, + DefaultBufferSelectorThresholdInBytes, + DefaultLargePoolBucketCount, + DefaultNormalPoolBucketCount); } /// @@ -31,7 +45,16 @@ /// The memory manager public static ArrayPoolMemoryManager CreateWithModeratePooling() { - return new ArrayPoolMemoryManager(1024 * 1024, 1024 * 16, 16, 24); + return new ArrayPoolMemoryManager(1024 * 1024, 32 * 1024, 16, 24); + } + + /// + /// Only pool small buffers like image rows. + /// + /// The memory manager + public static ArrayPoolMemoryManager CreateWithMinimalPooling() + { + return new ArrayPoolMemoryManager(64 * 1024, 32 * 1024, 8, 24); } /// diff --git a/src/ImageSharp/Memory/ArrayPoolMemoryManager.cs b/src/ImageSharp/Memory/ArrayPoolMemoryManager.cs index 36bc3a870..7b8c7ab32 100644 --- a/src/ImageSharp/Memory/ArrayPoolMemoryManager.cs +++ b/src/ImageSharp/Memory/ArrayPoolMemoryManager.cs @@ -48,7 +48,7 @@ namespace SixLabors.ImageSharp.Memory /// The maximum size of pooled arrays. Arrays over the thershold are gonna be always allocated. /// Arrays over this threshold will be pooled in which has less buckets for memory safety. public ArrayPoolMemoryManager(int maxPoolSizeInBytes, int poolSelectorThresholdInBytes) - : this(maxPoolSizeInBytes, poolSelectorThresholdInBytes, 8, 24) + : this(maxPoolSizeInBytes, poolSelectorThresholdInBytes, DefaultLargePoolBucketCount, DefaultNormalPoolBucketCount) { } @@ -106,6 +106,7 @@ namespace SixLabors.ImageSharp.Memory return buffer; } + /// internal override IManagedByteBuffer AllocateManagedByteBuffer(int length, bool clear) { ArrayPool pool = this.GetArrayPool(length); diff --git a/src/ImageSharp/Memory/IManagedByteBuffer.cs b/src/ImageSharp/Memory/IManagedByteBuffer.cs index d75fb9b6c..4d159ce86 100644 --- a/src/ImageSharp/Memory/IManagedByteBuffer.cs +++ b/src/ImageSharp/Memory/IManagedByteBuffer.cs @@ -4,7 +4,7 @@ namespace SixLabors.ImageSharp.Memory { /// - /// Represents a byte buffer backed by a managed array. + /// Represents a byte buffer backed by a managed array. Useful for interop with classic .NET API-s. /// internal interface IManagedByteBuffer : IBuffer { diff --git a/src/ImageSharp/Memory/MemoryManager.cs b/src/ImageSharp/Memory/MemoryManager.cs index 8e2df8cec..52bdc897f 100644 --- a/src/ImageSharp/Memory/MemoryManager.cs +++ b/src/ImageSharp/Memory/MemoryManager.cs @@ -19,6 +19,12 @@ namespace SixLabors.ImageSharp.Memory internal abstract IBuffer Allocate(int length, bool clear) where T : struct; + /// + /// Allocates an + /// + /// The requested buffer length + /// A value indicating whether to clean the buffer + /// The internal abstract IManagedByteBuffer AllocateManagedByteBuffer(int length, bool clear); ///