mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
add invoke_two fast path for single-arg filter invocation, avoids splat chain
This commit is contained in:
@@ -115,6 +115,11 @@ module Liquid
|
|||||||
strainer.invoke_single(method, input).to_liquid
|
strainer.invoke_single(method, input).to_liquid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Fast path for two-argument filter invocation (e.g. {{ value | default: 'x' }})
|
||||||
|
def invoke_two(method, input, arg1)
|
||||||
|
strainer.invoke_two(method, input, arg1).to_liquid
|
||||||
|
end
|
||||||
|
|
||||||
# Push new local scope on the stack. use <tt>Context#stack</tt> instead
|
# Push new local scope on the stack. use <tt>Context#stack</tt> instead
|
||||||
def push(new_scope = {})
|
def push(new_scope = {})
|
||||||
@scopes.unshift(new_scope)
|
@scopes.unshift(new_scope)
|
||||||
|
|||||||
@@ -72,5 +72,18 @@ module Liquid
|
|||||||
rescue ::ArgumentError => e
|
rescue ::ArgumentError => e
|
||||||
raise Liquid::ArgumentError, e.message, e.backtrace
|
raise Liquid::ArgumentError, e.message, e.backtrace
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Fast path for two-argument filter invocation (input + one arg).
|
||||||
|
def invoke_two(method, input, arg1)
|
||||||
|
if self.class.invokable?(method)
|
||||||
|
send(method, input, arg1)
|
||||||
|
elsif @context.strict_filters
|
||||||
|
raise Liquid::UndefinedFilter, "undefined filter #{method}"
|
||||||
|
else
|
||||||
|
input
|
||||||
|
end
|
||||||
|
rescue ::ArgumentError => e
|
||||||
|
raise Liquid::ArgumentError, e.message, e.backtrace
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -307,6 +307,9 @@ module Liquid
|
|||||||
@filters.each do |filter_name, filter_args, filter_kwargs|
|
@filters.each do |filter_name, filter_args, filter_kwargs|
|
||||||
if filter_args.empty? && !filter_kwargs
|
if filter_args.empty? && !filter_kwargs
|
||||||
obj = context.invoke_single(filter_name, obj)
|
obj = context.invoke_single(filter_name, obj)
|
||||||
|
elsif !filter_kwargs && filter_args.length == 1
|
||||||
|
# Single positional arg — most common after no-arg
|
||||||
|
obj = context.invoke_two(filter_name, obj, context.evaluate(filter_args[0]))
|
||||||
else
|
else
|
||||||
filter_args = evaluate_filter_expressions(context, filter_args, filter_kwargs)
|
filter_args = evaluate_filter_expressions(context, filter_args, filter_kwargs)
|
||||||
obj = context.invoke(filter_name, obj, *filter_args)
|
obj = context.invoke(filter_name, obj, *filter_args)
|
||||||
|
|||||||
Reference in New Issue
Block a user