From d79b9fa2549c2a97a654d5156f2d58f0da5a8f42 Mon Sep 17 00:00:00 2001 From: Tobi Lutke Date: Wed, 11 Mar 2026 07:23:08 -0400 Subject: [PATCH] avoid unnecessary strip allocation in Expression.parse, use byteslice for string literals --- lib/liquid/expression.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/liquid/expression.rb b/lib/liquid/expression.rb index 00c40a4c..b5178d38 100644 --- a/lib/liquid/expression.rb +++ b/lib/liquid/expression.rb @@ -35,11 +35,18 @@ module Liquid def parse(markup, ss = StringScanner.new(""), cache = nil) return unless markup - markup = markup.strip # markup can be a frozen string + # Only strip if there's leading/trailing whitespace (avoids allocation) + first_byte = markup.getbyte(0) + if first_byte == 32 || first_byte == 9 || first_byte == 10 || first_byte == 13 # space, tab, \n, \r + markup = markup.strip + else + last_byte = markup.getbyte(markup.bytesize - 1) + markup = markup.strip if last_byte == 32 || last_byte == 9 || last_byte == 10 || last_byte == 13 + end if (markup.start_with?('"') && markup.end_with?('"')) || (markup.start_with?("'") && markup.end_with?("'")) - return markup[1..-2] + return markup.byteslice(1, markup.bytesize - 2) elsif LITERALS.key?(markup) return LITERALS[markup] end