Replace manual scan_comparison_op with regex — cleaner and avoids byteslice allocation for op strings\n\nResult: {"status":"keep","combined_µs":4007,"parse_µs":2808,"render_µs":1199,"allocations":25535}

This commit is contained in:
Tobi Lutke
2026-04-04 17:42:33 -07:00
committed by Chris Pak
parent edd8fabb3a
commit 4896d6d497
+5 -19
View File
@@ -212,27 +212,13 @@ module Liquid
}.freeze }.freeze
# Scan a comparison operator. Returns frozen string or nil. # Scan a comparison operator. Returns frozen string or nil.
def scan_comparison_op # Regex for comparison operators
start = @ss.pos COMPARISON_OP_REGEX = /==|!=|<>|<=|>=|<|>|contains(?!\w)/
b = @ss.peek_byte
case b
when 61, 33, 60, 62 # = ! < >
@ss.scan_byte
b2 = @ss.peek_byte
if b2 == 61 || b2 == 62 # second char of ==, !=, <=, >=, <>
@ss.scan_byte
end
when 99 # 'c' for contains
id = scan_id
return unless id == "contains"
return COMPARISON_OPS['contains'] def scan_comparison_op
else if (op = @ss.scan(COMPARISON_OP_REGEX))
return COMPARISON_OPS[op]
end end
op_str = @source.byteslice(start, @ss.pos - start)
COMPARISON_OPS[op_str] || (@ss.pos = start
nil)
end end
# ── Tag parsing helpers ───────────────────────────────────────── # ── Tag parsing helpers ─────────────────────────────────────────