image-rs / image

Encoding and decoding images in Rust
Apache License 2.0
4.93k stars 610 forks source link

ImageReader::open and write_to performance issue #2271

Closed nricciardi closed 3 months ago

nricciardi commented 3 months ago

This is not pure bug (maybe). ImageReader::open and image.write_to spent a lot of time. In my Rust project (custom Markdown compiler) run 50% of the execution time despite there are only 20 images on more than 5000 line that must be parsed.

Maybe open both operations can be improved or there are inefficient algorithms behind.

fintelia commented 3 months ago

Make sure you are compiling in release mode / with optimizations. Beyond that, this issue really isn't actionable without a lot more detail

PR's for specific optimizations are of course welcome!

nricciardi commented 3 months ago

Which details are needed?

However, I compile my application in release mode (optimization 3).

I will try to inspect code...

fintelia commented 3 months ago

To start with it would help to know which image format(s) you're dealing with. But even that isn't much to go on. Many of our encoders/decoders are thousands of lines of code and have already had a significant amount of optimization. In some cases they are slow primarily just due to the complexity of the format

The most actionable things would probably be providing specific image files that decode much faster with other libraries or profiling data identifying particular slow sections that could be improved

nricciardi commented 3 months ago

I always encode in PNG (output format, which then is encoded in base64 to be printed in html). I wanted to encode in AVIF (I need a transparent background format), but it is not out-of-the-box format...

fintelia commented 3 months ago

The PNG encoder is already heavily optimized. AVIF as well, but you'll want to enable the nasm feature for best performance

nricciardi commented 3 months ago

Thank you, but, then I don't know how open and write_to are so a time consuming tasks...

Anyway, how can I use AVIF and what is nasm feature?

fintelia commented 3 months ago

The functions are time consuming because they do a lot of work. Just because each step is efficient doesn't mean it still won't take a while to complete.

You can encode to AVIF the same way you're currently doing for PNG, just change the file extension or pass ImageFormat::Avif as applicable. The nasm feature enables the asm feature on rav1e which you can read about in their readme

nricciardi commented 3 months ago

Thank you, I will try it!