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:
James Tucker
2013-08-31 18:56:35 +00:00
parent e8b41c8856
commit 9b2d5b7dd3
3 changed files with 36 additions and 7 deletions
+12 -4
View File
@@ -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)