SixLabors / ImageSharp

:camera: A modern, cross-platform, 2D Graphics library for .NET
https://sixlabors.com/products/imagesharp/
Other
7.42k stars 851 forks source link

WebP - Reduce the allocations in lossless encoding #2546

Closed JimBobSquarePants closed 1 year ago

JimBobSquarePants commented 1 year ago

Prerequisites

Description

This PR refactors the Vp8LHistogram type to use pooled allocations. It also introduces the concept of a Vp8LHistogramSet type to allow allocating a single buffer for a collection.

Benchmark indicates a 20ms speedup for lossless encoding while using less than half the original allocated memory.

BenchmarkDotNet=v0.13.0, OS=Windows 10.0.22621
11th Gen Intel Core i7-11370H 3.30GHz, 1 CPU, 8 logical and 4 physical cores
.NET SDK=8.0.100-rc.1.23455.8
  [Host]     : .NET 6.0.22 (6.0.2223.42425), X64 RyuJIT
  Job-ZADQXC : .NET 6.0.22 (6.0.2223.42425), X64 RyuJIT

Runtime=.NET 6.0  Arguments=/p:DebugType=portable  IterationCount=3
LaunchCount=1  WarmupCount=3

Main

Method TestImage Mean Error StdDev Ratio RatioSD Gen 0 Gen 1 Gen 2 Allocated
'Magick Webp Lossy' Png/Bike.png 22.30 ms 5.533 ms 0.303 ms 0.14 0.00 - - - 68 KB
'ImageSharp Webp Lossy' Png/Bike.png 74.82 ms 2.291 ms 0.126 ms 0.46 0.00 2428.5714 142.8571 - 15,187 KB
'Magick Webp Lossless' Png/Bike.png 161.16 ms 22.093 ms 1.211 ms 1.00 0.00 - - - 518 KB
'ImageSharp Webp Lossless' Png/Bike.png 249.44 ms 58.373 ms 3.200 ms 1.55 0.02 8000.0000 4000.0000 2000.0000 46,532 KB

PR

Method TestImage Mean Error StdDev Ratio RatioSD Gen 0 Gen 1 Gen 2 Allocated
'Magick Webp Lossy' Png/Bike.png 22.20 ms 5.392 ms 0.296 ms 0.13 0.00 - - - 68 KB
'ImageSharp Webp Lossy' Png/Bike.png 76.90 ms 61.754 ms 3.385 ms 0.46 0.01 2428.5714 142.8571 - 15,187 KB
'Magick Webp Lossless' Png/Bike.png 165.44 ms 85.798 ms 4.703 ms 1.00 0.00 - - - 518 KB
'ImageSharp Webp Lossless' Png/Bike.png 228.72 ms 40.127 ms 2.199 ms 1.38 0.05 3000.0000 2000.0000 2000.0000 20,623 KB
JimBobSquarePants commented 1 year ago

@antonfirsov I want to get this merged so it's not blocking #2569

None of the changes here represent security enhancements so there's nothing to backport. Any future ones can be manually handled.