Skip to content
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

Merged
merged 2 commits into from
Sep 4, 2024

Conversation

wtchnm
Copy link
Contributor

@wtchnm wtchnm commented Sep 2, 2024

Changes

Moves the loadImage method call to after the image cache check, improving performance massively and reducing memory consumption for remote images.

Website Version No cache Cache
brpartners.com.br Before Before, no cache, 129.47s Before, cache, 44.23s
brpartners.com.br After After, no cache, 126.67s After, cache, 0,56s
erika.florist Before Before, no cache, 47.20s Before, cache, 0.54s
erika.florist After After, no cache, 47.29s After, cache, 0,25s

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.

Copy link

changeset-bot bot commented Sep 2, 2024

🦋 Changeset detected

Latest 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

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Sep 2, 2024
@@ -207,6 +200,7 @@ export async function generateImagesForPath(
? (options.src as ImageMetadata).src
: (options.src as string);

const originalImage = await loadImage(originalFilePath, env);
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

@Princesseuh Princesseuh Sep 2, 2024

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks!

Copy link
Contributor Author

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.

@wtchnm wtchnm marked this pull request as draft September 2, 2024 21:13
@wtchnm wtchnm marked this pull request as ready for review September 3, 2024 01:18
Copy link
Member

@Princesseuh Princesseuh left a 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.

@Princesseuh Princesseuh merged commit ca54e3f into withastro:main Sep 4, 2024
12 of 13 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants