From f3471dd36fdccbada02eaf8fa5b57d74e128841b Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 22 Jan 2017 14:57:39 +0100 Subject: [PATCH] pooling JpegDecoderCore.DecodedBlocks + made Sandbox46 executable --- .../Components/Block8x8F.cs | 1 + .../JpegDecoderCore.cs | 22 ++++++++++++++--- .../ImageSharp.Sandbox46.csproj | 9 ++++++- tests/ImageSharp.Sandbox46/Program.cs | 24 +++++++++++++++++++ 4 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 tests/ImageSharp.Sandbox46/Program.cs diff --git a/src/ImageSharp.Formats.Jpeg/Components/Block8x8F.cs b/src/ImageSharp.Formats.Jpeg/Components/Block8x8F.cs index 13475af09..2b8c15ab3 100644 --- a/src/ImageSharp.Formats.Jpeg/Components/Block8x8F.cs +++ b/src/ImageSharp.Formats.Jpeg/Components/Block8x8F.cs @@ -6,6 +6,7 @@ namespace ImageSharp.Formats.Jpg { using System; + using System.Buffers; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; diff --git a/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs b/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs index aa5158395..b9f1dc77b 100644 --- a/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Formats { using System; + using System.Buffers; using System.IO; using System.Runtime.CompilerServices; using System.Threading.Tasks; @@ -21,6 +22,11 @@ namespace ImageSharp.Formats /// public const int MaxComponents = 4; + /// + /// The maximum number of quantization tables + /// + public const int MaxTq = 3; + // Complex value type field + mutable + available to other classes = the field MUST NOT be private :P #pragma warning disable SA1401 // FieldsMustBePrivate /// @@ -35,9 +41,10 @@ namespace ImageSharp.Formats #pragma warning restore SA401 /// - /// The maximum number of quantization tables + /// The used to pool data in . + /// Should always clean arrays when returning! /// - private const int MaxTq = 3; + private static readonly ArrayPool BlockPool = ArrayPool.Create(); /// /// The App14 marker color-space @@ -406,6 +413,15 @@ namespace ImageSharp.Formats this.HuffmanTrees[i].Dispose(); } + for (int i = 0; i < this.DecodedBlocks.Length; i++) + { + Block8x8F[] blockArray = this.DecodedBlocks[i]; + if (blockArray != null) + { + BlockPool.Return(blockArray, true); + } + } + this.ycbcrImage?.Dispose(); this.Bytes.Dispose(); this.grayImage.ReturnPooled(); @@ -1407,7 +1423,7 @@ namespace ImageSharp.Formats { int size = this.TotalMCUCount * this.ComponentArray[i].HorizontalFactor * this.ComponentArray[i].VerticalFactor; - this.DecodedBlocks[i] = new Block8x8F[size]; + this.DecodedBlocks[i] = BlockPool.Rent(size); } } diff --git a/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj b/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj index 5a27190db..7994456da 100644 --- a/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj +++ b/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj @@ -6,7 +6,7 @@ Debug AnyCPU {96188137-5FA6-4924-AB6E-4EFF79C6E0BB} - Library + Exe Properties ImageSharp ImageSharp.Sandbox46 @@ -34,6 +34,9 @@ 4 true + + ImageSharp.Sandbox46.Program + @@ -199,6 +202,7 @@ Tests\TestUtilities\TestUtilityExtensions.cs + @@ -208,6 +212,9 @@ + + + diff --git a/tests/ImageSharp.Sandbox46/Program.cs b/tests/ImageSharp.Sandbox46/Program.cs new file mode 100644 index 000000000..db57cdb3d --- /dev/null +++ b/tests/ImageSharp.Sandbox46/Program.cs @@ -0,0 +1,24 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Sandbox46 +{ + using System; + + public class Program + { + /// + /// The main entry point. Useful for executing benchmarks and performance unit tests manually, + /// when the IDE test runners lack some of the functionality. Eg.: it's not possible to run JetBrains memory profiler for unit tests. + /// + /// + /// The arguments to pass to the program. + /// + public static void Main(string[] args) + { + Console.WriteLine("Hello."); + } + } +}