From 80edd212a0690c3a3c569b6d8c54ba1feed0eb25 Mon Sep 17 00:00:00 2001 From: Tobi Lutke Date: Wed, 11 Mar 2026 09:10:05 -0400 Subject: [PATCH] add parse_simple to skip simple_lookup? check when caller validates --- lib/liquid/variable.rb | 4 ++-- lib/liquid/variable_lookup.rb | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/liquid/variable.rb b/lib/liquid/variable.rb index 40755b15..77b49ff8 100644 --- a/lib/liquid/variable.rb +++ b/lib/liquid/variable.rb @@ -163,9 +163,9 @@ module Liquid elsif Expression::LITERALS.key?(expr_markup) @name = Expression::LITERALS[expr_markup] elsif cache - @name = cache[expr_markup] || (cache[expr_markup] = VariableLookup.parse(expr_markup, ss, cache).freeze) + @name = cache[expr_markup] || (cache[expr_markup] = VariableLookup.parse_simple(expr_markup, ss, cache).freeze) else - @name = VariableLookup.parse(expr_markup, ss || StringScanner.new(""), nil).freeze + @name = VariableLookup.parse_simple(expr_markup, ss || StringScanner.new(""), nil).freeze end # End of markup? No filters. diff --git a/lib/liquid/variable_lookup.rb b/lib/liquid/variable_lookup.rb index 299d0729..23eb676d 100644 --- a/lib/liquid/variable_lookup.rb +++ b/lib/liquid/variable_lookup.rb @@ -10,6 +10,11 @@ module Liquid new(markup, string_scanner, cache) end + # Fast parse that skips simple_lookup? check — caller guarantees simple identifier chain + def self.parse_simple(markup, string_scanner = nil, cache = nil) + new(markup, string_scanner, cache, true) + end + # Fast manual scanner replacing markup.scan(VariableParser) # VariableParser = /\[(?>[^\[\]]+|\g<0>)*\]|[\w-]+\??/ # Splits "product.variants[0].title" into ["product", "variants", "[0]", "title"] @@ -93,9 +98,9 @@ module Liquid true end - def initialize(markup, string_scanner = StringScanner.new(""), cache = nil) + def initialize(markup, string_scanner = StringScanner.new(""), cache = nil, simple = false) # Fast path: simple identifier chain without brackets - if self.class.simple_lookup?(markup) + if simple || self.class.simple_lookup?(markup) dot_pos = markup.index('.') if dot_pos.nil? @name = markup