Skip to content

Commit 4db7ae5

Browse files
committed
Remove cache from Template::Resolver
1 parent 7656143 commit 4db7ae5

File tree

3 files changed

+1
-112
lines changed

3 files changed

+1
-112
lines changed

actionview/lib/action_view/template/resolver.rb

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -49,84 +49,18 @@ def parse(path)
4949
end
5050
end
5151

52-
# Threadsafe template cache
53-
class Cache # :nodoc:
54-
class SmallCache < Concurrent::Map
55-
def initialize(options = {})
56-
super(options.merge(initial_capacity: 2))
57-
end
58-
end
59-
60-
# Preallocate all the default blocks for performance/memory consumption reasons
61-
PARTIAL_BLOCK = lambda { |cache, partial| cache[partial] = SmallCache.new }
62-
PREFIX_BLOCK = lambda { |cache, prefix| cache[prefix] = SmallCache.new(&PARTIAL_BLOCK) }
63-
NAME_BLOCK = lambda { |cache, name| cache[name] = SmallCache.new(&PREFIX_BLOCK) }
64-
KEY_BLOCK = lambda { |cache, key| cache[key] = SmallCache.new(&NAME_BLOCK) }
65-
66-
# Usually a majority of template look ups return nothing, use this canonical preallocated array to save memory
67-
NO_TEMPLATES = [].freeze
68-
69-
def initialize
70-
@data = SmallCache.new(&KEY_BLOCK)
71-
end
72-
73-
def inspect
74-
"#{to_s[0..-2]} keys=#{@data.size}>"
75-
end
76-
77-
# Cache the templates returned by the block
78-
def cache(key, name, prefix, partial, locals)
79-
@data[key][name][prefix][partial][locals] ||= canonical_no_templates(yield)
80-
end
81-
82-
def clear
83-
@data.clear
84-
end
85-
86-
# Get the cache size. Do not call this
87-
# method. This method is not guaranteed to be here ever.
88-
def size # :nodoc:
89-
size = 0
90-
@data.each_value do |v1|
91-
v1.each_value do |v2|
92-
v2.each_value do |v3|
93-
v3.each_value do |v4|
94-
size += v4.size
95-
end
96-
end
97-
end
98-
end
99-
100-
size
101-
end
102-
103-
private
104-
def canonical_no_templates(templates)
105-
templates.empty? ? NO_TEMPLATES : templates
106-
end
107-
end
108-
10952
cattr_accessor :caching, default: true
11053

11154
class << self
11255
alias :caching? :caching
11356
end
11457

115-
def initialize
116-
@cache = Cache.new
117-
end
118-
11958
def clear_cache
120-
@cache.clear
12159
end
12260

12361
# Normalizes the arguments and passes it on to find_templates.
12462
def find_all(name, prefix = nil, partial = false, details = {}, key = nil, locals = [])
125-
locals = locals.map(&:to_s).sort!.freeze
126-
127-
cached(key, [name, prefix, partial], details, locals) do
128-
_find_all(name, prefix, partial, details, key, locals)
129-
end
63+
_find_all(name, prefix, partial, details, key, locals)
13064
end
13165

13266
def all_template_paths # :nodoc:
@@ -147,22 +81,6 @@ def _find_all(name, prefix, partial, details, key, locals)
14781
def find_templates(name, prefix, partial, details, locals = [])
14882
raise NotImplementedError, "Subclasses must implement a find_templates(name, prefix, partial, details, locals = []) method"
14983
end
150-
151-
# Handles templates caching. If a key is given and caching is on
152-
# always check the cache before hitting the resolver. Otherwise,
153-
# it always hits the resolver but if the key is present, check if the
154-
# resolver is fresher before returning it.
155-
def cached(key, path_info, details, locals)
156-
name, prefix, partial = path_info
157-
158-
if key
159-
@cache.cache(key, name, prefix, partial, locals) do
160-
yield
161-
end
162-
else
163-
yield
164-
end
165-
end
16684
end
16785

16886
# A resolver that loads files from the filesystem.

actionview/test/actionpack/abstract/layouts_test.rb

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -285,25 +285,6 @@ class TestBase < ActiveSupport::TestCase
285285
assert_equal "With String hello less than 3 bar", controller.response_body
286286
end
287287

288-
test "cache should not grow when locals change for a string template" do
289-
cache = WithString.view_paths.paths.first.instance_variable_get(:@cache)
290-
291-
controller = WithString.new
292-
controller.process(:index) # heat the cache
293-
294-
size = cache.size
295-
296-
10.times do |x|
297-
controller = WithString.new
298-
controller.define_singleton_method :index do
299-
render template: ActionView::Template::Text.new("Hello string!"), locals: { "x#{x}": :omg }
300-
end
301-
controller.process(:index)
302-
end
303-
304-
assert_equal size, cache.size
305-
end
306-
307288
test "when layout is specified as a string, render with that layout" do
308289
controller = WithString.new
309290
controller.process(:index)

actionview/test/template/resolver_cache_test.rb

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)