Browse Source

dropping minSizeBytes + fixing tests

pull/431/head
Anton Firszov 8 years ago
parent
commit
7e3cb2892d
  1. 2
      src/ImageSharp/Configuration.cs
  2. 40
      src/ImageSharp/Memory/ArrayPoolMemoryManager.cs
  3. 2
      src/ImageSharp/Memory/MemoryManager.cs
  4. 3
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
  5. 5
      tests/ImageSharp.Tests/PixelFormats/PixelOperationsTests.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(512);
public MemoryManager MemoryManager { get; set; } = new ArrayPoolMemoryManager();
/// <summary>
/// Gets the maximum header size of all the formats.

40
src/ImageSharp/Memory/ArrayPoolMemoryManager.cs

@ -10,23 +10,20 @@ namespace SixLabors.ImageSharp.Memory
/// </summary>
public class ArrayPoolMemoryManager : MemoryManager
{
private readonly int minSizeBytes;
/// <summary>
/// Defines the default maximum size of pooled arrays.
/// Currently set to a value equivalent to 16 Megapixels of an <see cref="Rgba32"/> image.
/// </summary>
public const int DefaultMaxSizeInBytes = 4096 * 4096 * 4;
private readonly ArrayPool<byte> pool;
/// <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)
public ArrayPoolMemoryManager()
{
this.minSizeBytes = minSizeBytes;
this.pool = ArrayPool<byte>.Create(CalculateMaxArrayLength(), 50);
this.pool = ArrayPool<byte>.Create(DefaultMaxSizeInBytes, 50);
}
/// <inheritdoc />
@ -41,14 +38,6 @@ namespace SixLabors.ImageSharp.Memory
int itemSizeBytes = Unsafe.SizeOf<T>();
int bufferSizeInBytes = itemCount * itemSizeBytes;
if (this.minSizeBytes > 0 && bufferSizeInBytes < this.minSizeBytes)
{
// Minimum size set to 8 bytes to get past a misbehaving test
// (otherwise PngDecoderTests.Decode_IncorrectCRCForNonCriticalChunk_ExceptionIsThrown fails for the wrong reason)
// TODO: Remove this once the test is fixed
return new Buffer<T>(new T[Math.Max(itemCount, 8)], itemCount);
}
byte[] byteBuffer = this.pool.Rent(bufferSizeInBytes);
var buffer = new Buffer<T>(Unsafe.As<T[]>(byteBuffer), itemCount, this);
if (clear)
@ -62,19 +51,8 @@ namespace SixLabors.ImageSharp.Memory
/// <inheritdoc />
internal override void Release<T>(Buffer<T> buffer)
{
var byteBuffer = Unsafe.As<byte[]>(buffer.Array);
byte[] byteBuffer = Unsafe.As<byte[]>(buffer.Array);
this.pool.Return(byteBuffer);
}
/// <summary>
/// Heuristically calculates a reasonable maxArrayLength value for the backing <see cref="ArrayPool{T}"/>.
/// </summary>
/// <returns>The maxArrayLength value</returns>
internal static int CalculateMaxArrayLength()
{
const int MaximumExpectedImageSize = 16384 * 16384;
const int MaximumBytesPerPixel = 4;
return MaximumExpectedImageSize * MaximumBytesPerPixel;
}
}
}

2
src/ImageSharp/Memory/MemoryManager.cs

@ -45,7 +45,7 @@ namespace SixLabors.ImageSharp.Memory
internal Buffer2D<T> Allocate2D<T>(int width, int height, bool clear = false)
where T : struct
{
var buffer = this.Allocate<T>(width * height, clear);
Buffer<T> buffer = this.Allocate<T>(width * height, clear);
return new Buffer2D<T>(buffer, width, height);
}

3
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

@ -118,8 +118,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
}
public const string DecodeBaselineJpegOutputName = "DecodeBaselineJpeg";
[Theory]
[WithFile(TestImages.Jpeg.Baseline.Calliphora, CommonNonDefaultPixelTypes, false)]
[WithFile(TestImages.Jpeg.Baseline.Calliphora, CommonNonDefaultPixelTypes, true)]

5
tests/ImageSharp.Tests/PixelFormats/PixelOperationsTests.cs

@ -362,8 +362,9 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats
if (typeof(TDest) == typeof(Vector4))
{
Vector4[] expected = this.ExpectedDestBuffer.Array as Vector4[];
Vector4[] actual = this.ActualDestBuffer.Array as Vector4[];
Span<Vector4> expected = this.ExpectedDestBuffer.Span.NonPortableCast<TDest, Vector4>();
Span<Vector4> actual = this.ActualDestBuffer.Span.NonPortableCast<TDest, Vector4>();
for (int i = 0; i < count; i++)
{

Loading…
Cancel
Save