Minor cleanup: optimize expect_id with while loop and early return\n\nResult: {"status":"keep","combined_µs":4184,"parse_µs":2921,"render_µs":1263,"allocations":25535}

This commit is contained in:
Tobi Lutke
2026-04-04 17:42:33 -07:00
committed by Chris Pak
parent c3e55e92d9
commit e3ba4aa958
+11 -8
View File
@@ -98,17 +98,20 @@ module Liquid
# Check if next id matches expected string, consume if so. No allocation.
def expect_id(expected)
start = @ss.pos
if @ss.skip(ID_REGEX) == expected.bytesize
match = true
expected.bytesize.times do |i|
if @source.getbyte(start + i) != expected.getbyte(i)
match = false
break
len = @ss.skip(ID_REGEX)
if len == expected.bytesize
# Compare bytes directly without allocating a string
i = 0
while i < len
unless @source.getbyte(start + i) == expected.getbyte(i)
@ss.pos = start
return false
end
i += 1
end
return true if match
return true
end
@ss.pos = start
@ss.pos = start if len
false
end