Cache SelfDrop per context

This commit is contained in:
Guilherme Carreiro
2026-06-05 10:12:13 +02:00
parent b18930f2ea
commit 771ceccf41
2 changed files with 6 additions and 2 deletions
+1 -1
View File
@@ -208,7 +208,7 @@ module Liquid
# `self` resolves to a SelfDrop (enabling `self['var']` lookups), # `self` resolves to a SelfDrop (enabling `self['var']` lookups),
# but only when it hasn't been explicitly assigned as a local variable. # but only when it hasn't been explicitly assigned as a local variable.
return SelfDrop.new(self) if key == Expression::SELF && !index return @self_drop ||= SelfDrop.new(self) if key == Expression::SELF && !index
variable = if index variable = if index
lookup_and_evaluate(@scopes[index], key, raise_on_not_found: raise_on_not_found) lookup_and_evaluate(@scopes[index], key, raise_on_not_found: raise_on_not_found)
+5 -1
View File
@@ -79,8 +79,12 @@ class SelfDropContextTest < Minitest::Test
def test_self_drop_repeated_lookups_compare_equal_for_same_context def test_self_drop_repeated_lookups_compare_equal_for_same_context
context = Context.new context = Context.new
drop = context.find_variable("self")
cached_drop = context.find_variable("self")
assert_equal(context.find_variable("self"), context.find_variable("self")) assert_same(drop, cached_drop)
assert_equal(drop.object_id, cached_drop.object_id)
assert_equal(drop, cached_drop)
end end
def test_assigned_self_drop_compares_equal_to_itself def test_assigned_self_drop_compares_equal_to_itself