From 24e8b0ac1ea45719937948607259f13866c8bc64 Mon Sep 17 00:00:00 2001 From: Rui914 Date: Mon, 7 Mar 2016 00:53:45 +0900 Subject: [PATCH] Faster insertion into table --- builtin/common/filterlist.lua | 2 +- builtin/common/misc_helpers.lua | 29 +++++++++++++-------------- builtin/common/serialize.lua | 13 ++++++------ builtin/fstk/buttonbar.lua | 7 ++++++- builtin/fstk/tabview.lua | 2 +- builtin/game/auth.lua | 2 +- builtin/game/chatcommands.lua | 6 +++--- builtin/game/misc.lua | 10 ++++----- builtin/game/register.lua | 8 ++++---- builtin/mainmenu/common.lua | 4 ++-- builtin/mainmenu/modmgr.lua | 6 +++--- builtin/mainmenu/tab_mods.lua | 2 +- builtin/mainmenu/tab_texturepacks.lua | 2 +- 13 files changed, 48 insertions(+), 45 deletions(-) diff --git a/builtin/common/filterlist.lua b/builtin/common/filterlist.lua index 210681133..2a62362e3 100644 --- a/builtin/common/filterlist.lua +++ b/builtin/common/filterlist.lua @@ -189,7 +189,7 @@ function filterlist.process(self) for k,v in pairs(self.m_raw_list) do if self.m_filtercriteria == nil or self.m_filter_fct(v,self.m_filtercriteria) then - table.insert(self.m_processed_list,v) + self.m_processed_list[#self.m_processed_list + 1] = v end end diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua index 08a230431..e4653d41d 100644 --- a/builtin/common/misc_helpers.lua +++ b/builtin/common/misc_helpers.lua @@ -2,7 +2,6 @@ -------------------------------------------------------------------------------- -- Localize functions to avoid table lookups (better performance). -local table_insert = table.insert local string_sub, string_find = string.sub, string.find -------------------------------------------------------------------------------- @@ -94,13 +93,13 @@ function dump2(o, name, dumped) -- the form _G["table: 0xFFFFFFF"] keyStr = string.format("_G[%q]", tostring(k)) -- Dump key table - table_insert(t, dump2(k, keyStr, dumped)) + t[#t + 1] = dump2(k, keyStr, dumped) end else keyStr = basic_dump(k) end local vname = string.format("%s[%s]", name, keyStr) - table_insert(t, dump2(v, vname, dumped)) + t[#t + 1] = dump2(v, vname, dumped) end return string.format("%s = {}\n%s", name, table.concat(t)) end @@ -135,7 +134,7 @@ function dump(o, indent, nested, level) local t = {} local dumped_indexes = {} for i, v in ipairs(o) do - table_insert(t, dump(v, indent, nested, level + 1)) + t[#t + 1] = dump(v, indent, nested, level + 1) dumped_indexes[i] = true end for k, v in pairs(o) do @@ -144,7 +143,7 @@ function dump(o, indent, nested, level) k = "["..dump(k, indent, nested, level + 1).."]" end v = dump(v, indent, nested, level + 1) - table_insert(t, k.." = "..v) + t[#t + 1] = k.." = "..v end end nested[o] = nil @@ -177,7 +176,7 @@ function string.split(str, delim, include_empty, max_splits, sep_is_pattern) local s = string_sub(str, pos, np - 1) if include_empty or (s ~= "") then max_splits = max_splits - 1 - table_insert(items, s) + items[#items + 1] = s end pos = npe + 1 until (max_splits == 0) or (pos > (len + 1)) @@ -186,8 +185,8 @@ end -------------------------------------------------------------------------------- function table.indexof(list, val) - for i = 1, #list do - if list[i] == val then + for i, v in ipairs(list) do + if v == val then return i end end @@ -324,7 +323,7 @@ function core.splittext(text,charlimit) local last_line = "" while start ~= nil do if string.len(last_line) + (stop-start) > charlimit then - table_insert(retval, last_line) + retval[#retval + 1] = last_line last_line = "" end @@ -335,7 +334,7 @@ function core.splittext(text,charlimit) last_line = last_line .. string_sub(text, current_idx, stop - 1) if gotnewline then - table_insert(retval, last_line) + retval[#retval + 1] = last_line last_line = "" gotnewline = false end @@ -353,11 +352,11 @@ function core.splittext(text,charlimit) --add last part of text if string.len(last_line) + (string.len(text) - current_idx) > charlimit then - table_insert(retval, last_line) - table_insert(retval, string_sub(text, current_idx)) + retval[#retval + 1] = last_line + retval[#retval + 1] = string_sub(text, current_idx) else last_line = last_line .. " " .. string_sub(text, current_idx) - table_insert(retval, last_line) + retval[#retval + 1] = last_line end return retval @@ -430,14 +429,14 @@ if INIT == "game" then if iswall then core.set_node(pos, {name = wield_name, - param2 = dirs1[fdir+1]}) + param2 = dirs1[fdir + 1]}) elseif isceiling then if orient_flags.force_facedir then core.set_node(pos, {name = wield_name, param2 = 20}) else core.set_node(pos, {name = wield_name, - param2 = dirs2[fdir+1]}) + param2 = dirs2[fdir + 1]}) end else -- place right side up if orient_flags.force_facedir then diff --git a/builtin/common/serialize.lua b/builtin/common/serialize.lua index 90b8b2ad6..b2165648e 100644 --- a/builtin/common/serialize.lua +++ b/builtin/common/serialize.lua @@ -104,7 +104,7 @@ function core.serialize(x) local i = local_index local_index = local_index + 1 var = "_["..i.."]" - table.insert(local_defs, var.." = "..val) + local_defs[#local_defs + 1] = var.." = "..val dumped[x] = var return var end @@ -135,16 +135,15 @@ function core.serialize(x) local np = nest_points[x] for i, v in ipairs(x) do if not np or not np[i] then - table.insert(vals, dump_or_ref_val(v)) + vals[#vals + 1] = dump_or_ref_val(v) end idx_dumped[i] = true end for k, v in pairs(x) do if (not np or not np[k]) and not idx_dumped[k] then - table.insert(vals, - "["..dump_or_ref_val(k).."] = " - ..dump_or_ref_val(v)) + vals[#vals + 1] = "["..dump_or_ref_val(k).."] = " + ..dump_or_ref_val(v) end end return "{"..table.concat(vals, ", ").."}" @@ -156,9 +155,9 @@ function core.serialize(x) local function dump_nest_points() for parent, vals in pairs(nest_points) do for k, v in pairs(vals) do - table.insert(local_defs, dump_or_ref_val(parent) + local_defs[#local_defs + 1] = dump_or_ref_val(parent) .."["..dump_or_ref_val(k).."] = " - ..dump_or_ref_val(v)) + ..dump_or_ref_val(v) end end end diff --git a/builtin/fstk/buttonbar.lua b/builtin/fstk/buttonbar.lua index 1ad175624..465588324 100644 --- a/builtin/fstk/buttonbar.lua +++ b/builtin/fstk/buttonbar.lua @@ -145,7 +145,12 @@ local buttonbar_metatable = { if image == nil then image = "" end if tooltip == nil then tooltip = "" end - table.insert(self.buttons,{ name=name, caption=caption, image=image, tooltip=tooltip}) + self.buttons[#self.buttons + 1] = { + name = name, + caption = caption, + image = image, + tooltip = tooltip + } if self.orientation == "horizontal" then if ( (self.btn_size * #self.buttons) + (self.btn_size * 0.05 *2) > self.size.x ) then diff --git a/builtin/fstk/tabview.lua b/builtin/fstk/tabview.lua index c65f6dd7f..72551afd7 100644 --- a/builtin/fstk/tabview.lua +++ b/builtin/fstk/tabview.lua @@ -46,7 +46,7 @@ local function add_tab(self,tab) tabdata = {}, } - table.insert(self.tablist,newtab) + self.tablist[#self.tablist + 1] = newtab if self.last_tab_index == #self.tablist then self.current_tab = tab.name diff --git a/builtin/game/auth.lua b/builtin/game/auth.lua index 423eb3134..deb811b14 100644 --- a/builtin/game/auth.lua +++ b/builtin/game/auth.lua @@ -20,7 +20,7 @@ function core.privs_to_string(privs, delim) local list = {} for priv, bool in pairs(privs) do if bool then - table.insert(list, priv) + list[#list + 1] = priv end end return table.concat(list, delim) diff --git a/builtin/game/chatcommands.lua b/builtin/game/chatcommands.lua index 6b4ca0d12..2c7a0b532 100644 --- a/builtin/game/chatcommands.lua +++ b/builtin/game/chatcommands.lua @@ -116,7 +116,7 @@ core.register_chatcommand("help", { local cmds = {} for cmd, def in pairs(core.chatcommands) do if core.check_player_privs(name, def.privs) then - table.insert(cmds, cmd) + cmds[#cmds + 1] = cmd end end table.sort(cmds) @@ -127,7 +127,7 @@ core.register_chatcommand("help", { local cmds = {} for cmd, def in pairs(core.chatcommands) do if core.check_player_privs(name, def.privs) then - table.insert(cmds, format_help_line(cmd, def)) + cmds[#cmds + 1] = format_help_line(cmd, def) end end table.sort(cmds) @@ -135,7 +135,7 @@ core.register_chatcommand("help", { elseif param == "privs" then local privs = {} for priv, def in pairs(core.registered_privileges) do - table.insert(privs, priv .. ": " .. def.description) + privs[#privs + 1] = priv .. ": " .. def.description end table.sort(privs) return true, "Available privileges:\n"..table.concat(privs, "\n") diff --git a/builtin/game/misc.lua b/builtin/game/misc.lua index d0e67bd88..4f58710d0 100644 --- a/builtin/game/misc.lua +++ b/builtin/game/misc.lua @@ -40,12 +40,12 @@ end) function core.after(after, func, ...) assert(tonumber(time) and type(func) == "function", "Invalid core.after invocation") - table.insert(jobs, { + jobs[#jobs + 1] = { func = func, expire = time + after, arg = {...}, mod_origin = core.get_last_run_mod() - }) + } end function core.check_player_privs(player_or_name, ...) @@ -63,14 +63,14 @@ function core.check_player_privs(player_or_name, ...) -- We were provided with a table like { privA = true, privB = true }. for priv, value in pairs(requested_privs[1]) do if value and not player_privs[priv] then - table.insert(missing_privileges, priv) + missing_privileges[#missing_privileges + 1] = priv end end else -- Only a list, we can process it directly. for key, priv in pairs(requested_privs) do if not player_privs[priv] then - table.insert(missing_privileges, priv) + missing_privileges[#missing_privileges + 1] = priv end end end @@ -96,7 +96,7 @@ function core.get_connected_players() local temp_table = {} for index, value in pairs(player_list) do if value:is_player_connected() then - table.insert(temp_table, value) + temp_table[#temp_table + 1] = value end end return temp_table diff --git a/builtin/game/register.lua b/builtin/game/register.lua index ba5f69d67..398daf057 100644 --- a/builtin/game/register.lua +++ b/builtin/game/register.lua @@ -75,7 +75,7 @@ end function core.register_abm(spec) -- Add to core.registered_abms - core.registered_abms[#core.registered_abms+1] = spec + core.registered_abms[#core.registered_abms + 1] = spec spec.mod_origin = core.get_current_modname() or "??" end @@ -391,7 +391,7 @@ end local function make_registration() local t = {} local registerfunc = function(func) - table.insert(t, func) + t[#t + 1] = func core.callback_origins[func] = { mod = core.get_current_modname() or "??", name = debug.getinfo(1, "n").name or "??" @@ -467,9 +467,9 @@ end function core.register_on_player_hpchange(func, modifier) if modifier then - table.insert(core.registered_on_player_hpchanges.modifiers, func) + core.registered_on_player_hpchanges.modifiers[#core.registered_on_player_hpchanges.modifiers + 1] = func else - table.insert(core.registered_on_player_hpchanges.loggers, func) + core.registered_on_player_hpchanges.loggers[#core.registered_on_player_hpchanges.loggers + 1] = func end core.callback_origins[func] = { mod = core.get_current_modname() or "??", diff --git a/builtin/mainmenu/common.lua b/builtin/mainmenu/common.lua index f4020aaaf..f40c787a2 100644 --- a/builtin/mainmenu/common.lua +++ b/builtin/mainmenu/common.lua @@ -67,13 +67,13 @@ function order_favorite_list(list) for i=1,#list,1 do local fav = list[i] if is_server_protocol_compat(fav.proto_min, fav.proto_max) then - table.insert(res, fav) + res[#res + 1] = fav end end for i=1,#list,1 do local fav = list[i] if not is_server_protocol_compat(fav.proto_min, fav.proto_max) then - table.insert(res, fav) + res[#res + 1] = fav end end return res diff --git a/builtin/mainmenu/modmgr.lua b/builtin/mainmenu/modmgr.lua index 41e747b33..f996df7ba 100644 --- a/builtin/mainmenu/modmgr.lua +++ b/builtin/mainmenu/modmgr.lua @@ -23,7 +23,7 @@ function get_mods(path,retval,modpack) if name:sub(1, 1) ~= "." then local prefix = path .. DIR_DELIM .. name .. DIR_DELIM local toadd = {} - table.insert(retval, toadd) + retval[#retval + 1] = toadd local mod_conf = Settings(prefix .. "mod.conf"):to_table() if mod_conf.name then @@ -412,7 +412,7 @@ function modmgr.preparemodlist(data) for i=1,#global_mods,1 do global_mods[i].typ = "global_mod" - table.insert(retval,global_mods[i]) + retval[#retval + 1] = global_mods[i] end --read game mods @@ -421,7 +421,7 @@ function modmgr.preparemodlist(data) for i=1,#game_mods,1 do game_mods[i].typ = "game_mod" - table.insert(retval,game_mods[i]) + retval[#retval + 1] = game_mods[i] end if data.worldpath == nil then diff --git a/builtin/mainmenu/tab_mods.lua b/builtin/mainmenu/tab_mods.lua index 2ddc9b07c..af758f8df 100644 --- a/builtin/mainmenu/tab_mods.lua +++ b/builtin/mainmenu/tab_mods.lua @@ -78,7 +78,7 @@ local function get_formspec(tabview, name, tabdata) descriptionfile:close() else descriptionlines = {} - table.insert(descriptionlines,fgettext("No mod description available")) + descriptionlines[#descriptionlines + 1] = fgettext("No mod description available") end retval = retval .. diff --git a/builtin/mainmenu/tab_texturepacks.lua b/builtin/mainmenu/tab_texturepacks.lua index d8b9ba35f..d669d5682 100644 --- a/builtin/mainmenu/tab_texturepacks.lua +++ b/builtin/mainmenu/tab_texturepacks.lua @@ -20,7 +20,7 @@ local function filter_texture_pack_list(list) local retval = {} for _, item in ipairs(list) do if item ~= "base" then - table.insert(retval, item) + retval[#retval + 1] = item end end -- 2.25.1