From 90cacc0184b47f79ddc765822218582936e2e737 Mon Sep 17 00:00:00 2001 From: gregorycu Date: Tue, 3 Nov 2015 16:08:41 +1100 Subject: [PATCH] Make spreadLight 2.5x faster --- src/map.cpp | 28 +++++++++++++++++----------- src/map.h | 2 +- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/map.cpp b/src/map.cpp index 5583d78d..caed758d 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -432,7 +432,7 @@ void Map::unLightNeighbors(enum LightBank bank, goes on recursively. */ void Map::spreadLight(enum LightBank bank, - std::set & from_nodes, + std::vector & from_nodes, std::map & modified_blocks) { INodeDefManager *nodemgr = m_gamedef->ndef(); @@ -451,7 +451,8 @@ void Map::spreadLight(enum LightBank bank, u32 blockchangecount = 0; - std::set lighted_nodes; + std::vector lighted_nodes; + lighted_nodes.reserve(100); /* Initialize block cache @@ -461,7 +462,7 @@ void Map::spreadLight(enum LightBank bank, // Cache this a bit, too bool block_checked_in_modified = false; - for(std::set::iterator j = from_nodes.begin(); + for(std::vector::iterator j = from_nodes.begin(); j != from_nodes.end(); ++j) { v3s16 pos = *j; @@ -528,7 +529,7 @@ void Map::spreadLight(enum LightBank bank, */ if(n2.getLight(bank, nodemgr) > undiminish_light(oldlight)) { - lighted_nodes.insert(n2pos); + lighted_nodes.push_back(n2pos); changed = true; } /* @@ -541,7 +542,7 @@ void Map::spreadLight(enum LightBank bank, { n2.setLight(bank, newlight, nodemgr); block->setNode(relpos, n2); - lighted_nodes.insert(n2pos); + lighted_nodes.push_back(n2pos); changed = true; } } @@ -564,6 +565,9 @@ void Map::spreadLight(enum LightBank bank, <<" for "< & modified_blocks) { - std::set from_nodes; - from_nodes.insert(pos); + std::vector from_nodes; + from_nodes.push_back(pos); spreadLight(bank, from_nodes, modified_blocks); } @@ -827,7 +831,8 @@ void Map::updateLighting(enum LightBank bank, { //TimeTaker timer("spreadLight"); - spreadLight(bank, light_sources, modified_blocks); + std::vector light_sources_vec(light_sources.begin(), light_sources.end()); + spreadLight(bank, light_sources_vec, modified_blocks); } /*if(debug) @@ -1060,7 +1065,8 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n, /* Spread light from all nodes that might be capable of doing so */ - spreadLight(bank, light_sources, modified_blocks); + std::vector light_sources_vec(light_sources.begin(), light_sources.end()); + spreadLight(bank, light_sources_vec, modified_blocks); } /* @@ -1183,7 +1189,8 @@ void Map::removeNodeAndUpdate(v3s16 p, /* Recalculate lighting */ - spreadLight(bank, light_sources, modified_blocks); + std::vector light_sources_vec(light_sources.begin(), light_sources.end()); + spreadLight(bank, light_sources_vec, modified_blocks); } // Add the block of the removed node to modified_blocks @@ -1619,7 +1626,6 @@ s32 Map::transforming_liquid_size() { void Map::transformLiquids(std::map & modified_blocks) { - INodeDefManager *nodemgr = m_gamedef->ndef(); DSTACK(FUNCTION_NAME); diff --git a/src/map.h b/src/map.h index 293a8920..7e481df4 100644 --- a/src/map.h +++ b/src/map.h @@ -220,7 +220,7 @@ public: std::map & modified_blocks); void spreadLight(enum LightBank bank, - std::set & from_nodes, + std::vector & from_nodes, std::map & modified_blocks); void lightNeighbors(enum LightBank bank,