Faster insertion into table
authorRui914 <rui914t@gmail.com>
Sun, 6 Mar 2016 15:53:45 +0000 (00:53 +0900)
committerparamat <mat.gregory@virginmedia.com>
Sun, 6 Mar 2016 23:42:04 +0000 (23:42 +0000)
13 files changed:
builtin/common/filterlist.lua
builtin/common/misc_helpers.lua
builtin/common/serialize.lua
builtin/fstk/buttonbar.lua
builtin/fstk/tabview.lua
builtin/game/auth.lua
builtin/game/chatcommands.lua
builtin/game/misc.lua
builtin/game/register.lua
builtin/mainmenu/common.lua
builtin/mainmenu/modmgr.lua
builtin/mainmenu/tab_mods.lua
builtin/mainmenu/tab_texturepacks.lua

index 2106811335faeff02485c0a101807deb992073e3..2a62362e3a8ef00d0197451bd9cf6b6fe6013fbc 100644 (file)
@@ -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
        
index 08a230431f874ef106350e29bb9455ecbd4f4b69..e4653d41dda1c53642f809a68594ffb0fde239f3 100644 (file)
@@ -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
index 90b8b2ad6a6e40ab4a695e36e97f484b4579fd15..b2165648ed472255ee281bdaccd851420a1df56c 100644 (file)
@@ -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
index 1ad175624afacb7306e18995d0967f3395e3d6e6..465588324a4ae8885ed6e3b085ac95c09552325c 100644 (file)
@@ -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
index c65f6dd7f985bf323b04df8d38f4e69d20672d4a..72551afd7e3ca5ed045b81e2a997808ed369d114 100644 (file)
@@ -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
index 423eb3134a29964b1f23a7738df408b620ad432c..deb811b1448133077e8af1e61020053ec3fcbf03 100644 (file)
@@ -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)
index 6b4ca0d12df4f4d6417296abfdcf107cc43549b6..2c7a0b532837a63881014c6827edeaae4c23b9ba 100644 (file)
@@ -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")
index d0e67bd8875aa10931e1328832ad67df61576c99..4f58710d0a4cce469923b45504e29bb78c8c5682 100644 (file)
@@ -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
index ba5f69d67defaab0f7ad940002e3d99255267b77..398daf057410c93716021ea4749d87919a18bdc3 100644 (file)
@@ -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 "??",
index f4020aaaf00c5adc46f079aff4d1a424fe8234ca..f40c787a22c477644fd3d6055f3d08afabb7b47e 100644 (file)
@@ -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
index 41e747b33ffd73d4b3fe19e9ac57db6e2843f570..f996df7baf23ac49dfff39e1eea46bb05b96e41e 100644 (file)
@@ -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
index 2ddc9b07cbfa516a18eb9a2963289b5f608d7cf6..af758f8dfb0c1fb48fca4b3102f9770d7d78b7ff 100644 (file)
@@ -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 ..
index d8b9ba35f2f28004c18dcf3585b26e4cb8fdc799..d669d5682349bb22bbc7cbb6901f53c2cf45bd2e 100644 (file)
@@ -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