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

Memoize resource URLs for major performance gain #915

Merged
merged 2 commits into from
Sep 6, 2024

Conversation

jaredcwhite
Copy link
Member

@jaredcwhite jaredcwhite commented Sep 6, 2024

Resolves #914

Awesome detective work by @MaxLap, we're able to get full build performance gains of 15-25% or maybe even higher! Apparently the code to determine relative/absolute URLs for resources is somewhat expensive, and repeating that constantly throughout template rendering really adds up. This PR memoizes the values upon resource read, and resets them if there's a fast refresh (in case the permalink was changed or whatever).

Copy link

render bot commented Sep 6, 2024

@jaredcwhite jaredcwhite added this to the 2.0 milestone Sep 6, 2024
Copy link

render bot commented Sep 6, 2024

@MaxLap
Copy link

MaxLap commented Sep 6, 2024

Glad you are seeing benefits too!

A bit pedantic, but the way this is implemented feel more like a pre-calculation than a memoization ;). I don't know the overall flow of the system, but since the place where you set the instance variables is not the place where you produce the value nor the place where you need it, it feels like the values could sometimes be calculated for nothing.

Visually, using ||= directly in the relative_url and absolute_url is a much more common pattern. So much so I thought it was a typo when I first saw it.

@jaredcwhite
Copy link
Member Author

@MaxLap You're right, I was experimenting with different patterns and testing the outcome, and I landed on that one without it needing to be like that. I've updated to ||=. 🙏

@jaredcwhite jaredcwhite merged commit 1194708 into main Sep 6, 2024
3 checks passed
@jaredcwhite jaredcwhite deleted the memoize-resource-urls branch September 6, 2024 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memoize relative_url for performance gains
2 participants