Prerequisites
ImageSharp version
3.1.8
Environment (Operating system, version and so on)
Windows 11 with latest updates
.NET Framework version
.NET 9
Description
Lossless WebP encoder allocates both a lot of small object and also a few big chunks, which are NOT reused.
It is about 500 K objects and 200-300 MB total memory to encode single UWQHD (3440x1440) image.
It leads to very high pressure on GC. Especially, if there are many images to process.
My encoding settings are probably the lowest for lossless saving:
WebpEncoder {
FileFormat = WebpFileFormatType.Lossless,
Quality = 0,
TransparentColorMode = WebpTransparentColorMode.Preserve,
}
I found the following hot paths (for one image):


PixOrCopy.CreateLiteral produces TONS of small objects.
Vp8LEncoder's constructor may not look that scary, but let's sequentially encode 10 images:


As you can see, those big chunks are not reused in the future.
I tried to provide a custom memory allocator. It didn't help.
I suppose the allocator is totally ignored there.
Fixing these two moments should greatly accelerate the WebP encoder:
PixOrCopy.CreateLiteral problem is probably the hardest one as it requires changing of data structures.
- Changing
Vp8LEncoder should be much easier.
Steps to Reproduce
Just encode an image with performance profiler and object allocation tracking.
Prerequisites
DEBUGandRELEASEmodeImageSharp version
3.1.8
Environment (Operating system, version and so on)
Windows 11 with latest updates
.NET Framework version
.NET 9
Description
Lossless WebP encoder allocates both a lot of small object and also a few big chunks, which are NOT reused.
It is about 500 K objects and 200-300 MB total memory to encode single UWQHD (3440x1440) image.
It leads to very high pressure on GC. Especially, if there are many images to process.
My encoding settings are probably the lowest for lossless saving:
I found the following hot paths (for one image):


PixOrCopy.CreateLiteralproduces TONS of small objects.Vp8LEncoder's constructor may not look that scary, but let's sequentially encode 10 images:As you can see, those big chunks are not reused in the future.
I tried to provide a custom memory allocator. It didn't help.
I suppose the allocator is totally ignored there.
Fixing these two moments should greatly accelerate the WebP encoder:
PixOrCopy.CreateLiteralproblem is probably the hardest one as it requires changing of data structures.Vp8LEncodershould be much easier.Steps to Reproduce
Just encode an image with performance profiler and object allocation tracking.