From 27ca2cb629335bae3c8def011f5fbeb1bcf346a8 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 13 Apr 2017 10:57:24 +1000 Subject: [PATCH] Use a single array pool --- src/ImageSharp/Quantizers/Wu/WuArrayPool.cs | 36 +++++++++++++++++ src/ImageSharp/Quantizers/Wu/WuQuantizer.cs | 43 +++++++-------------- 2 files changed, 50 insertions(+), 29 deletions(-) create mode 100644 src/ImageSharp/Quantizers/Wu/WuArrayPool.cs diff --git a/src/ImageSharp/Quantizers/Wu/WuArrayPool.cs b/src/ImageSharp/Quantizers/Wu/WuArrayPool.cs new file mode 100644 index 000000000..5e4956f01 --- /dev/null +++ b/src/ImageSharp/Quantizers/Wu/WuArrayPool.cs @@ -0,0 +1,36 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Quantizers +{ + using System.Buffers; + + /// + /// Provides array pooling for the . + /// This is a separate class so that the pools can be shared accross multiple generic quantizer instaces. + /// + internal static class WuArrayPool + { + /// + /// The long array pool. + /// + public static readonly ArrayPool LongPool = ArrayPool.Create(TableLength, 25); + + /// + /// The float array pool. + /// + public static readonly ArrayPool FloatPool = ArrayPool.Create(TableLength, 5); + + /// + /// The byte array pool. + /// + public static readonly ArrayPool BytePool = ArrayPool.Create(TableLength, 5); + + /// + /// The table length. Matches the calculated value in + /// + private const int TableLength = 2471625; + } +} \ No newline at end of file diff --git a/src/ImageSharp/Quantizers/Wu/WuQuantizer.cs b/src/ImageSharp/Quantizers/Wu/WuQuantizer.cs index a8cdb0f9a..ba8047c06 100644 --- a/src/ImageSharp/Quantizers/Wu/WuQuantizer.cs +++ b/src/ImageSharp/Quantizers/Wu/WuQuantizer.cs @@ -58,21 +58,6 @@ namespace ImageSharp.Quantizers /// private const int TableLength = IndexCount * IndexCount * IndexCount * IndexAlphaCount; - /// - /// The long array pool. - /// - private static readonly ArrayPool LongPool = ArrayPool.Create(TableLength, 25); - - /// - /// The float array pool. - /// - private static readonly ArrayPool FloatPool = ArrayPool.Create(TableLength, 5); - - /// - /// The byte array pool. - /// - private static readonly ArrayPool BytePool = ArrayPool.Create(TableLength, 5); - /// /// Moment of P(c). /// @@ -118,13 +103,13 @@ namespace ImageSharp.Quantizers /// public WuQuantizer() { - this.vwt = LongPool.Rent(TableLength); - this.vmr = LongPool.Rent(TableLength); - this.vmg = LongPool.Rent(TableLength); - this.vmb = LongPool.Rent(TableLength); - this.vma = LongPool.Rent(TableLength); - this.m2 = FloatPool.Rent(TableLength); - this.tag = BytePool.Rent(TableLength); + this.vwt = WuArrayPool.LongPool.Rent(TableLength); + this.vmr = WuArrayPool.LongPool.Rent(TableLength); + this.vmg = WuArrayPool.LongPool.Rent(TableLength); + this.vmb = WuArrayPool.LongPool.Rent(TableLength); + this.vma = WuArrayPool.LongPool.Rent(TableLength); + this.m2 = WuArrayPool.FloatPool.Rent(TableLength); + this.tag = WuArrayPool.BytePool.Rent(TableLength); } /// @@ -789,13 +774,13 @@ namespace ImageSharp.Quantizers }); // Cleanup - LongPool.Return(this.vwt); - LongPool.Return(this.vmr); - LongPool.Return(this.vmg); - LongPool.Return(this.vmb); - LongPool.Return(this.vma); - FloatPool.Return(this.m2); - BytePool.Return(this.tag); + WuArrayPool.LongPool.Return(this.vwt); + WuArrayPool.LongPool.Return(this.vmr); + WuArrayPool.LongPool.Return(this.vmg); + WuArrayPool.LongPool.Return(this.vmb); + WuArrayPool.LongPool.Return(this.vma); + WuArrayPool.FloatPool.Return(this.m2); + WuArrayPool.BytePool.Return(this.tag); return new QuantizedImage(width, height, pallette, pixels); }