From e168ae6a2c8bb4774c871f1372bab4d7f8051b3d Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Tue, 26 Oct 2021 16:42:23 +0200 Subject: [PATCH] Use Span in GetHTreeGroupForPos to avoid allocations --- .../Formats/Webp/Lossless/WebpLosslessDecoder.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs b/src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs index 960416009..768365e44 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs @@ -218,7 +218,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless ColorCache colorCache = decoder.Metadata.ColorCache; int colorCacheLimit = lenCodeLimit + colorCacheSize; int mask = decoder.Metadata.HuffmanMask; - HTreeGroup[] hTreeGroup = GetHTreeGroupForPos(decoder.Metadata, col, row); + Span hTreeGroup = GetHTreeGroupForPos(decoder.Metadata, col, row); int totalPixels = width * height; int decodedPixels = 0; @@ -731,7 +731,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless int lastRow = height; const int lenCodeLimit = WebpConstants.NumLiteralCodes + WebpConstants.NumLengthCodes; int mask = hdr.HuffmanMask; - HTreeGroup[] htreeGroup = pos < last ? GetHTreeGroupForPos(hdr, col, row) : null; + Span htreeGroup = pos < last ? GetHTreeGroupForPos(hdr, col, row) : null; while (!this.bitReader.Eos && pos < last) { // Only update when changing tile. @@ -815,7 +815,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless decoder.Metadata.HuffmanMask = numBits == 0 ? ~0 : (1 << numBits) - 1; } - private uint ReadPackedSymbols(HTreeGroup[] group, Span pixelData, int decodedPixels) + private uint ReadPackedSymbols(Span group, Span pixelData, int decodedPixels) { uint val = (uint)(this.bitReader.PrefetchBits() & (HuffmanUtils.HuffmanPackedTableSize - 1)); HuffmanCode code = group[0].PackedTable[val]; @@ -895,10 +895,10 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless } [MethodImpl(InliningOptions.ShortMethod)] - private static HTreeGroup[] GetHTreeGroupForPos(Vp8LMetadata metadata, int x, int y) + private static Span GetHTreeGroupForPos(Vp8LMetadata metadata, int x, int y) { uint metaIndex = GetMetaIndex(metadata.HuffmanImage, metadata.HuffmanXSize, metadata.HuffmanSubSampleBits, x, y); - return metadata.HTreeGroups.AsSpan((int)metaIndex).ToArray(); + return metadata.HTreeGroups.AsSpan((int)metaIndex); } [MethodImpl(InliningOptions.ShortMethod)]