-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf(astro/assets): avoid downloading original image when using cache #11904
Conversation
🦋 Changeset detectedLatest commit: de53cab The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@@ -207,6 +200,7 @@ export async function generateImagesForPath( | |||
? (options.src as ImageMetadata).src | |||
: (options.src as string); | |||
|
|||
const originalImage = await loadImage(originalFilePath, env); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a tradeoff here with this that I made somewhat on purpose, this massively improve fully cached runs, especially with remote images, but it makes it way worse for initial builds with a lot of variants of images. Ideally we'd be able to come to a perfect solution, but I couldn't figure it out when I first did it though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your input @Princesseuh! I’ll mark this PR as draft and try to investigate this scenario. Do you have any idea why it made it worse?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because you end up loading the image for every transform of an image. For instance, if you have an image of which you generate 16 variations (which is quite easy to do with Picture), you'll end up loading the image 17 times.
Probably that a refactor here would be possible where we check if every variants is cached first, if so skip loading the image
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Princesseuh, I've implemented your suggestions and updated the screenshots to include a non-cached run.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in theory, due to concurrent running there's still some cases where this will cause the image to be loaded multiple times, but perhaps it's not too bad if it improves cached builds in most cases.
Changes
Moves the
loadImage
method call to after the image cache check, improving performance massively and reducing memory consumption for remote images.Testing
Manual testing was performed in my company's website, with 950 optimized images, and in erika.florist website, with 626 images.
Docs
N/A.