Faster insertion into table

master
Rui914 2016-03-07 00:53:45 +09:00 committed by paramat
parent 75db0543f3
commit 24e8b0ac1e
13 changed files with 48 additions and 45 deletions

View File

@ -189,7 +189,7 @@ function filterlist.process(self)
for k,v in pairs(self.m_raw_list) do for k,v in pairs(self.m_raw_list) do
if self.m_filtercriteria == nil or if self.m_filtercriteria == nil or
self.m_filter_fct(v,self.m_filtercriteria) then 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
end end

View File

@ -2,7 +2,6 @@
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Localize functions to avoid table lookups (better performance). -- Localize functions to avoid table lookups (better performance).
local table_insert = table.insert
local string_sub, string_find = string.sub, string.find local string_sub, string_find = string.sub, string.find
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -94,13 +93,13 @@ function dump2(o, name, dumped)
-- the form _G["table: 0xFFFFFFF"] -- the form _G["table: 0xFFFFFFF"]
keyStr = string.format("_G[%q]", tostring(k)) keyStr = string.format("_G[%q]", tostring(k))
-- Dump key table -- Dump key table
table_insert(t, dump2(k, keyStr, dumped)) t[#t + 1] = dump2(k, keyStr, dumped)
end end
else else
keyStr = basic_dump(k) keyStr = basic_dump(k)
end end
local vname = string.format("%s[%s]", name, keyStr) local vname = string.format("%s[%s]", name, keyStr)
table_insert(t, dump2(v, vname, dumped)) t[#t + 1] = dump2(v, vname, dumped)
end end
return string.format("%s = {}\n%s", name, table.concat(t)) return string.format("%s = {}\n%s", name, table.concat(t))
end end
@ -135,7 +134,7 @@ function dump(o, indent, nested, level)
local t = {} local t = {}
local dumped_indexes = {} local dumped_indexes = {}
for i, v in ipairs(o) do 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 dumped_indexes[i] = true
end end
for k, v in pairs(o) do for k, v in pairs(o) do
@ -144,7 +143,7 @@ function dump(o, indent, nested, level)
k = "["..dump(k, indent, nested, level + 1).."]" k = "["..dump(k, indent, nested, level + 1).."]"
end end
v = dump(v, indent, nested, level + 1) v = dump(v, indent, nested, level + 1)
table_insert(t, k.." = "..v) t[#t + 1] = k.." = "..v
end end
end end
nested[o] = nil 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) local s = string_sub(str, pos, np - 1)
if include_empty or (s ~= "") then if include_empty or (s ~= "") then
max_splits = max_splits - 1 max_splits = max_splits - 1
table_insert(items, s) items[#items + 1] = s
end end
pos = npe + 1 pos = npe + 1
until (max_splits == 0) or (pos > (len + 1)) until (max_splits == 0) or (pos > (len + 1))
@ -186,8 +185,8 @@ end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function table.indexof(list, val) function table.indexof(list, val)
for i = 1, #list do for i, v in ipairs(list) do
if list[i] == val then if v == val then
return i return i
end end
end end
@ -324,7 +323,7 @@ function core.splittext(text,charlimit)
local last_line = "" local last_line = ""
while start ~= nil do while start ~= nil do
if string.len(last_line) + (stop-start) > charlimit then if string.len(last_line) + (stop-start) > charlimit then
table_insert(retval, last_line) retval[#retval + 1] = last_line
last_line = "" last_line = ""
end end
@ -335,7 +334,7 @@ function core.splittext(text,charlimit)
last_line = last_line .. string_sub(text, current_idx, stop - 1) last_line = last_line .. string_sub(text, current_idx, stop - 1)
if gotnewline then if gotnewline then
table_insert(retval, last_line) retval[#retval + 1] = last_line
last_line = "" last_line = ""
gotnewline = false gotnewline = false
end end
@ -353,11 +352,11 @@ function core.splittext(text,charlimit)
--add last part of text --add last part of text
if string.len(last_line) + (string.len(text) - current_idx) > charlimit then if string.len(last_line) + (string.len(text) - current_idx) > charlimit then
table_insert(retval, last_line) retval[#retval + 1] = last_line
table_insert(retval, string_sub(text, current_idx)) retval[#retval + 1] = string_sub(text, current_idx)
else else
last_line = last_line .. " " .. string_sub(text, current_idx) last_line = last_line .. " " .. string_sub(text, current_idx)
table_insert(retval, last_line) retval[#retval + 1] = last_line
end end
return retval return retval
@ -430,14 +429,14 @@ if INIT == "game" then
if iswall then if iswall then
core.set_node(pos, {name = wield_name, core.set_node(pos, {name = wield_name,
param2 = dirs1[fdir+1]}) param2 = dirs1[fdir + 1]})
elseif isceiling then elseif isceiling then
if orient_flags.force_facedir then if orient_flags.force_facedir then
core.set_node(pos, {name = wield_name, core.set_node(pos, {name = wield_name,
param2 = 20}) param2 = 20})
else else
core.set_node(pos, {name = wield_name, core.set_node(pos, {name = wield_name,
param2 = dirs2[fdir+1]}) param2 = dirs2[fdir + 1]})
end end
else -- place right side up else -- place right side up
if orient_flags.force_facedir then if orient_flags.force_facedir then

View File

@ -104,7 +104,7 @@ function core.serialize(x)
local i = local_index local i = local_index
local_index = local_index + 1 local_index = local_index + 1
var = "_["..i.."]" var = "_["..i.."]"
table.insert(local_defs, var.." = "..val) local_defs[#local_defs + 1] = var.." = "..val
dumped[x] = var dumped[x] = var
return var return var
end end
@ -135,16 +135,15 @@ function core.serialize(x)
local np = nest_points[x] local np = nest_points[x]
for i, v in ipairs(x) do for i, v in ipairs(x) do
if not np or not np[i] then 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 end
idx_dumped[i] = true idx_dumped[i] = true
end end
for k, v in pairs(x) do for k, v in pairs(x) do
if (not np or not np[k]) and if (not np or not np[k]) and
not idx_dumped[k] then not idx_dumped[k] then
table.insert(vals, vals[#vals + 1] = "["..dump_or_ref_val(k).."] = "
"["..dump_or_ref_val(k).."] = " ..dump_or_ref_val(v)
..dump_or_ref_val(v))
end end
end end
return "{"..table.concat(vals, ", ").."}" return "{"..table.concat(vals, ", ").."}"
@ -156,9 +155,9 @@ function core.serialize(x)
local function dump_nest_points() local function dump_nest_points()
for parent, vals in pairs(nest_points) do for parent, vals in pairs(nest_points) do
for k, v in pairs(vals) 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(k).."] = "
..dump_or_ref_val(v)) ..dump_or_ref_val(v)
end end
end end
end end

View File

@ -145,7 +145,12 @@ local buttonbar_metatable = {
if image == nil then image = "" end if image == nil then image = "" end
if tooltip == nil then tooltip = "" 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.orientation == "horizontal" then
if ( (self.btn_size * #self.buttons) + (self.btn_size * 0.05 *2) if ( (self.btn_size * #self.buttons) + (self.btn_size * 0.05 *2)
> self.size.x ) then > self.size.x ) then

View File

@ -46,7 +46,7 @@ local function add_tab(self,tab)
tabdata = {}, tabdata = {},
} }
table.insert(self.tablist,newtab) self.tablist[#self.tablist + 1] = newtab
if self.last_tab_index == #self.tablist then if self.last_tab_index == #self.tablist then
self.current_tab = tab.name self.current_tab = tab.name

View File

@ -20,7 +20,7 @@ function core.privs_to_string(privs, delim)
local list = {} local list = {}
for priv, bool in pairs(privs) do for priv, bool in pairs(privs) do
if bool then if bool then
table.insert(list, priv) list[#list + 1] = priv
end end
end end
return table.concat(list, delim) return table.concat(list, delim)

View File

@ -116,7 +116,7 @@ core.register_chatcommand("help", {
local cmds = {} local cmds = {}
for cmd, def in pairs(core.chatcommands) do for cmd, def in pairs(core.chatcommands) do
if core.check_player_privs(name, def.privs) then if core.check_player_privs(name, def.privs) then
table.insert(cmds, cmd) cmds[#cmds + 1] = cmd
end end
end end
table.sort(cmds) table.sort(cmds)
@ -127,7 +127,7 @@ core.register_chatcommand("help", {
local cmds = {} local cmds = {}
for cmd, def in pairs(core.chatcommands) do for cmd, def in pairs(core.chatcommands) do
if core.check_player_privs(name, def.privs) then 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
end end
table.sort(cmds) table.sort(cmds)
@ -135,7 +135,7 @@ core.register_chatcommand("help", {
elseif param == "privs" then elseif param == "privs" then
local privs = {} local privs = {}
for priv, def in pairs(core.registered_privileges) do for priv, def in pairs(core.registered_privileges) do
table.insert(privs, priv .. ": " .. def.description) privs[#privs + 1] = priv .. ": " .. def.description
end end
table.sort(privs) table.sort(privs)
return true, "Available privileges:\n"..table.concat(privs, "\n") return true, "Available privileges:\n"..table.concat(privs, "\n")

View File

@ -40,12 +40,12 @@ end)
function core.after(after, func, ...) function core.after(after, func, ...)
assert(tonumber(time) and type(func) == "function", assert(tonumber(time) and type(func) == "function",
"Invalid core.after invocation") "Invalid core.after invocation")
table.insert(jobs, { jobs[#jobs + 1] = {
func = func, func = func,
expire = time + after, expire = time + after,
arg = {...}, arg = {...},
mod_origin = core.get_last_run_mod() mod_origin = core.get_last_run_mod()
}) }
end end
function core.check_player_privs(player_or_name, ...) 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 }. -- We were provided with a table like { privA = true, privB = true }.
for priv, value in pairs(requested_privs[1]) do for priv, value in pairs(requested_privs[1]) do
if value and not player_privs[priv] then if value and not player_privs[priv] then
table.insert(missing_privileges, priv) missing_privileges[#missing_privileges + 1] = priv
end end
end end
else else
-- Only a list, we can process it directly. -- Only a list, we can process it directly.
for key, priv in pairs(requested_privs) do for key, priv in pairs(requested_privs) do
if not player_privs[priv] then if not player_privs[priv] then
table.insert(missing_privileges, priv) missing_privileges[#missing_privileges + 1] = priv
end end
end end
end end
@ -96,7 +96,7 @@ function core.get_connected_players()
local temp_table = {} local temp_table = {}
for index, value in pairs(player_list) do for index, value in pairs(player_list) do
if value:is_player_connected() then if value:is_player_connected() then
table.insert(temp_table, value) temp_table[#temp_table + 1] = value
end end
end end
return temp_table return temp_table

View File

@ -75,7 +75,7 @@ end
function core.register_abm(spec) function core.register_abm(spec)
-- Add to core.registered_abms -- 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 "??" spec.mod_origin = core.get_current_modname() or "??"
end end
@ -391,7 +391,7 @@ end
local function make_registration() local function make_registration()
local t = {} local t = {}
local registerfunc = function(func) local registerfunc = function(func)
table.insert(t, func) t[#t + 1] = func
core.callback_origins[func] = { core.callback_origins[func] = {
mod = core.get_current_modname() or "??", mod = core.get_current_modname() or "??",
name = debug.getinfo(1, "n").name or "??" name = debug.getinfo(1, "n").name or "??"
@ -467,9 +467,9 @@ end
function core.register_on_player_hpchange(func, modifier) function core.register_on_player_hpchange(func, modifier)
if modifier then 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 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 end
core.callback_origins[func] = { core.callback_origins[func] = {
mod = core.get_current_modname() or "??", mod = core.get_current_modname() or "??",

View File

@ -67,13 +67,13 @@ function order_favorite_list(list)
for i=1,#list,1 do for i=1,#list,1 do
local fav = list[i] local fav = list[i]
if is_server_protocol_compat(fav.proto_min, fav.proto_max) then if is_server_protocol_compat(fav.proto_min, fav.proto_max) then
table.insert(res, fav) res[#res + 1] = fav
end end
end end
for i=1,#list,1 do for i=1,#list,1 do
local fav = list[i] local fav = list[i]
if not is_server_protocol_compat(fav.proto_min, fav.proto_max) then if not is_server_protocol_compat(fav.proto_min, fav.proto_max) then
table.insert(res, fav) res[#res + 1] = fav
end end
end end
return res return res

View File

@ -23,7 +23,7 @@ function get_mods(path,retval,modpack)
if name:sub(1, 1) ~= "." then if name:sub(1, 1) ~= "." then
local prefix = path .. DIR_DELIM .. name .. DIR_DELIM local prefix = path .. DIR_DELIM .. name .. DIR_DELIM
local toadd = {} local toadd = {}
table.insert(retval, toadd) retval[#retval + 1] = toadd
local mod_conf = Settings(prefix .. "mod.conf"):to_table() local mod_conf = Settings(prefix .. "mod.conf"):to_table()
if mod_conf.name then if mod_conf.name then
@ -412,7 +412,7 @@ function modmgr.preparemodlist(data)
for i=1,#global_mods,1 do for i=1,#global_mods,1 do
global_mods[i].typ = "global_mod" global_mods[i].typ = "global_mod"
table.insert(retval,global_mods[i]) retval[#retval + 1] = global_mods[i]
end end
--read game mods --read game mods
@ -421,7 +421,7 @@ function modmgr.preparemodlist(data)
for i=1,#game_mods,1 do for i=1,#game_mods,1 do
game_mods[i].typ = "game_mod" game_mods[i].typ = "game_mod"
table.insert(retval,game_mods[i]) retval[#retval + 1] = game_mods[i]
end end
if data.worldpath == nil then if data.worldpath == nil then

View File

@ -78,7 +78,7 @@ local function get_formspec(tabview, name, tabdata)
descriptionfile:close() descriptionfile:close()
else else
descriptionlines = {} descriptionlines = {}
table.insert(descriptionlines,fgettext("No mod description available")) descriptionlines[#descriptionlines + 1] = fgettext("No mod description available")
end end
retval = retval .. retval = retval ..

View File

@ -20,7 +20,7 @@ local function filter_texture_pack_list(list)
local retval = {} local retval = {}
for _, item in ipairs(list) do for _, item in ipairs(list) do
if item ~= "base" then if item ~= "base" then
table.insert(retval, item) retval[#retval + 1] = item
end end
end end