diff --git a/src/ImageSharp/Metadata/Profiles/IPTC/IptcProfile.cs b/src/ImageSharp/Metadata/Profiles/IPTC/IptcProfile.cs
index 0a6831e9b..ec3eed265 100644
--- a/src/ImageSharp/Metadata/Profiles/IPTC/IptcProfile.cs
+++ b/src/ImageSharp/Metadata/Profiles/IPTC/IptcProfile.cs
@@ -26,11 +26,6 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Iptc
///
private const byte IptcEnvelopeCodedCharacterSet = 0x5A;
- ///
- /// This value marks that UTF-8 encoding is used in application records.
- ///
- private static readonly byte[] CodedCharacterSetUtf8Value = { 0x1B, 0x25, 0x47 };
-
///
/// Initializes a new instance of the class.
///
@@ -75,6 +70,11 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Iptc
}
}
+ ///
+ /// Gets a byte array marking that UTF-8 encoding is used in application records.
+ ///
+ private static ReadOnlySpan CodedCharacterSetUtf8Value => new byte[] { 0x1B, 0x25, 0x47 }; // Uses C#'s optimization to refer to the data segment in the assembly directly, no allocation occurs.
+
///
/// Gets the byte data of the IPTC profile.
///
@@ -291,16 +291,18 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Iptc
}
}
- private int WriteRecord(int offset, byte[] recordData, IptcRecordNumber recordNumber, byte recordBinaryRepresentation)
+ private int WriteRecord(int offset, ReadOnlySpan recordData, IptcRecordNumber recordNumber, byte recordBinaryRepresentation)
{
- this.Data[offset++] = IptcTagMarkerByte;
- this.Data[offset++] = (byte)recordNumber;
- this.Data[offset++] = recordBinaryRepresentation;
- this.Data[offset++] = (byte)(recordData.Length >> 8);
- this.Data[offset++] = (byte)recordData.Length;
+ Span data = this.Data.AsSpan(offset, 5);
+ data[0] = IptcTagMarkerByte;
+ data[1] = (byte)recordNumber;
+ data[2] = recordBinaryRepresentation;
+ data[3] = (byte)(recordData.Length >> 8);
+ data[4] = (byte)recordData.Length;
+ offset += 5;
if (recordData.Length > 0)
{
- Buffer.BlockCopy(recordData, 0, this.Data, offset, recordData.Length);
+ recordData.CopyTo(this.Data.AsSpan(offset));
offset += recordData.Length;
}