From 2491b6ab626f4329a40b9363e4058d5857d8567d Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Tue, 1 Feb 2022 16:16:35 +0100 Subject: [PATCH] Change AverageBytesPerMb to ReadOnlySpan --- src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs index 927b04c0cf..695359e5ea 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs @@ -73,8 +73,6 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy private readonly bool alphaCompression; - private readonly byte[] averageBytesPerMb = { 50, 24, 16, 9, 7, 5, 3, 2 }; - private const int NumMbSegments = 4; private const int MaxItersKMeans = 6; @@ -174,6 +172,9 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy this.ResetBoundaryPredictions(); } + // This uses C#'s optimization to refer to the static data segment of the assembly, no allocation occurs. + private static ReadOnlySpan AverageBytesPerMb => new byte[] { 50, 24, 16, 9, 7, 5, 3, 2 }; + public int BaseQuant { get; set; } /// @@ -319,7 +320,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy this.SetLoopParams(this.quality); // Initialize the bitwriter. - int averageBytesPerMacroBlock = this.averageBytesPerMb[this.BaseQuant >> 4]; + int averageBytesPerMacroBlock = AverageBytesPerMb[this.BaseQuant >> 4]; int expectedSize = this.Mbw * this.Mbh * averageBytesPerMacroBlock; this.bitWriter = new Vp8BitWriter(expectedSize, this);