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
|
||||
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
|
||||
def push(new_scope = {})
|
||||
@scopes.unshift(new_scope)
|
||||
|
||||
@@ -72,5 +72,18 @@ module Liquid
|
||||
rescue ::ArgumentError => e
|
||||
raise Liquid::ArgumentError, e.message, e.backtrace
|
||||
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
|
||||
|
||||
@@ -307,6 +307,9 @@ module Liquid
|
||||
@filters.each do |filter_name, filter_args, filter_kwargs|
|
||||
if filter_args.empty? && !filter_kwargs
|
||||
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
|
||||
filter_args = evaluate_filter_expressions(context, filter_args, filter_kwargs)
|
||||
obj = context.invoke(filter_name, obj, *filter_args)
|
||||
|
||||
Reference in New Issue
Block a user