Skip to content

Lossless WebP encoder allocates A LOT #2934

Closed
@SladeThe

Description

@SladeThe

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

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):
Image
Image

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

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:

  1. PixOrCopy.CreateLiteral problem is probably the hardest one as it requires changing of data structures.
  2. Changing Vp8LEncoder should be much easier.

Steps to Reproduce

Just encode an image with performance profiler and object allocation tracking.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions