diff --git a/src/ImageSharp/Formats/WebP/BitWriter/Vp8LBitWriter.cs b/src/ImageSharp/Formats/WebP/BitWriter/Vp8LBitWriter.cs index 139eebf9a1..eb25027204 100644 --- a/src/ImageSharp/Formats/WebP/BitWriter/Vp8LBitWriter.cs +++ b/src/ImageSharp/Formats/WebP/BitWriter/Vp8LBitWriter.cs @@ -13,6 +13,11 @@ namespace SixLabors.ImageSharp.Formats.WebP.BitWriter /// internal class Vp8LBitWriter : BitWriterBase { + /// + /// A scratch buffer to reduce allocations. + /// + private readonly byte[] scratchBuffer = new byte[8]; + /// /// This is the minimum amount of size the memory buffer is guaranteed to grow when extra space is needed. /// @@ -166,7 +171,9 @@ namespace SixLabors.ImageSharp.Formats.WebP.BitWriter this.BitWriterResize(extraSize); } - BinaryPrimitives.WriteUInt64LittleEndian(this.Buffer.AsSpan(this.cur), this.bits); + BinaryPrimitives.WriteUInt64LittleEndian(this.scratchBuffer, this.bits); + this.scratchBuffer.AsSpan(0, 4).CopyTo(this.Buffer.AsSpan(this.cur)); + this.cur += WriterBytes; this.bits >>= WriterBits; this.used -= WriterBits;