Replace simple_lookup? byte scan with match? regex — 8x faster per call, cleaner code\n\nResult: {"status":"keep","combined_µs":3489,"parse_µs":2353,"render_µs":1136,"allocations":24647}

This commit is contained in:
Tobi Lutke
2026-04-04 17:42:33 -07:00
committed by Chris Pak
parent f39fad8934
commit 6faa62697b
2 changed files with 5 additions and 25 deletions
+4 -25
View File
@@ -70,32 +70,11 @@ module Liquid
end
# Check if markup is a simple identifier chain: [\w-]+\??(.[\w-]+\??)*
# Returns true if it only contains word chars, hyphens, dots, and optional trailing ?
# Uses C-level match? — 8x faster than Ruby byte scanning
SIMPLE_LOOKUP_RE = /\A[\w-]+\??(?:\.[\w-]+\??)*\z/
def self.simple_lookup?(markup)
pos = 0
len = markup.bytesize
return false if len == 0
while pos < len
b = markup.getbyte(pos)
if (b >= 97 && b <= 122) || (b >= 65 && b <= 90) || (b >= 48 && b <= 57) || b == 95 || b == 45 # \w or -
pos += 1
elsif b == 63 # '?'
pos += 1
# '?' must be followed by '.' or end
return true if pos >= len
return false unless markup.getbyte(pos) == 46
elsif b == 46 # '.'
pos += 1
# Must have at least one word char after dot
return false if pos >= len
b2 = markup.getbyte(pos)
return false unless (b2 >= 97 && b2 <= 122) || (b2 >= 65 && b2 <= 90) || b2 == 95
pos += 1
else
return false
end
end
true
markup.bytesize > 0 && markup.match?(SIMPLE_LOOKUP_RE)
end
def initialize(markup, string_scanner = StringScanner.new(""), cache = nil, simple = false)