Skip to content

Commit 2491b6a

Browse files
committed
Change AverageBytesPerMb to ReadOnlySpan<byte>
1 parent 192cfb0 commit 2491b6a

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ internal class Vp8Encoder : IDisposable
7373

7474
private readonly bool alphaCompression;
7575

76-
private readonly byte[] averageBytesPerMb = { 50, 24, 16, 9, 7, 5, 3, 2 };
77-
7876
private const int NumMbSegments = 4;
7977

8078
private const int MaxItersKMeans = 6;
@@ -174,6 +172,9 @@ public Vp8Encoder(
174172
this.ResetBoundaryPredictions();
175173
}
176174

175+
// This uses C#'s optimization to refer to the static data segment of the assembly, no allocation occurs.
176+
private static ReadOnlySpan<byte> AverageBytesPerMb => new byte[] { 50, 24, 16, 9, 7, 5, 3, 2 };
177+
177178
public int BaseQuant { get; set; }
178179

179180
/// <summary>
@@ -319,7 +320,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
319320
this.SetLoopParams(this.quality);
320321

321322
// Initialize the bitwriter.
322-
int averageBytesPerMacroBlock = this.averageBytesPerMb[this.BaseQuant >> 4];
323+
int averageBytesPerMacroBlock = AverageBytesPerMb[this.BaseQuant >> 4];
323324
int expectedSize = this.Mbw * this.Mbh * averageBytesPerMacroBlock;
324325
this.bitWriter = new Vp8BitWriter(expectedSize, this);
325326

0 commit comments

Comments
 (0)