Skip to content

Commit ea49ba2

Browse files
Use shared instances
1 parent 5c13ca7 commit ea49ba2

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

src/ImageSharp/Formats/Png/PngDecoderCore.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ internal sealed class PngDecoderCore : IImageDecoderInternals
121121
/// </summary>
122122
private readonly PngCrcChunkHandling pngCrcChunkHandling;
123123

124+
/// <summary>
125+
/// A reusable Crc32 hashing instance.
126+
/// </summary>
127+
private readonly Crc32 crc32 = new();
128+
124129
/// <summary>
125130
/// Initializes a new instance of the <see cref="PngDecoderCore"/> class.
126131
/// </summary>
@@ -1912,11 +1917,11 @@ private void ValidateChunk(in PngChunk chunk, Span<byte> buffer)
19121917
Span<byte> chunkType = stackalloc byte[4];
19131918
BinaryPrimitives.WriteUInt32BigEndian(chunkType, (uint)chunk.Type);
19141919

1915-
Crc32 crc32 = new();
1916-
crc32.Append(chunkType);
1917-
crc32.Append(chunk.Data.GetSpan());
1920+
this.crc32.Reset();
1921+
this.crc32.Append(chunkType);
1922+
this.crc32.Append(chunk.Data.GetSpan());
19181923

1919-
if (crc32.GetCurrentHashAsUInt32() != inputCrc)
1924+
if (this.crc32.GetCurrentHashAsUInt32() != inputCrc)
19201925
{
19211926
string chunkTypeName = Encoding.ASCII.GetString(chunkType);
19221927

src/ImageSharp/Formats/Png/PngEncoderCore.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable
124124
/// </summary>
125125
private int derivedTransparencyIndex = -1;
126126

127+
/// <summary>
128+
/// A reusable Crc32 hashing instance.
129+
/// </summary>
130+
private readonly Crc32 crc32 = new();
131+
127132
/// <summary>
128133
/// Initializes a new instance of the <see cref="PngEncoderCore" /> class.
129134
/// </summary>
@@ -1364,17 +1369,17 @@ private void WriteChunk(Stream stream, PngChunkType type, Span<byte> data, int o
13641369

13651370
stream.Write(buffer);
13661371

1367-
Crc32 crc32 = new();
1368-
crc32.Append(buffer[4..]); // Write the type buffer
1372+
this.crc32.Reset();
1373+
this.crc32.Append(buffer[4..]); // Write the type buffer
13691374

13701375
if (data.Length > 0 && length > 0)
13711376
{
13721377
stream.Write(data, offset, length);
13731378

1374-
crc32.Append(data.Slice(offset, length));
1379+
this.crc32.Append(data.Slice(offset, length));
13751380
}
13761381

1377-
BinaryPrimitives.WriteUInt32BigEndian(buffer, crc32.GetCurrentHashAsUInt32());
1382+
BinaryPrimitives.WriteUInt32BigEndian(buffer, this.crc32.GetCurrentHashAsUInt32());
13781383

13791384
stream.Write(buffer, 0, 4); // write the crc
13801385
}
@@ -1397,17 +1402,17 @@ private void WriteFrameDataChunk(Stream stream, uint sequenceNumber, Span<byte>
13971402

13981403
stream.Write(buffer);
13991404

1400-
Crc32 crc32 = new();
1401-
crc32.Append(buffer[4..]); // Write the type buffer
1405+
this.crc32.Reset();
1406+
this.crc32.Append(buffer[4..]); // Write the type buffer
14021407

14031408
if (data.Length > 0 && length > 0)
14041409
{
14051410
stream.Write(data, offset, length);
14061411

1407-
crc32.Append(data.Slice(offset, length));
1412+
this.crc32.Append(data.Slice(offset, length));
14081413
}
14091414

1410-
BinaryPrimitives.WriteUInt32BigEndian(buffer, crc32.GetCurrentHashAsUInt32());
1415+
BinaryPrimitives.WriteUInt32BigEndian(buffer, this.crc32.GetCurrentHashAsUInt32());
14111416

14121417
stream.Write(buffer, 0, 4); // write the crc
14131418
}

tests/ImageSharp.Benchmarks/Bulk/ToVector4_Rgba32.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
1313

14-
[Config(typeof(Config.ShortCore31))]
14+
[Config(typeof(Config.Short))]
1515
public class ToVector4_Rgba32 : ToVector4<Rgba32>
1616
{
1717
[Benchmark]

0 commit comments

Comments
 (0)