Skip to content

Commit 169dce5

Browse files
committed
perf: apply table optimization
1 parent 3905ac9 commit 169dce5

1 file changed

Lines changed: 35 additions & 20 deletions

File tree

lib/liquid.lua

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,10 +1206,12 @@ end
12061206
function Parser:block( ... )
12071207
-- document : (RAWSTRING | state)*
12081208
local node = Compoud:new()
1209+
local n = #node
12091210
while self.current_token.token_type ~= EOF do
12101211
if self.current_token.token_type == RAWSTRING then
12111212
local temp = RawStr:new(self.current_token, self.parser_context)
1212-
table.insert(node, temp)
1213+
node[n+1] = temp
1214+
n = n + 1
12131215
self:eat(RAWSTRING)
12141216
else
12151217
if self.current_token.token_type == TAGSTART
@@ -1229,7 +1231,8 @@ function Parser:block( ... )
12291231
return node
12301232
end
12311233
local temp = self:tag()
1232-
table.insert(node, temp)
1234+
node[n+1] = temp
1235+
n = n + 1
12331236
elseif self.current_token.token_type == VARSTART
12341237
or self.current_token.token_type == VARSTARTWC then
12351238
if self.current_token.token_type == VARSTARTWC then
@@ -1240,7 +1243,8 @@ function Parser:block( ... )
12401243
end
12411244
self:eat(self.current_token.token_type)
12421245
local temp = self:var()
1243-
table.insert(node, temp)
1246+
node[n+1] = temp
1247+
n = n + 1
12441248
else
12451249
self:raise_error("expect tag_start: \'{%\' or \'{%-\' ; var_start \'{{ \' or \'{{-\'")
12461250
end
@@ -1569,12 +1573,12 @@ function Parser:paramlist( ... )
15691573
paramlist : factor (COMMA factor)*
15701574
]]
15711575
local params = {}
1572-
local param = self:factor()
1573-
table.insert(params, param)
1576+
params[1] = self:factor()
1577+
local n = 2
15741578
while self.current_token.token_type == COMMA do
15751579
self:eat(COMMA)
1576-
param = self:factor()
1577-
table.insert(params, param)
1580+
params[n] = self:factor()
1581+
n = n + 1
15781582
end
15791583
return params
15801584
end
@@ -1977,9 +1981,11 @@ function Interpreter:visit_Range( node )
19771981
return({from})
19781982
else
19791983
local result = {}
1984+
local n = 1
19801985
for k = from, to, 1 do
19811986
self.resourcelimit:check_loopcount()
1982-
table.insert(result, k)
1987+
result[n] = k
1988+
n = n + 1
19831989
end
19841990
return(result)
19851991
end
@@ -2402,11 +2408,13 @@ function Interpreter:visit_Filter( node)
24022408
self:raise_error("cannot find filter: " .. node.filter_name, node, 'filter_name')
24032409
end
24042410

2405-
local params = {}
2406-
for i,v in ipairs(node.params) do
2407-
table.insert(params, self:visit(v))
2411+
local params = node.params
2412+
local n = #params
2413+
local resolved = new_tab(n, 0)
2414+
for i = 1, n do
2415+
resolved[i] = self:visit(params[i])
24082416
end
2409-
local status, err = pcall(filter, table.unpack(params))
2417+
local status, err = pcall(filter, table.unpack(resolved))
24102418
if status then
24112419
return err
24122420
else
@@ -2451,7 +2459,7 @@ function Interpreter:visit_CycleLoop( node )
24512459
local length = #(node.elementarray)
24522460
local cycle_value = {}
24532461
for i,v in ipairs(node.elementarray) do
2454-
table.insert(cycle_value, self:visit(v))
2462+
cycle_value[i] = self:visit(v)
24552463
end
24562464
local obj = {["_group_name"] = group_name, ["_cycle_value"] = cycle_value}
24572465
-- get json string as index
@@ -2523,7 +2531,7 @@ function InterpreterContext:new( context )
25232531
if type(context) ~= "table" then
25242532
error("Initilied fail! context should be a table type ")
25252533
end
2526-
table.insert(instance.stackframe, context)
2534+
instance.stackframe[1] = context
25272535
return instance
25282536
end
25292537
--
@@ -2847,12 +2855,15 @@ local function last( a )
28472855
end
28482856
local function concat( a, b)
28492857
-- body
2858+
local n = 1
28502859
local temp = {}
28512860
for i,v in ipairs(iterator(a)) do
2852-
table.insert(temp, v)
2861+
temp[n] = v
2862+
n = n + 1
28532863
end
28542864
for i,v in ipairs(iterator(b)) do
2855-
table.insert(temp, v)
2865+
temp[n] = v
2866+
n = n + 1
28562867
end
28572868
return temp
28582869
end
@@ -2864,7 +2875,7 @@ local function map( a, map_field)
28642875
-- body
28652876
local temp = {}
28662877
for i,v in ipairs(a) do
2867-
table.insert(temp, v[map_field])
2878+
temp[i] = v[map_field]
28682879
end
28692880
return join(temp, '')
28702881
end
@@ -2873,8 +2884,10 @@ local function reverse( a )
28732884
local temp = {}
28742885
local it = iterator(a)
28752886
local num = size(a)
2887+
local n = 1
28762888
for k = num, 1, -1 do
2877-
table.insert(temp, it[k])
2889+
temp[n] = it[k]
2890+
n = n + 1
28782891
end
28792892
return temp
28802893
end
@@ -2883,7 +2896,7 @@ local function sort( a, sort_field)
28832896
-- body
28842897
local t = {}
28852898
for i,v in ipairs(iterator(a)) do
2886-
table.insert(t, v)
2899+
t[i] = v
28872900
end
28882901
if not sort_field then
28892902
table.sort(t)
@@ -2898,11 +2911,13 @@ local function uniq( a )
28982911
-- body
28992912
local t = {}
29002913
local result = {}
2914+
local n = 1
29012915
for i,v in ipairs(iterator(a)) do
29022916
local k = cjson.encode(v)
29032917
if not t[k] then
29042918
t[k] = true
2905-
table.insert(result, v)
2919+
result[n] = v
2920+
n = n + 1
29062921
end
29072922
end
29082923
return result

0 commit comments

Comments
 (0)