Browse Source

Use Span version of Sort() to reduce allocations

pull/1799/head
Brian Popow 5 years ago
parent
commit
ed8d2afcb0
  1. 5
      src/ImageSharp/Formats/Webp/Lossless/HuffmanUtils.cs
  2. 5
      src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs

5
src/ImageSharp/Formats/Webp/Lossless/HuffmanUtils.cs

@ -202,9 +202,14 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
}
// Build the Huffman tree.
#if NET5_0_OR_GREATER
Span<HuffmanTree> treeSlice = tree.AsSpan().Slice(0, treeSize);
treeSlice.Sort(HuffmanTree.Compare);
#else
HuffmanTree[] treeCopy = tree.AsSpan().Slice(0, treeSize).ToArray();
Array.Sort(treeCopy, HuffmanTree.Compare);
treeCopy.AsSpan().CopyTo(tree);
#endif
if (treeSize > 1)
{

5
src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs

@ -1204,9 +1204,14 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
return false;
}
#if NET5_0_OR_GREATER
var paletteSlice = palette.Slice(0, this.PaletteSize);
paletteSlice.Sort();
#else
uint[] paletteArray = palette.Slice(0, this.PaletteSize).ToArray();
Array.Sort(paletteArray);
paletteArray.CopyTo(palette);
#endif
if (PaletteHasNonMonotonousDeltas(palette, this.PaletteSize))
{

Loading…
Cancel
Save