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. # Check if next id matches expected string, consume if so. No allocation.
def expect_id(expected) def expect_id(expected)
start = @ss.pos start = @ss.pos
if @ss.skip(ID_REGEX) == expected.bytesize len = @ss.skip(ID_REGEX)
match = true if len == expected.bytesize
expected.bytesize.times do |i| # Compare bytes directly without allocating a string
if @source.getbyte(start + i) != expected.getbyte(i) i = 0
match = false while i < len
break unless @source.getbyte(start + i) == expected.getbyte(i)
@ss.pos = start
return false
end end
i += 1
end end
return true if match return true
end end
@ss.pos = start @ss.pos = start if len
false false
end end