Browse Source

Use shared instances

pull/2611/head
James Jackson-South 2 years ago
parent
commit
ea49ba226a
  1. 13
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  2. 21
      src/ImageSharp/Formats/Png/PngEncoderCore.cs
  3. 2
      tests/ImageSharp.Benchmarks/Bulk/ToVector4_Rgba32.cs

13
src/ImageSharp/Formats/Png/PngDecoderCore.cs

@ -121,6 +121,11 @@ internal sealed class PngDecoderCore : IImageDecoderInternals
/// </summary>
private readonly PngCrcChunkHandling pngCrcChunkHandling;
/// <summary>
/// A reusable Crc32 hashing instance.
/// </summary>
private readonly Crc32 crc32 = new();
/// <summary>
/// Initializes a new instance of the <see cref="PngDecoderCore"/> class.
/// </summary>
@ -1912,11 +1917,11 @@ internal sealed class PngDecoderCore : IImageDecoderInternals
Span<byte> 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);

21
src/ImageSharp/Formats/Png/PngEncoderCore.cs

@ -124,6 +124,11 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable
/// </summary>
private int derivedTransparencyIndex = -1;
/// <summary>
/// A reusable Crc32 hashing instance.
/// </summary>
private readonly Crc32 crc32 = new();
/// <summary>
/// Initializes a new instance of the <see cref="PngEncoderCore" /> class.
/// </summary>
@ -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
}

2
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<Rgba32>
{
[Benchmark]

Loading…
Cancel
Save