Skip to content

Commit

Permalink
Use Span in GetHTreeGroupForPos to avoid allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
brianpopow committed Oct 26, 2021
1 parent d3f3eec commit e168ae6
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public void DecodeImageData(Vp8LDecoder decoder, Span<uint> pixelData)
ColorCache colorCache = decoder.Metadata.ColorCache;
int colorCacheLimit = lenCodeLimit + colorCacheSize;
int mask = decoder.Metadata.HuffmanMask;
HTreeGroup[] hTreeGroup = GetHTreeGroupForPos(decoder.Metadata, col, row);
Span<HTreeGroup> hTreeGroup = GetHTreeGroupForPos(decoder.Metadata, col, row);

int totalPixels = width * height;
int decodedPixels = 0;
Expand Down Expand Up @@ -731,7 +731,7 @@ public void DecodeAlphaData(AlphaDecoder dec)
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> htreeGroup = pos < last ? GetHTreeGroupForPos(hdr, col, row) : null;
while (!this.bitReader.Eos && pos < last)
{
// Only update when changing tile.
Expand Down Expand Up @@ -815,7 +815,7 @@ private void UpdateDecoder(Vp8LDecoder decoder, int width, int height)
decoder.Metadata.HuffmanMask = numBits == 0 ? ~0 : (1 << numBits) - 1;
}

private uint ReadPackedSymbols(HTreeGroup[] group, Span<uint> pixelData, int decodedPixels)
private uint ReadPackedSymbols(Span<HTreeGroup> group, Span<uint> pixelData, int decodedPixels)
{
uint val = (uint)(this.bitReader.PrefetchBits() & (HuffmanUtils.HuffmanPackedTableSize - 1));
HuffmanCode code = group[0].PackedTable[val];
Expand Down Expand Up @@ -895,10 +895,10 @@ private int GetCopyDistance(int distanceSymbol)
}

[MethodImpl(InliningOptions.ShortMethod)]
private static HTreeGroup[] GetHTreeGroupForPos(Vp8LMetadata metadata, int x, int y)
private static Span<HTreeGroup> 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)]
Expand Down

0 comments on commit e168ae6

Please sign in to comment.