Skip to content

Commit 24e8b0a

Browse files
Rui914paramat
authored andcommitted
Faster insertion into table
1 parent 75db054 commit 24e8b0a

File tree

13 files changed

+48
-45
lines changed

13 files changed

+48
-45
lines changed

builtin/common/filterlist.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ function filterlist.process(self)
189189
for k,v in pairs(self.m_raw_list) do
190190
if self.m_filtercriteria == nil or
191191
self.m_filter_fct(v,self.m_filtercriteria) then
192-
table.insert(self.m_processed_list,v)
192+
self.m_processed_list[#self.m_processed_list + 1] = v
193193
end
194194
end
195195

builtin/common/misc_helpers.lua

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
--------------------------------------------------------------------------------
44
-- Localize functions to avoid table lookups (better performance).
5-
local table_insert = table.insert
65
local string_sub, string_find = string.sub, string.find
76

87
--------------------------------------------------------------------------------
@@ -94,13 +93,13 @@ function dump2(o, name, dumped)
9493
-- the form _G["table: 0xFFFFFFF"]
9594
keyStr = string.format("_G[%q]", tostring(k))
9695
-- Dump key table
97-
table_insert(t, dump2(k, keyStr, dumped))
96+
t[#t + 1] = dump2(k, keyStr, dumped)
9897
end
9998
else
10099
keyStr = basic_dump(k)
101100
end
102101
local vname = string.format("%s[%s]", name, keyStr)
103-
table_insert(t, dump2(v, vname, dumped))
102+
t[#t + 1] = dump2(v, vname, dumped)
104103
end
105104
return string.format("%s = {}\n%s", name, table.concat(t))
106105
end
@@ -135,7 +134,7 @@ function dump(o, indent, nested, level)
135134
local t = {}
136135
local dumped_indexes = {}
137136
for i, v in ipairs(o) do
138-
table_insert(t, dump(v, indent, nested, level + 1))
137+
t[#t + 1] = dump(v, indent, nested, level + 1)
139138
dumped_indexes[i] = true
140139
end
141140
for k, v in pairs(o) do
@@ -144,7 +143,7 @@ function dump(o, indent, nested, level)
144143
k = "["..dump(k, indent, nested, level + 1).."]"
145144
end
146145
v = dump(v, indent, nested, level + 1)
147-
table_insert(t, k.." = "..v)
146+
t[#t + 1] = k.." = "..v
148147
end
149148
end
150149
nested[o] = nil
@@ -177,7 +176,7 @@ function string.split(str, delim, include_empty, max_splits, sep_is_pattern)
177176
local s = string_sub(str, pos, np - 1)
178177
if include_empty or (s ~= "") then
179178
max_splits = max_splits - 1
180-
table_insert(items, s)
179+
items[#items + 1] = s
181180
end
182181
pos = npe + 1
183182
until (max_splits == 0) or (pos > (len + 1))
@@ -186,8 +185,8 @@ end
186185

187186
--------------------------------------------------------------------------------
188187
function table.indexof(list, val)
189-
for i = 1, #list do
190-
if list[i] == val then
188+
for i, v in ipairs(list) do
189+
if v == val then
191190
return i
192191
end
193192
end
@@ -324,7 +323,7 @@ function core.splittext(text,charlimit)
324323
local last_line = ""
325324
while start ~= nil do
326325
if string.len(last_line) + (stop-start) > charlimit then
327-
table_insert(retval, last_line)
326+
retval[#retval + 1] = last_line
328327
last_line = ""
329328
end
330329

@@ -335,7 +334,7 @@ function core.splittext(text,charlimit)
335334
last_line = last_line .. string_sub(text, current_idx, stop - 1)
336335

337336
if gotnewline then
338-
table_insert(retval, last_line)
337+
retval[#retval + 1] = last_line
339338
last_line = ""
340339
gotnewline = false
341340
end
@@ -353,11 +352,11 @@ function core.splittext(text,charlimit)
353352

354353
--add last part of text
355354
if string.len(last_line) + (string.len(text) - current_idx) > charlimit then
356-
table_insert(retval, last_line)
357-
table_insert(retval, string_sub(text, current_idx))
355+
retval[#retval + 1] = last_line
356+
retval[#retval + 1] = string_sub(text, current_idx)
358357
else
359358
last_line = last_line .. " " .. string_sub(text, current_idx)
360-
table_insert(retval, last_line)
359+
retval[#retval + 1] = last_line
361360
end
362361

363362
return retval
@@ -430,14 +429,14 @@ if INIT == "game" then
430429

431430
if iswall then
432431
core.set_node(pos, {name = wield_name,
433-
param2 = dirs1[fdir+1]})
432+
param2 = dirs1[fdir + 1]})
434433
elseif isceiling then
435434
if orient_flags.force_facedir then
436435
core.set_node(pos, {name = wield_name,
437436
param2 = 20})
438437
else
439438
core.set_node(pos, {name = wield_name,
440-
param2 = dirs2[fdir+1]})
439+
param2 = dirs2[fdir + 1]})
441440
end
442441
else -- place right side up
443442
if orient_flags.force_facedir then

builtin/common/serialize.lua

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function core.serialize(x)
104104
local i = local_index
105105
local_index = local_index + 1
106106
var = "_["..i.."]"
107-
table.insert(local_defs, var.." = "..val)
107+
local_defs[#local_defs + 1] = var.." = "..val
108108
dumped[x] = var
109109
return var
110110
end
@@ -135,16 +135,15 @@ function core.serialize(x)
135135
local np = nest_points[x]
136136
for i, v in ipairs(x) do
137137
if not np or not np[i] then
138-
table.insert(vals, dump_or_ref_val(v))
138+
vals[#vals + 1] = dump_or_ref_val(v)
139139
end
140140
idx_dumped[i] = true
141141
end
142142
for k, v in pairs(x) do
143143
if (not np or not np[k]) and
144144
not idx_dumped[k] then
145-
table.insert(vals,
146-
"["..dump_or_ref_val(k).."] = "
147-
..dump_or_ref_val(v))
145+
vals[#vals + 1] = "["..dump_or_ref_val(k).."] = "
146+
..dump_or_ref_val(v)
148147
end
149148
end
150149
return "{"..table.concat(vals, ", ").."}"
@@ -156,9 +155,9 @@ function core.serialize(x)
156155
local function dump_nest_points()
157156
for parent, vals in pairs(nest_points) do
158157
for k, v in pairs(vals) do
159-
table.insert(local_defs, dump_or_ref_val(parent)
158+
local_defs[#local_defs + 1] = dump_or_ref_val(parent)
160159
.."["..dump_or_ref_val(k).."] = "
161-
..dump_or_ref_val(v))
160+
..dump_or_ref_val(v)
162161
end
163162
end
164163
end

builtin/fstk/buttonbar.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,12 @@ local buttonbar_metatable = {
145145
if image == nil then image = "" end
146146
if tooltip == nil then tooltip = "" end
147147

148-
table.insert(self.buttons,{ name=name, caption=caption, image=image, tooltip=tooltip})
148+
self.buttons[#self.buttons + 1] = {
149+
name = name,
150+
caption = caption,
151+
image = image,
152+
tooltip = tooltip
153+
}
149154
if self.orientation == "horizontal" then
150155
if ( (self.btn_size * #self.buttons) + (self.btn_size * 0.05 *2)
151156
> self.size.x ) then

builtin/fstk/tabview.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ local function add_tab(self,tab)
4646
tabdata = {},
4747
}
4848

49-
table.insert(self.tablist,newtab)
49+
self.tablist[#self.tablist + 1] = newtab
5050

5151
if self.last_tab_index == #self.tablist then
5252
self.current_tab = tab.name

builtin/game/auth.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function core.privs_to_string(privs, delim)
2020
local list = {}
2121
for priv, bool in pairs(privs) do
2222
if bool then
23-
table.insert(list, priv)
23+
list[#list + 1] = priv
2424
end
2525
end
2626
return table.concat(list, delim)

builtin/game/chatcommands.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ core.register_chatcommand("help", {
116116
local cmds = {}
117117
for cmd, def in pairs(core.chatcommands) do
118118
if core.check_player_privs(name, def.privs) then
119-
table.insert(cmds, cmd)
119+
cmds[#cmds + 1] = cmd
120120
end
121121
end
122122
table.sort(cmds)
@@ -127,15 +127,15 @@ core.register_chatcommand("help", {
127127
local cmds = {}
128128
for cmd, def in pairs(core.chatcommands) do
129129
if core.check_player_privs(name, def.privs) then
130-
table.insert(cmds, format_help_line(cmd, def))
130+
cmds[#cmds + 1] = format_help_line(cmd, def)
131131
end
132132
end
133133
table.sort(cmds)
134134
return true, "Available commands:\n"..table.concat(cmds, "\n")
135135
elseif param == "privs" then
136136
local privs = {}
137137
for priv, def in pairs(core.registered_privileges) do
138-
table.insert(privs, priv .. ": " .. def.description)
138+
privs[#privs + 1] = priv .. ": " .. def.description
139139
end
140140
table.sort(privs)
141141
return true, "Available privileges:\n"..table.concat(privs, "\n")

builtin/game/misc.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ end)
4040
function core.after(after, func, ...)
4141
assert(tonumber(time) and type(func) == "function",
4242
"Invalid core.after invocation")
43-
table.insert(jobs, {
43+
jobs[#jobs + 1] = {
4444
func = func,
4545
expire = time + after,
4646
arg = {...},
4747
mod_origin = core.get_last_run_mod()
48-
})
48+
}
4949
end
5050

5151
function core.check_player_privs(player_or_name, ...)
@@ -63,14 +63,14 @@ function core.check_player_privs(player_or_name, ...)
6363
-- We were provided with a table like { privA = true, privB = true }.
6464
for priv, value in pairs(requested_privs[1]) do
6565
if value and not player_privs[priv] then
66-
table.insert(missing_privileges, priv)
66+
missing_privileges[#missing_privileges + 1] = priv
6767
end
6868
end
6969
else
7070
-- Only a list, we can process it directly.
7171
for key, priv in pairs(requested_privs) do
7272
if not player_privs[priv] then
73-
table.insert(missing_privileges, priv)
73+
missing_privileges[#missing_privileges + 1] = priv
7474
end
7575
end
7676
end
@@ -96,7 +96,7 @@ function core.get_connected_players()
9696
local temp_table = {}
9797
for index, value in pairs(player_list) do
9898
if value:is_player_connected() then
99-
table.insert(temp_table, value)
99+
temp_table[#temp_table + 1] = value
100100
end
101101
end
102102
return temp_table

builtin/game/register.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ end
7575

7676
function core.register_abm(spec)
7777
-- Add to core.registered_abms
78-
core.registered_abms[#core.registered_abms+1] = spec
78+
core.registered_abms[#core.registered_abms + 1] = spec
7979
spec.mod_origin = core.get_current_modname() or "??"
8080
end
8181

@@ -391,7 +391,7 @@ end
391391
local function make_registration()
392392
local t = {}
393393
local registerfunc = function(func)
394-
table.insert(t, func)
394+
t[#t + 1] = func
395395
core.callback_origins[func] = {
396396
mod = core.get_current_modname() or "??",
397397
name = debug.getinfo(1, "n").name or "??"
@@ -467,9 +467,9 @@ end
467467

468468
function core.register_on_player_hpchange(func, modifier)
469469
if modifier then
470-
table.insert(core.registered_on_player_hpchanges.modifiers, func)
470+
core.registered_on_player_hpchanges.modifiers[#core.registered_on_player_hpchanges.modifiers + 1] = func
471471
else
472-
table.insert(core.registered_on_player_hpchanges.loggers, func)
472+
core.registered_on_player_hpchanges.loggers[#core.registered_on_player_hpchanges.loggers + 1] = func
473473
end
474474
core.callback_origins[func] = {
475475
mod = core.get_current_modname() or "??",

builtin/mainmenu/common.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ function order_favorite_list(list)
6767
for i=1,#list,1 do
6868
local fav = list[i]
6969
if is_server_protocol_compat(fav.proto_min, fav.proto_max) then
70-
table.insert(res, fav)
70+
res[#res + 1] = fav
7171
end
7272
end
7373
for i=1,#list,1 do
7474
local fav = list[i]
7575
if not is_server_protocol_compat(fav.proto_min, fav.proto_max) then
76-
table.insert(res, fav)
76+
res[#res + 1] = fav
7777
end
7878
end
7979
return res

0 commit comments

Comments
 (0)