Faster insertion into table

This commit is contained in:
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
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

View 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

View 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

View 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

View 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

View 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)

View 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")

View 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

View 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 "??",

View 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

View 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

View 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 ..

View 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