Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
speed up rails 5.0 feature specs by caching templates during suite
# spec/support/disable_template_cache_clearing
# may also need to configure, either here
Rails.application.config.action_view.cache_template_loading = true
# or in config/environments/test.rb:
# config.action_view.cache_template_loading = true
# ensure ActionView::Digestor is already autoloaded before patching it
# stop the PerExecutionDigestCacheExpiry actually clearing the cache
module ActionView
class Digestor
module PerExecutionDigestCacheExpiry
def self.before(target)
# not clearing cache now
# ensure view context is cleared once before each test run
RSpec.configure do |config|
config.before(:suite) {

This comment has been minimized.

Copy link
Owner Author

@timdiggins timdiggins commented Jan 29, 2018


After the upgrade of a rails app from 4.2 to 5.0.6, the performance of features tests went slower by 50-100% and overall test suite went from 11min +/-1min to 17min +/-1min (with parallelism). The app had some STI and abstract model inheritance and plenty of views, and a preponderance of feature tests.


Collected stats on speeds of the suite for 4.2 and 5.0.6 and compared significant changes in different specs and determined that the features were the ones with the significant change. Used stackprof ( with wall-time sampling) on a small set of feature specs and found that the top three items on the list were:

     26432  (16.8%)       26432  (16.8%)     (garbage collection)
      9921   (6.3%)        9921   (6.3%)     ActionView::PathResolver#find_template_paths
     40713  (25.9%)        7433   (4.7%)     ActionView::Template#compile

the second two were nowhere in the list in 4.2.

Searching on find_template_paths found some suggestions like rails/rails#20752 (comment) (didn't make an impact). But then reading and got me to creating this to work around the problem.

By adding the spec support above both the sampled feature tests and the overall suite became the same as it was on the 4.2 branch.

However this is fixed in rails 5.1: rails/rails@2380354#diff-5630af1610e29418fbdfd318b8f68eb3 provided either: config.cache_classes = false or config.cache_template_loading = true in config/environments/test.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment