Repository navigation
Closed
Labels
Description
Are you using the latest version? Is the version currently in use as reported by npm ls sharp the same as the latest version as reported by npm view sharp dist-tags.latest?
Yes - 0.27.2
What are the steps to reproduce?
Converting a 4000px wide jpg to avif causes cpu to spike to 400% and takes 2.5gb of RAM vs a webp conversion taking 20% CPU and 200mb of RAM.
- It appears file size doesn't matter, only resolution. Tried with a 6MB jpg, and a 700k jpg as source, both 4000px wide
- This is happening on macOS and Debian
What is the expected behaviour?
Conversion to take up less RAM and CPU
Are you able to provide a minimal, standalone code sample, without other dependencies, that demonstrates this problem?
const avifOptions = {
quality: 50,
lossless: false,
speed: 8, // default is 5
chromaSubsampling: '4:2:0',
}
// AVif original
// -------------------------------------------------------------------------
await sharp(path.join(inputDirectory, file))
.toFormat('avif')
.avif(avifOptions)
.toFile(path.join(outputDirectory, `test.avif`))
// Avif half
// -------------------------------------------------------------------------
await sharp(path.join(inputDirectory, file))
.resize({ width: halfWidth })
.toFormat('avif')
.avif(avifOptions)
.toFile(path.join(outputDirectory, `[email protected]`))Are you able to provide a sample image that helps explain the problem?
What is the output of running npx envinfo --binaries --system?
macOS:
System:
OS: macOS 11.2
CPU: (8) x64 Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
Memory: 1.70 GB / 40.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 14.15.4 - ~/.nvm/versions/node/v14.15.4/bin/node
npm: 6.14.10 - ~/.nvm/versions/node/v14.15.4/bin/npm
Debian:
System:
OS: Linux 5.4 Debian GNU/Linux 9 (stretch) 9 (stretch)
CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 270.71 MB / 1.94 GB
Container: Yes
Shell: 4.4.12 - /bin/bash
Binaries:
Node: 12.18.2 - /usr/local/bin/node
Yarn: 1.22.4 - /usr/local/bin/yarn
npm: 6.14.5 - /usr/local/bin/npm
Reacted by Steven and Mrxbox98

Activity
lovell commentedon Feb 27, 2021
Yes, this is expected as AVIF compression is relatively slow and memory hungry compared with most other formats.
Using the following code sample and your test image:
...when run locally on Linux (x64 CPU with AVX2) via callgrind using locally compiled and installed libaom, libheif and libvips with symbols intact, I see the top 10 "hottest" functions are:
These are all from within libaom or invoked by it.
By setting
speedto 8, you're telling libaom to favour performance over all else. It's possible you might find that slowing it down also reduces memory consumption for some tasks.The prebuilt binaries provided by sharp use libaom as it is the reference de/encoder with good cross-platform support. There is a plan to replace it with the slightly faster dav1d (decode) and rav1e (encode) combination, but I'm unsure of their relative memory consumption.
jcontonio commentedon Mar 1, 2021
It seems any speed setting drives my CPU up to 400% and RAM to 2+GB. I think we'll probably ditch AVIF support in our app until that space matures a little more. Thanks for the detailed reply!
lovell commentedon Mar 2, 2021
No worries, please subscribe to #2604 for updates about the migration to dav1d+rav1e.
Disable AVIF by default
tom10271 commentedon Feb 2, 2023
Sharing some of the results we tested with different parameters for converting images to AVIF then store in S3 by Lambda function. We ran 10 times and secSpent is the average seconds spent. Just in case people that do not know, more memory Lambda have, more CPU credits we can spend in the lambda function.
The source image is 300KB with mainly gradient background with text on it, simply a banner. Setting effort to 0 and 1 are both fast, 2 is very slow already and 3+ are like 1 extra second per effort marginally added.
From 1 to 2 it further reduce the size by 25% which is amazing, from 1 to 9 the size would be reduced by 45% down from 20KB to 11KB. If effort 9 would run fast, this would be amazing as our company is paying huge price on CDN.
However, if it is a photographic image, raising effort the file size result in more or less the same.
alexmacarthur commentedon Feb 1, 2024
It doesn't seem like the resource demands for the AVIF conversion process have lessened much, but if anyone has any ideas on how I can improve performance, I'd love to hear them.
VM details:

Here's what converting to AVIF is doing:

avif conversion atm is meh: lovell/sharp#2597
avif conversion atm is meh: lovell/sharp#2597