mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-16 17:30:43 -07:00
Add a class cache to avoid runtime extend calls
* Strainer has a class cache that creates Strainer subclasses for each filter set that is used on .create calls. * Context now creates a list of filters and passes this to Strainer.create to utilize the class cache in almost all use cases. * If add_filter was called after a render, then the method cache may still be invalidated. Conflicts: lib/liquid/strainer.rb
This commit is contained in:
+12
-4
@@ -11,6 +11,11 @@ module Liquid
|
||||
@@filters = []
|
||||
@@known_filters = Set.new
|
||||
@@known_methods = Set.new
|
||||
@@strainer_class_cache = Hash.new do |hash, filters|
|
||||
hash[filters] = Class.new(Strainer) do
|
||||
filters.each { |f| include f }
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(context)
|
||||
@context = context
|
||||
@@ -32,10 +37,13 @@ module Liquid
|
||||
end
|
||||
end
|
||||
|
||||
def self.create(context)
|
||||
strainer = Strainer.new(context)
|
||||
@@filters.each { |m| strainer.extend(m) }
|
||||
strainer
|
||||
def self.strainer_class_cache
|
||||
@@strainer_class_cache
|
||||
end
|
||||
|
||||
def self.create(context, filters = nil)
|
||||
filters = @@filters.values + (filters || [])
|
||||
strainer_class_cache[filters].new(context)
|
||||
end
|
||||
|
||||
def invoke(method, *args)
|
||||
|
||||
Reference in New Issue
Block a user