From 7419a44e72ed7e546c4aa0a89a0eca3e8b41f4e2 Mon Sep 17 00:00:00 2001 From: Michael Go Date: Thu, 14 Nov 2024 16:51:45 -0400 Subject: [PATCH] don't cache string expressions to be more perfomant --- lib/liquid/expression.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/liquid/expression.rb b/lib/liquid/expression.rb index e10f97939..4ee0621f8 100644 --- a/lib/liquid/expression.rb +++ b/lib/liquid/expression.rb @@ -81,16 +81,18 @@ def parse(markup) markup = markup.strip # markup can be a frozen string + if (markup.start_with?('"') && markup.end_with?('"')) || + (markup.start_with?("'") && markup.end_with?("'")) + return markup[1..-2] + end + return CACHE[markup] if CACHE.key?(markup) CACHE[markup] = inner_parse(markup) end def inner_parse(markup) - if (markup.start_with?('"') && markup.end_with?('"')) || - (markup.start_with?("'") && markup.end_with?("'")) - return markup[1..-2] - elsif (markup.start_with?("(") && markup.end_with?(")")) && markup =~ RANGES_REGEX + if (markup.start_with?("(") && markup.end_with?(")")) && markup =~ RANGES_REGEX return RangeLookup.parse(Regexp.last_match(1), Regexp.last_match(2)) end @@ -149,5 +151,5 @@ def parse_number(markup) end end - Expression = StringScanner.instance_methods.include?(:scan_byte) ? Expression2 : Expression1 + Expression = StringScanner.instance_methods.include?(:scan_byte) ? Expression2 : Expression11 end