Skip to content

Commit 3905ac9

Browse files
committed
perf: short circuit visit_Compound
1 parent 948f655 commit 3905ac9

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

lib/liquid.lua

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ local Nodetab = {}
1515
local stringbyte = string.byte
1616
local stringchar = string.char
1717
local table_unpack = table.unpack
18+
local table_concat = table.concat
19+
20+
local ok, new_tab = pcall(require, "table.new")
21+
if not ok or type(new_tab) ~= "function" then
22+
new_tab = function (narr, nrec) return {} end
23+
end
1824

1925
local BYTE_LBRACE = stringbyte("{")
2026
local BYTE_RBRACE = stringbyte("}")
@@ -1983,15 +1989,23 @@ function Interpreter:visit_Range( node )
19831989
end
19841990
--
19851991
function Interpreter:visit_Compoud( node )
1986-
-- body
1992+
local n = #node
1993+
if n == 0 then return '' end
1994+
1995+
if n == 1 then
1996+
local result = self:visit(node[1])
1997+
if result == nil then return '' end
1998+
return self:obj2str(result) or ''
1999+
end
2000+
19872001
local output = {}
1988-
for k,v in ipairs(node) do
1989-
if self.interrupt_flag then
1990-
return self:safe_concat(output, '')
1991-
end
1992-
local result = self:visit(v)
2002+
local count = 1
2003+
for i = 1, n do
2004+
if self.interrupt_flag then break end
2005+
local result = self:visit(node[i])
19932006
if result ~= nil then
1994-
table.insert(output, result)
2007+
output[count] = result
2008+
count = count + 1
19952009
end
19962010
end
19972011
return self:safe_concat(output, '')
@@ -2754,14 +2768,15 @@ do
27542768
end
27552769

27562770
function Interpreter:safe_concat(t, d)
2757-
local tmp = {}
2771+
local n = #t
2772+
local tmp = new_tab(n, 0)
27582773

27592774
-- string keys are ignored by concat anyway
2760-
for i,v in ipairs(t) do
2761-
tmp[i] = Interpreter:obj2str(v)
2775+
for i=1, n do
2776+
tmp[i] = Interpreter:obj2str(t[i])
27622777
end
27632778

2764-
return table.concat(tmp, d)
2779+
return table_concat(tmp, d)
27652780
end
27662781
end
27672782
------------------------------------------------------------- helper methods end ---------------------------------------------------------

0 commit comments

Comments
 (0)