From ea49ba226ac66366bb14bacd6e759ce511c10b51 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sat, 9 Dec 2023 19:58:02 +1000 Subject: [PATCH] Use shared instances --- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 13 ++++++++---- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 21 ++++++++++++------- .../Bulk/ToVector4_Rgba32.cs | 2 +- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 3d0ba6f97..3eabbdef0 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -121,6 +121,11 @@ internal sealed class PngDecoderCore : IImageDecoderInternals /// private readonly PngCrcChunkHandling pngCrcChunkHandling; + /// + /// A reusable Crc32 hashing instance. + /// + private readonly Crc32 crc32 = new(); + /// /// Initializes a new instance of the class. /// @@ -1912,11 +1917,11 @@ internal sealed class PngDecoderCore : IImageDecoderInternals Span chunkType = stackalloc byte[4]; BinaryPrimitives.WriteUInt32BigEndian(chunkType, (uint)chunk.Type); - Crc32 crc32 = new(); - crc32.Append(chunkType); - crc32.Append(chunk.Data.GetSpan()); + this.crc32.Reset(); + this.crc32.Append(chunkType); + this.crc32.Append(chunk.Data.GetSpan()); - if (crc32.GetCurrentHashAsUInt32() != inputCrc) + if (this.crc32.GetCurrentHashAsUInt32() != inputCrc) { string chunkTypeName = Encoding.ASCII.GetString(chunkType); diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 751df49f1..e348d7467 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -124,6 +124,11 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable /// private int derivedTransparencyIndex = -1; + /// + /// A reusable Crc32 hashing instance. + /// + private readonly Crc32 crc32 = new(); + /// /// Initializes a new instance of the class. /// @@ -1364,17 +1369,17 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable stream.Write(buffer); - Crc32 crc32 = new(); - crc32.Append(buffer[4..]); // Write the type buffer + this.crc32.Reset(); + this.crc32.Append(buffer[4..]); // Write the type buffer if (data.Length > 0 && length > 0) { stream.Write(data, offset, length); - crc32.Append(data.Slice(offset, length)); + this.crc32.Append(data.Slice(offset, length)); } - BinaryPrimitives.WriteUInt32BigEndian(buffer, crc32.GetCurrentHashAsUInt32()); + BinaryPrimitives.WriteUInt32BigEndian(buffer, this.crc32.GetCurrentHashAsUInt32()); stream.Write(buffer, 0, 4); // write the crc } @@ -1397,17 +1402,17 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable stream.Write(buffer); - Crc32 crc32 = new(); - crc32.Append(buffer[4..]); // Write the type buffer + this.crc32.Reset(); + this.crc32.Append(buffer[4..]); // Write the type buffer if (data.Length > 0 && length > 0) { stream.Write(data, offset, length); - crc32.Append(data.Slice(offset, length)); + this.crc32.Append(data.Slice(offset, length)); } - BinaryPrimitives.WriteUInt32BigEndian(buffer, crc32.GetCurrentHashAsUInt32()); + BinaryPrimitives.WriteUInt32BigEndian(buffer, this.crc32.GetCurrentHashAsUInt32()); stream.Write(buffer, 0, 4); // write the crc } diff --git a/tests/ImageSharp.Benchmarks/Bulk/ToVector4_Rgba32.cs b/tests/ImageSharp.Benchmarks/Bulk/ToVector4_Rgba32.cs index 9abf0ed22..1ceae8414 100644 --- a/tests/ImageSharp.Benchmarks/Bulk/ToVector4_Rgba32.cs +++ b/tests/ImageSharp.Benchmarks/Bulk/ToVector4_Rgba32.cs @@ -11,7 +11,7 @@ using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Benchmarks.Bulk; -[Config(typeof(Config.ShortCore31))] +[Config(typeof(Config.Short))] public class ToVector4_Rgba32 : ToVector4 { [Benchmark]