mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
fast-path find_variable: check top scope first before find_index
This commit is contained in:
@@ -199,6 +199,11 @@ module Liquid
|
||||
|
||||
# Fetches an object starting at the local scope and then moving up the hierachy
|
||||
def find_variable(key, raise_on_not_found: true)
|
||||
# Fast path: check top scope first (most common in for loops)
|
||||
scope = @scopes[0]
|
||||
if scope.key?(key)
|
||||
variable = lookup_and_evaluate(scope, key, raise_on_not_found: raise_on_not_found)
|
||||
else
|
||||
# This was changed from find() to find_index() because this is a very hot
|
||||
# path and find_index() is optimized in MRI to reduce object allocation
|
||||
index = @scopes.find_index { |s| s.key?(key) }
|
||||
@@ -208,6 +213,7 @@ module Liquid
|
||||
else
|
||||
try_variable_find_in_environments(key, raise_on_not_found: raise_on_not_found)
|
||||
end
|
||||
end
|
||||
|
||||
# update variable's context before invoking #to_liquid
|
||||
variable.context = self if variable.respond_to?(:context=)
|
||||
|
||||
Reference in New Issue
Block a user