From 78e936bea7831e9a0d253b72ca8a63da558c9e81 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Tue, 31 Jan 2017 20:52:02 +0100 Subject: Replace all table indices that were pos_to_string strings with node hashes. Lua table string lookup consumes about 10 times more time than numeric indices --- advtrains/advtrains/atc.lua | 15 +++++++++++---- advtrains/advtrains/tracks.lua | 6 +++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/advtrains/advtrains/atc.lua b/advtrains/advtrains/atc.lua index dcddc65..7c2cec0 100644 --- a/advtrains/advtrains/atc.lua +++ b/advtrains/advtrains/atc.lua @@ -5,7 +5,14 @@ local atc={} -- ATC persistence table. advtrains.atc is created by init.lua when it loads the save file. atc.controllers = {} function atc.load_data(data) - atc.controllers = data and data.controllers or {} + local temp = data and data.controllers or {} + --transcode atc controller data to node hashes: table access times for numbers are far less than for strings + for pts, data in pairs(temp) do + if type(pts)="string" then + pts=minetest.hash_node_position(minetest.pos_to_string(pts)) + end + atc.controllers[pts] = data + end end function atc.save_data() return {controllers = atc.controllers} @@ -21,7 +28,7 @@ end --general function atc.send_command(pos) - local pts=minetest.pos_to_string(pos) + local pts=minetest.hash_node_position(ppos) if atc.controllers[pts] then --atprint("Called send_command at "..pts) local train_id = advtrains.detector.on_node[pts] @@ -81,7 +88,7 @@ advtrains.register_tracks("default", { after_dig_node=function(pos) advtrains.invalidate_all_paths() advtrains.ndb.clear(pos) - local pts=minetest.pos_to_string(pos) + local pts=minetest.hash_node_position(ppos) atc.controllers[pts]=nil end, on_receive_fields = function(pos, formname, fields, player) @@ -115,7 +122,7 @@ advtrains.register_tracks("default", { end meta:set_string("formspec", atc.get_atc_controller_formspec(pos, meta)) - local pts=minetest.pos_to_string(pos) + local pts=minetest.hash_node_position(pos) local _, conn1=advtrains.get_rail_info_at(pos, advtrains.all_tracktypes) atc.controllers[pts]={command=fields.command, arrowconn=conn1} atc.send_command(pos) diff --git a/advtrains/advtrains/tracks.lua b/advtrains/advtrains/tracks.lua index a44acb3..2c5c121 100644 --- a/advtrains/advtrains/tracks.lua +++ b/advtrains/advtrains/tracks.lua @@ -389,19 +389,19 @@ advtrains.detector.on_node = {} function advtrains.detector.enter_node(pos, train_id) local ppos=advtrains.round_vector_floor_y(pos) - local pts=minetest.pos_to_string(ppos) + local pts=minetest.hash_node_position(ppos) advtrains.detector.on_node[pts]=train_id advtrains.detector.call_enter_callback(ppos, train_id) end function advtrains.detector.leave_node(pos, train_id) local ppos=advtrains.round_vector_floor_y(pos) - local pts=minetest.pos_to_string(ppos) + local pts=minetest.hash_node_position(ppos) advtrains.detector.on_node[pts]=nil advtrains.detector.call_leave_callback(ppos, train_id) end function advtrains.detector.stay_node(pos, train_id) local ppos=advtrains.round_vector_floor_y(pos) - local pts=minetest.pos_to_string(ppos) + local pts=minetest.hash_node_position(ppos) advtrains.detector.on_node[pts]=train_id end -- cgit v1.2.3