rewrite parts of the teleportation tube to avoid a few grave performance issues:

* don't forceload the block of every single tube in the world on every item being send through any one of them
* cache file contents to avoid unnecessary reads on every single item send through a tp tube
* reduce redundant metadata and table lookups
* reduce write operations during the same changes to a file

still to do is a switch from an arraylist-table to a hashmap-table to make lookups and updates on the cache faster
This commit is contained in:
Tim 2015-01-20 22:58:46 +01:00 committed by Vanessa Ezekowitz
parent 9a83380b2b
commit 82a7b6a749

View File

@ -1,53 +1,59 @@
local filename=minetest.get_worldpath() .. "/teleport_tubes" local filename=minetest.get_worldpath() .. "/teleport_tubes"
local function read_file() local tp_tube_db = nil -- nil forces a read
local f = io.open(filename, "r")
if f == nil then return {} end
local t = f:read("*all")
f:close()
if t == "" or t == nil then return {} end
return minetest.deserialize(t)
end
local function write_file(tbl) local function read_tube_db()
local f = io.open(filename, "w") local file = io.open(filename, "r")
f:write(minetest.serialize(tbl)) if file ~= nil then
f:close() local file_content = file:read("*all")
end io.close(file)
local function update_pos_in_file(pos) if file_content and file_content ~= "" then
local tbl=read_file() tp_tube_db = minetest.deserialize(file_content)
for _, val in ipairs(tbl) do return tp_tube_db-- we read sucessfully
if val.x == pos.x and val.y == pos.y and val.z == pos.z then
local meta = minetest.get_meta(val)
val.channel = meta:get_string("channel")
val.cr = meta:get_int("can_receive")
end end
end end
write_file(tbl) tp_tube_db = {}
return tp_tube_db
end end
local function add_tube_in_file(pos,channel, cr) local function save_tube_db()
local tbl=read_file() local file, err = io.open(filename, "w")
for _,val in ipairs(tbl) do if file then
if val.x==pos.x and val.y==pos.y and val.z==pos.z then file:write(minetest.serialize(tp_tube_db))
io.close(file)
else
error(err)
end
end
-- updates or adds a tube
local function set_tube(pos, channel, can_receive)
local tubes = tp_tube_db or read_tube_db()
for _, val in ipairs(tubes) do
if val.x == pos.x and val.y == pos.y and val.z == pos.z then
val.channel = channel
val.cr = can_receive
save_tube_db()
return return
end end
end end
table.insert(tbl,{x=pos.x,y=pos.y,z=pos.z,channel=channel,cr=cr})
write_file(tbl) -- we haven't found any tp tube to update, so lets add it
table.insert(tp_tube_db,{x=pos.x,y=pos.y,z=pos.z,channel=channel,cr=cr})
save_tube_db()
end end
local function remove_tube_in_file(pos) local function remove_tube(pos)
local tbl = read_file() local tubes = tp_tube_db or read_tube_db()
local newtbl = {} local newtbl = {}
for _, val in ipairs(tbl) do for _, val in ipairs(tubes) do
if val.x ~= pos.x or val.y ~= pos.y or val.z ~= pos.z then if val.x ~= pos.x or val.y ~= pos.y or val.z ~= pos.z then
table.insert(newtbl, val) table.insert(newtbl, val)
end end
end end
write_file(newtbl) tp_tube_db = newtbl
save_tube_db()
end end
local function read_node_with_vm(pos) local function read_node_with_vm(pos)
@ -58,39 +64,35 @@ local function read_node_with_vm(pos)
return minetest.get_name_from_content_id(data[area:index(pos.x, pos.y, pos.z)]) return minetest.get_name_from_content_id(data[area:index(pos.x, pos.y, pos.z)])
end end
local function get_tubes_in_file(pos,channel) local function get_receivers(pos,channel)
local tbl = read_file() local tubes = tp_tube_db or read_tube_db()
local newtbl = {} local receivers = {}
local changed = false local changed = false
for _, val in ipairs(tbl) do for _, val in ipairs(tubes) do
local meta = minetest.get_meta(val) -- skip all non-receivers and the tube that it came from as early as possible, as this is called often
local name = read_node_with_vm(val) if (val.cr == 1 and val.channel == channel and (val.x ~= pos.x or val.y ~= pos.y or val.z ~= pos.z)) then
local is_loaded = (minetest.get_node_or_nil(val) ~= nil) local is_loaded = (minetest.get_node_or_nil(val) ~= nil)
local is_teleport_tube = minetest.registered_nodes[name] and minetest.registered_nodes[name].is_teleport_tube local node_name = is_loaded and minetest.get_node(pos).name or read_node_with_vm(val)
if is_teleport_tube then
if is_loaded and (val.channel ~= meta:get_string("channel") or val.cr ~= meta:get_int("can_receive")) then if minetest.registered_nodes[node_name] and minetest.registered_nodes[node_name].is_teleport_tube then
val.channel = meta:get_string("channel") table.insert(receivers, val)
val.cr = meta:get_int("can_receive") else
val.to_remove = true
changed = true changed = true
end end
if val.cr == 1 and val.channel == channel and (val.x ~= pos.x or val.y ~= pos.y or val.z ~= pos.z) then
table.insert(newtbl, val)
end
else
val.to_remove = true
changed = true
end end
end end
if changed then if changed then
local updated = {} local updated = {}
for _, val in ipairs(tbl) do for _, val in ipairs(tubes) do
if not val.to_remove then if not val.to_remove then
table.insert(updated, val) table.insert(updated, val)
end end
end end
write_file(updated) tp_tube_db = updated
save_tube_db()
end end
return newtbl return receivers
end end
local teleport_noctr_textures={"pipeworks_teleport_tube_noctr.png","pipeworks_teleport_tube_noctr.png","pipeworks_teleport_tube_noctr.png", local teleport_noctr_textures={"pipeworks_teleport_tube_noctr.png","pipeworks_teleport_tube_noctr.png","pipeworks_teleport_tube_noctr.png",
@ -102,11 +104,11 @@ local teleport_end_textures={"pipeworks_teleport_tube_end.png","pipeworks_telepo
local teleport_short_texture="pipeworks_teleport_tube_short.png" local teleport_short_texture="pipeworks_teleport_tube_short.png"
local teleport_inv_texture="pipeworks_teleport_tube_inv.png" local teleport_inv_texture="pipeworks_teleport_tube_inv.png"
local function set_teleport_tube_formspec(meta) local function set_teleport_tube_formspec(meta, can_receive)
local cr = meta:get_int("can_receive") ~= 0 local cr = (can_receive ~= 0)
meta:set_string("formspec","size[10.5,1;]".. meta:set_string("formspec","size[10.5,1;]"..
"field[0,0.5;7,1;channel;Channel:;${channel}]".. "field[0,0.5;7,1;channel;Channel:;${channel}]"..
"button[8,0;2.5,1;"..(cr and "cr0" or "cr1")..";".. "button[8,0;2.5,1;"..(cr and "cr0" or "cr1")..";"..
(cr and "Send and Receive" or "Send only").."]") (cr and "Send and Receive" or "Send only").."]")
end end
@ -120,7 +122,7 @@ pipeworks.register_tube("pipeworks:teleport_tube","Teleporting Pneumatic Tube Se
velocity.z = 0 velocity.z = 0
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local channel = meta:get_string("channel") local channel = meta:get_string("channel")
local target = get_tubes_in_file(pos,channel) local target = get_receivers(pos, channel)
if target[1] == nil then return {} end if target[1] == nil then return {} end
local d = math.random(1,#target) local d = math.random(1,#target)
pos.x = target[d].x pos.x = target[d].x
@ -133,48 +135,57 @@ pipeworks.register_tube("pipeworks:teleport_tube","Teleporting Pneumatic Tube Se
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("channel","") meta:set_string("channel","")
meta:set_int("can_receive",1) meta:set_int("can_receive",1)
add_tube_in_file(pos,"") set_tube(pos, "", 1)
set_teleport_tube_formspec(meta) set_teleport_tube_formspec(meta, 1)
end, end,
on_receive_fields = function(pos,formname,fields,sender) on_receive_fields = function(pos,formname,fields,sender)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local can_receive = meta:get_int("can_receive")
--check for private channels if fields.channel then
if fields.channel ~= nil then -- check for private channels
local sender_name = sender:get_player_name()
local name, mode = fields.channel:match("^([^:;]+)([:;])") local name, mode = fields.channel:match("^([^:;]+)([:;])")
if name and mode and name ~= sender:get_player_name() then if name and mode and name ~= sender_name then
--channels starting with '[name]:' can only be used by the named player --channels starting with '[name]:' can only be used by the named player
if mode == ":" then if mode == ":" then
minetest.chat_send_player(sender:get_player_name(), "Sorry, channel '"..fields.channel.."' is reserved for exclusive use by "..name) minetest.chat_send_player(sender_name, "Sorry, channel '"..fields.channel.."' is reserved for exclusive use by "..name)
return return
--channels starting with '[name];' can be used by other players, but cannot be received from --channels starting with '[name];' can be used by other players, but cannot be received from
elseif mode == ";" and (fields.cr1 or (meta:get_int("can_receive") ~= 0 and not fields.cr0)) then elseif mode == ";" and (fields.cr1 or (can_receive ~= 0 and not fields.cr0)) then
minetest.chat_send_player(sender:get_player_name(), "Sorry, receiving from channel '"..fields.channel.."' is reserved for "..name) minetest.chat_send_player(sender_name, "Sorry, receiving from channel '"..fields.channel.."' is reserved for "..name)
return return
end end
end end
-- save channel if we set one
meta:set_string("channel", fields.channel)
end
-- make sure we have a channel, either the newly set one or the one from metadata
local channel = fields.channel or meta:get_string("channel")
if fields.cr0 and can_receive ~= 0 then
can_receive = 0
meta:set_int("can_receive", can_receive)
elseif fields.cr1 and can_receive ~= 1 then
can_receive = 1
meta:set_int("can_receive", can_receive)
end end
if fields.channel==nil then fields.channel=meta:get_string("channel") end set_tube(pos, fields.channel, can_receive)
meta:set_string("channel",fields.channel) set_teleport_tube_formspec(meta, can_receive)
remove_tube_in_file(pos)
if fields.cr0 then meta:set_int("can_receive", 0) end
if fields.cr1 then meta:set_int("can_receive", 1) end
local cr = meta:get_int("can_receive")
add_tube_in_file(pos, fields.channel, meta:get_int("can_receive"))
set_teleport_tube_formspec(meta)
end, end,
on_destruct = function(pos) on_destruct = function(pos)
remove_tube_in_file(pos) remove_tube(pos)
end}) end
})
if minetest.get_modpath("mesecons_mvps") ~= nil then if minetest.get_modpath("mesecons_mvps") ~= nil then
mesecon.register_on_mvps_move(function(moved_nodes) mesecon.register_on_mvps_move(function(moved_nodes)
for _, n in ipairs(moved_nodes) do for _, n in ipairs(moved_nodes) do
if string.find(n.node.name, "pipeworks:teleport_tube") ~= nil then if string.find(n.node.name, "pipeworks:teleport_tube") ~= nil then
update_pos_in_file(n.pos) local meta = minetest.get_meta(n.pos)
set_tube(n.pos, meta:get_string("channel"), meta:get_int("can_receive"))
end end
end end
end) end)