Skip to content

Instantly share code, notes, and snippets.

@inopinatus
Last active June 1, 2018 23:30
Show Gist options
  • Save inopinatus/8bd36be4783ccf544ad4f57bf979f4ed to your computer and use it in GitHub Desktop.
Save inopinatus/8bd36be4783ccf544ad4f57bf979f4ed to your computer and use it in GitHub Desktop.
Temporary workaround for Rails globbing performance issue https://github.com/rails/rails/issues/30502
# frozen_string_literal: true
# config/initializers/path_resolver_dir_glob_cache.rb
#
# Work around glob performance woes for JS requests in development, c.f
# https://github.com/rails/rails/issues/30502, by
# prepending ActionView::PathResolver#find_template_paths to use a
# result cache. Cache invalidation via fs listeners.
# Use at your own peril.
hacks = Module.new do
module_function
def dir_name_from_query(query)
File.dirname(query.gsub(/(?<!\\){.*/, ''))
end
GLOB_CACHE = Concurrent::Hash.new
GLOB_CACHE_LISTENERS = Concurrent::Hash.new do |hash, dir|
hash[dir] = Listen.to(dir) do
GLOB_CACHE.delete_if do |cached_query, _|
dir == dir_name_from_query(cached_query)
end
end
end
def find_template_paths(query)
dirname = dir_name_from_query(query)
return [] unless Dir.exist?(dirname)
if GLOB_CACHE.has_key?(query)
GLOB_CACHE[query]
else
GLOB_CACHE_LISTENERS[dirname].start
GLOB_CACHE[query] = super
end
end
end
ActionView::PathResolver.prepend(hacks) if Rails.env.development?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment