Replace manual scan_fragment/scan_quoted_string_raw/skip_fragment with regex — cleaner, same/better perf\n\nResult: {"status":"keep","combined_µs":4132,"parse_µs":2890,"render_µs":1242,"allocations":25535}

This commit is contained in:
Tobi Lutke
2026-04-04 17:42:33 -07:00
committed by Chris Pak
parent 28ede9c539
commit edd8fabb3a
+9 -41
View File
@@ -161,17 +161,12 @@ module Liquid
content content
end end
# Regex for quoted strings (single or double quoted, including quotes)
QUOTED_STRING_RAW = /"[^"]*"|'[^']*'/
# Scan a quoted string including quotes. Returns the full "..." or '...' string, or nil. # Scan a quoted string including quotes. Returns the full "..." or '...' string, or nil.
def scan_quoted_string_raw def scan_quoted_string_raw
b = @ss.peek_byte @ss.scan(QUOTED_STRING_RAW)
return unless b == QUOTE_S || b == QUOTE_D
quote = b
start = @ss.pos
@ss.scan_byte
@ss.scan_byte while (b = @ss.peek_byte) && b != quote
@ss.scan_byte if @ss.peek_byte == quote
@source.byteslice(start, @ss.pos - start)
end end
# ── Expressions ───────────────────────────────────────────────── # ── Expressions ─────────────────────────────────────────────────
@@ -193,42 +188,15 @@ module Liquid
# Skip a fragment without allocating. Returns length skipped, or 0. # Skip a fragment without allocating. Returns length skipped, or 0.
def skip_fragment def skip_fragment
b = @ss.peek_byte @ss.skip(QUOTED_STRING_RAW) || @ss.skip(UNQUOTED_FRAGMENT) || 0
return 0 unless b
start = @ss.pos
if b == QUOTE_S || b == QUOTE_D
quote = b
@ss.scan_byte
@ss.scan_byte while (b = @ss.peek_byte) && b != quote
@ss.scan_byte if @ss.peek_byte == quote
else
while (b = @ss.peek_byte)
break if b == SPACE || b == TAB || b == NL || b == CR || b == COMMA || b == PIPE
@ss.scan_byte
end
end
@ss.pos - start
end end
# Regex for unquoted fragment: non-whitespace/comma/pipe sequence
UNQUOTED_FRAGMENT = /[^\s,|]+/
# Scan a "QuotedFragment" — a quoted string or non-whitespace/comma/pipe run # Scan a "QuotedFragment" — a quoted string or non-whitespace/comma/pipe run
def scan_fragment def scan_fragment
b = @ss.peek_byte @ss.scan(QUOTED_STRING_RAW) || @ss.scan(UNQUOTED_FRAGMENT)
return unless b
if b == QUOTE_S || b == QUOTE_D
scan_quoted_string_raw
else
start = @ss.pos
while (b = @ss.peek_byte)
break if b == SPACE || b == TAB || b == NL || b == CR || b == COMMA || b == PIPE
@ss.scan_byte
end
len = @ss.pos - start
len > 0 ? @source.byteslice(start, len) : nil
end
end end
# ── Comparison operators ──────────────────────────────────────── # ── Comparison operators ────────────────────────────────────────