Improved performance of flush_cache and prime_cache when parameters are passed to them. #2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The more methods defined on the class and the more methods that are asked to flush, the slower the code becomes because Array Math is performed in a loop. The attached pull requests minimizes this.
From the original rails ticket from @Fryguy: https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/5517-activesupportmemoizable-flush_cache-is-very-slow4#ticket-5517-3:
"After more playing around with this, I realized the issue is not with calling methods + private_methods + protected_methods directly, but instead they are being called IN A LOOP of the parameters passed in. So, if you pass, for example, 20 method names to flush cache, it will call methods + private_methods + protected_methods 20 times. This is why I was seeing such horrible performance. flush_cache by itself wasn't the problem, it was flush_cache with parameters...the more the worse."
Before patch:
After patch:
See this branch for a slow test example without this optimization: https://github.com/ManageIQ/memoist/tree/slow_test_without_optimization
And here's a branch with the same test and the optimization:
https://github.com/ManageIQ/memoist/tree/flush_and_prime_cache_optimization_with_slow_test_example