mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
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:
+13
-10
@@ -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
|
||||
end
|
||||
end
|
||||
return true if match
|
||||
end
|
||||
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
|
||||
end
|
||||
@ss.pos = start if len
|
||||
false
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user