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

Improves caching lookup performance #773

Merged
merged 1 commit into from
Jan 3, 2025
Merged

Conversation

pharris2411
Copy link
Collaborator

@pharris2411 pharris2411 commented Jan 3, 2025

I've been tracking down some performance issues in our codebase and found a somewhat extreme example where this block of code is eating up around 5 seconds of compute time.

The circumstances are:

  1. Our Rabl.configuration.view_paths has over 500 paths in it
  2. I'm testing a index operation that outputs data for about 3600 ActiveRecord objects
  3. The Rabl main file has around 10 extends, and they nest even further

Even with caching enabled, it still takes nearly 5 seconds to generate all the cache keys as each extends allocates an array with 500+ elements and then concatenates it to generate the cache key. In this flame graph you can see Rabl::source_cache takes a total of 4.84 seconds in this trace and it's all down to Array#join

Screenshot 2024-12-09 at 2 22 52 PM (2)

This change removes the Rabl.configuration.view_paths from the path passed in to source_cache so that only the custom view_path (if specified) is considered for part of the key. As long as Rabl.configuration.view_paths is only configured once at application start up and not modified on the fly, this should ultimately result in the same caching key behavior but with a lot less info needed to generate each key.

After this change the same request spends just 35 ms total executing Rabl.source_cache.

Screenshot 2025-01-03 at 5 16 38 PM

All existing tests passed, I wasn't sure how else to test this if further tests are needed.

@nesquena
Copy link
Owner

nesquena commented Jan 3, 2025

Thank you!

@nesquena nesquena merged commit f66fc1f into nesquena:master Jan 3, 2025
6 of 14 checks passed
@nesquena
Copy link
Owner

nesquena commented Jan 3, 2025

I’ve also invited you as a collaborator to the repo, thanks for contributing 🙏

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.

2 participants