Skip to content

Cache ActionView::FixtureResolvers created by stub_template #1979

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

Merged
merged 2 commits into from
Apr 5, 2018

Conversation

urbanautomaton
Copy link
Contributor

@urbanautomaton urbanautomaton commented Mar 30, 2018

The #stub_template view spec helper works by prepending an ActionView::FixtureResolver to the view instance's view_paths. This intercepts queries for specific template files, providing a canned response that's used to instantiate a dummy ActionView::Template.

These canned responses still require compilation and finalization, however. As the resolver holds all the references to ActionView::Template instances, and a new resolver is created for each stub in every example, this means that a single stub_template call in a before block covering 20 examples will result in the same template being compiled and finalized 20 times.

Both compilation and finalization can be expensive. To avoid this expense where possible, this change adds a global resolver cache for stubbed templates.

By way of anecdote ('cos everyone loves those), this dropped our ~4k example view spec suite with ~500 stub_template calls from 6 min to 2 min total runtime.

If there's anything you'd like done differently, or if there are any negative consequences of this change I haven't thought of, just let me know; I'll be happy to update the PR accordingly.

Cheers,
Simon

edit: ugh, the only build job that failed looks transient...

I'd like to add caching to this method to avoid a performance problem
caused by repeated compilation of stubbed template contents.

Since there aren't currently any unit tests around it, this adds a
simple spec to make sure basic behaviour is preserved.
module StubResolverCache
def self.resolver_for(hash)
@resolvers ||= {}
@resolvers[hash] ||= ActionView::FixtureResolver.new(hash)
Copy link
Member

Choose a reason for hiding this comment

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

My only concern is that this introduces shared state across tests (I know this is by design) but if we were to freeze this object it wouldn't be mutable which would alleviate this concern

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, that's a straightforward change to make, have pushed with the extra .freeze. 👍

However, there's some further mutable internal state in the resolver that I don't think we'll be able to freeze; it only builds and caches Template instances when it receives queries for a specific path, so this happens on first render, not when we're instantiating the resolver.

My intuition is that this shared state should be safe, though. Since we're indexing the cache on the full stub definition (i.e. filename and stubbed contents), and the output of a compiled template should be a function of its contents, I don't think there's any way for the cache to return incorrect values.

@urbanautomaton urbanautomaton force-pushed the cache-stub-template-resolvers branch from fabcd51 to 309885a Compare March 31, 2018 11:26
@urbanautomaton
Copy link
Contributor Author

Thanks for the feedback @JonRowe - I've made the requested change.

I also got bored and wrote a small repro that constructs a pathological case for the performance problem the caching addresses. The caching turns a 4 minute run into a 13s one, which is nice. 😄

@urbanautomaton
Copy link
Contributor Author

Ah; it seems that freezing the FixtureResolver isn't an option in Rails 3.0.20 / ruby 1.9 (Failing jobs 1, 2).

On the grounds that freezing it doesn't actually prevent its internal state from being mutated anyway, I'd be tempted to argue that it's okay to leave it unfrozen, for the reasons expressed earlier. By way of comparison, the OptimizedFileSystemResolver instances that constitute the default view_paths are already shared between examples, seemingly without problems. What do you think?

@JonRowe
Copy link
Member

JonRowe commented Apr 3, 2018

My hesitation is I have no idea if there will be any unintended consequences for allowing it to be mutated, I believe our OptimisedFileSystemResolver took it into consideration from the outset. If you're more familiar with the FixtureResolver internals to say it won't be a problem I'm happy to accept your judgement :)

The `#stub_template` view spec helper works by prepending an
ActionView::FixtureResolver[1] to the view instance's view_paths. This
intercepts queries for specific template files, providing a canned
response that's used to instantiate a dummy ActionView::Template.

These templates still require compilation and finalization[2], however.
As a new resolver is created for each stub in every example, this means
that a single `stub_template` call in a `before` block that covers 20
examples will result in the same template being compiled and finalized
20 times.

Both compilation and finalization can be expensive, so to avoid this
where possible, this change adds a global resolver cache for stubbed
templates.

[1] https://github.com/rails/rails/blob/9f65d2a08bc80a94bbb2c0b6e00957c7059aed25/actionview/lib/action_view/testing/resolvers.rb#L6-L9
[2] https://github.com/rails/rails/blob/9f65d2a08bc80a94bbb2c0b6e00957c7059aed25/actionview/lib/action_view/template.rb#L118-L126
@urbanautomaton urbanautomaton force-pushed the cache-stub-template-resolvers branch from 309885a to 3666c35 Compare April 3, 2018 07:29
@urbanautomaton
Copy link
Contributor Author

I definitely share your concerns; it'd be nice to have more confidence than just a code reading to assure us this is safe.

From the code reading perspective: the only significant internal state to both ActionView::OptimizedFileSystemResolver and ActionView::FixtureResolver is the resolver's template cache itself. This is built into the base ActionView::Resolver class from which both resolvers ultimately inherit, so my understanding is that there shouldn't be any difference, state-wise, in behaviour between the two resolvers; fundamentally they only differ in how they map input paths to template source strings, and the rest of their functionality is shared. So any state mutation problem that affects FixtureResolver ought to affect OptimizedFileSystemResolver too. FWIW the latter is defined in Rails itself, so as best I can tell it's not had any specific attention paid to this issue.

So I'm pretty confident it's okay, but that's obviously not the same as "there can't be a problem".

I've monkey-patched this change into the app I'm currently working on. While this wouldn't conclusively prove the absence of problems either, would it be worth sitting on this PR for a week or so to see if any issues crop up in our heavily-trafficked build?

@urbanautomaton
Copy link
Contributor Author

Okeydoke - thanks very much! ❤️

@JonRowe JonRowe merged commit 47134af into rspec:master Apr 5, 2018
@urbanautomaton urbanautomaton deleted the cache-stub-template-resolvers branch April 5, 2018 07:23
JonRowe added a commit that referenced this pull request Apr 6, 2018
sebjacobs pushed a commit to futurelearn/rspec-rails that referenced this pull request Mar 15, 2019
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