Skip to content

Commit 5d65ef0

Browse files
committed
Reduced intermediate allocations: Profiles
1 parent b9b6f72 commit 5d65ef0

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ private void GetThumbnail(uint offset)
8686
/// </summary>
8787
internal abstract class BaseExifReader
8888
{
89-
private readonly byte[] buf8 = new byte[8];
90-
private readonly byte[] buf4 = new byte[4];
91-
private readonly byte[] buf2 = new byte[2];
92-
9389
private readonly MemoryAllocator? allocator;
9490
private readonly Stream data;
9591
private List<ExifTag>? invalidTags;
@@ -528,20 +524,33 @@ private bool TryReadSpan(Span<byte> span)
528524
return read == length;
529525
}
530526

531-
protected ulong ReadUInt64() =>
532-
this.TryReadSpan(this.buf8)
533-
? this.ConvertToUInt64(this.buf8)
527+
protected ulong ReadUInt64()
528+
{
529+
Span<byte> buffer = stackalloc byte[8];
530+
531+
return this.TryReadSpan(buffer)
532+
? this.ConvertToUInt64(buffer)
534533
: default;
534+
}
535535

536536
// Known as Long in Exif Specification.
537-
protected uint ReadUInt32() =>
538-
this.TryReadSpan(this.buf4)
539-
? this.ConvertToUInt32(this.buf4)
537+
protected uint ReadUInt32()
538+
{
539+
Span<byte> buffer = stackalloc byte[4];
540+
541+
return this.TryReadSpan(buffer)
542+
? this.ConvertToUInt32(buffer)
540543
: default;
544+
}
541545

542-
protected ushort ReadUInt16() => this.TryReadSpan(this.buf2)
543-
? this.ConvertToShort(this.buf2)
546+
protected ushort ReadUInt16()
547+
{
548+
Span<byte> buffer = stackalloc byte[2];
549+
550+
return this.TryReadSpan(buffer)
551+
? this.ConvertToShort(buffer)
544552
: default;
553+
}
545554

546555
private long ConvertToInt64(ReadOnlySpan<byte> buffer)
547556
{

src/ImageSharp/Metadata/Profiles/Exif/Values/ExifByteArray.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ public override bool TrySetValue(object? value)
4545

4646
private bool TrySetSignedIntArray(int[] intArrayValue)
4747
{
48-
if (Array.FindIndex(intArrayValue, x => x < byte.MinValue || x > byte.MaxValue) > -1)
48+
if (Array.FindIndex(intArrayValue, x => (uint)x > byte.MaxValue) >= 0)
4949
{
5050
return false;
5151
}
5252

53-
var value = new byte[intArrayValue.Length];
53+
byte[] value = new byte[intArrayValue.Length];
5454
for (int i = 0; i < intArrayValue.Length; i++)
5555
{
5656
int s = intArrayValue[i];

src/ImageSharp/Metadata/Profiles/ICC/IccProfile.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ public static IccProfileId CalculateHash(byte[] data)
108108
const int profileIdPos = 84;
109109

110110
// need to copy some values because they need to be zero for the hashing
111-
byte[] temp = new byte[24];
112-
Buffer.BlockCopy(data, profileFlagPos, temp, 0, 4);
113-
Buffer.BlockCopy(data, renderingIntentPos, temp, 4, 4);
114-
Buffer.BlockCopy(data, profileIdPos, temp, 8, 16);
111+
Span<byte> temp = stackalloc byte[24];
112+
data.AsSpan(profileFlagPos, 4).CopyTo(temp);
113+
data.AsSpan(renderingIntentPos, 4).CopyTo(temp.Slice(4));
114+
data.AsSpan(profileIdPos, 16).CopyTo(temp.Slice(8));
115115

116116
try
117117
{
@@ -131,9 +131,9 @@ public static IccProfileId CalculateHash(byte[] data)
131131
}
132132
finally
133133
{
134-
Buffer.BlockCopy(temp, 0, data, profileFlagPos, 4);
135-
Buffer.BlockCopy(temp, 4, data, renderingIntentPos, 4);
136-
Buffer.BlockCopy(temp, 8, data, profileIdPos, 16);
134+
temp.Slice(0, 4).CopyTo(data.AsSpan(profileFlagPos));
135+
temp.Slice(4, 4).CopyTo(data.AsSpan(renderingIntentPos));
136+
temp.Slice(8, 16).CopyTo(data.AsSpan(profileIdPos));
137137
}
138138
}
139139

0 commit comments

Comments
 (0)