Replace std::list by std::vector into ClientMap::updateDrawList, Map::timerUpdate...
authorLoic Blot <loic.blot@unix-experience.fr>
Tue, 17 Feb 2015 14:28:49 +0000 (15:28 +0100)
committerLoic Blot <loic.blot@unix-experience.fr>
Tue, 17 Feb 2015 14:28:49 +0000 (15:28 +0100)
This will speedup the loop reading into those functions

src/clientmap.cpp
src/map.cpp
src/mapblock.h
src/mapsector.cpp
src/mapsector.h

index 2db901f226c4d4311362020de568cffc18e1dae3..4ceb7bc9037f1761d51c49d74c7150e6051f2be1 100644 (file)
@@ -247,7 +247,7 @@ void ClientMap::updateDrawList(video::IVideoDriver* driver)
                                continue;
                }
 
-               std::list< MapBlock * > sectorblocks;
+               MapBlockVect sectorblocks;
                sector->getBlocks(sectorblocks);
 
                /*
@@ -256,8 +256,8 @@ void ClientMap::updateDrawList(video::IVideoDriver* driver)
 
                u32 sector_blocks_drawn = 0;
 
-               std::list< MapBlock * >::iterator i;
-               for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++)
+               for(MapBlockVect::iterator i = sectorblocks.begin();
+                               i != sectorblocks.end(); i++)
                {
                        MapBlock *block = *i;
 
index 191b05ed5601afe332977109e63e8140d44d48c4..0877d8c8ef7796510c2637b312b75ce7fb1efbdd 100644 (file)
@@ -1440,23 +1440,20 @@ void Map::timerUpdate(float dtime, float unload_timeout,
 
                bool all_blocks_deleted = true;
 
-               std::list<MapBlock*> blocks;
+               MapBlockVect blocks;
                sector->getBlocks(blocks);
 
-               for(std::list<MapBlock*>::iterator i = blocks.begin();
-                               i != blocks.end(); ++i)
-               {
+               for(MapBlockVect::iterator i = blocks.begin();
+                               i != blocks.end(); ++i) {
                        MapBlock *block = (*i);
 
                        block->incrementUsageTimer(dtime);
 
-                       if(block->refGet() == 0 && block->getUsageTimer() > unload_timeout)
-                       {
+                       if(block->refGet() == 0 && block->getUsageTimer() > unload_timeout) {
                                v3s16 p = block->getPos();
 
                                // Save if modified
-                               if (block->getModified() != MOD_STATE_CLEAN && save_before_unloading)
-                               {
+                               if (block->getModified() != MOD_STATE_CLEAN && save_before_unloading) {
                                        modprofiler.add(block->getModifiedReason(), 1);
                                        if (!saveBlock(block))
                                                continue;
@@ -1471,15 +1468,13 @@ void Map::timerUpdate(float dtime, float unload_timeout,
 
                                deleted_blocks_count++;
                        }
-                       else
-                       {
+                       else {
                                all_blocks_deleted = false;
                                block_count_all++;
                        }
                }
 
-               if(all_blocks_deleted)
-               {
+               if(all_blocks_deleted) {
                        sector_deletion_queue.push_back(si->first);
                }
        }
@@ -2982,8 +2977,7 @@ std::string ServerMap::getBlockFilename(v3s16 p)
 void ServerMap::save(ModifiedState save_level)
 {
        DSTACK(__FUNCTION_NAME);
-       if(m_map_saving_enabled == false)
-       {
+       if(m_map_saving_enabled == false) {
                infostream<<"WARNING: Not saving map, saving disabled."<<std::endl;
                return;
        }
@@ -2992,8 +2986,7 @@ void ServerMap::save(ModifiedState save_level)
                infostream<<"ServerMap: Saving whole map, this can take time."
                                <<std::endl;
 
-       if(m_map_metadata_changed || save_level == MOD_STATE_CLEAN)
-       {
+       if(m_map_metadata_changed || save_level == MOD_STATE_CLEAN) {
                saveMapMeta();
        }
 
@@ -3008,30 +3001,27 @@ void ServerMap::save(ModifiedState save_level)
        bool save_started = false;
 
        for(std::map<v2s16, MapSector*>::iterator i = m_sectors.begin();
-               i != m_sectors.end(); ++i)
-       {
+               i != m_sectors.end(); ++i) {
                ServerMapSector *sector = (ServerMapSector*)i->second;
                assert(sector->getId() == MAPSECTOR_SERVER);
 
-               if(sector->differs_from_disk || save_level == MOD_STATE_CLEAN)
-               {
+               if(sector->differs_from_disk || save_level == MOD_STATE_CLEAN) {
                        saveSectorMeta(sector);
                        sector_meta_count++;
                }
-               std::list<MapBlock*> blocks;
+
+               MapBlockVect blocks;
                sector->getBlocks(blocks);
 
-               for(std::list<MapBlock*>::iterator j = blocks.begin();
-                       j != blocks.end(); ++j)
-               {
+               for(MapBlockVect::iterator j = blocks.begin();
+                       j != blocks.end(); ++j) {
                        MapBlock *block = *j;
 
                        block_count_all++;
 
-                       if(block->getModified() >= (u32)save_level)
-                       {
+                       if(block->getModified() >= (u32)save_level) {
                                // Lazy beginSave()
-                               if(!save_started){
+                               if(!save_started) {
                                        beginSave();
                                        save_started = true;
                                }
@@ -3049,6 +3039,7 @@ void ServerMap::save(ModifiedState save_level)
                        }
                }
        }
+
        if(save_started)
                endSave();
 
@@ -3056,8 +3047,7 @@ void ServerMap::save(ModifiedState save_level)
                Only print if something happened or saved whole map
        */
        if(save_level == MOD_STATE_CLEAN || sector_meta_count != 0
-                       || block_count != 0)
-       {
+                       || block_count != 0) {
                infostream<<"ServerMap: Written: "
                                <<sector_meta_count<<" sector metadata files, "
                                <<block_count<<" block files"
@@ -3085,14 +3075,12 @@ void ServerMap::listAllLoadedBlocks(std::vector<v3s16> &dst)
        {
                MapSector *sector = si->second;
 
-               std::list<MapBlock*> blocks;
+               MapBlockVect blocks;
                sector->getBlocks(blocks);
 
-               for(std::list<MapBlock*>::iterator i = blocks.begin();
-                               i != blocks.end(); ++i)
-               {
-                       MapBlock *block = (*i);
-                       v3s16 p = block->getPos();
+               for(MapBlockVect::iterator i = blocks.begin();
+                               i != blocks.end(); ++i) {
+                       v3s16 p = (*i)->getPos();
                        dst.push_back(p);
                }
        }
index e7d7798b87a14bb6c9676272eb7253bb05cda774..76ba94c3437baa9653f5b77428f0db0009ff2749 100644 (file)
@@ -614,6 +614,8 @@ private:
        int m_refcount;
 };
 
+typedef std::vector<MapBlock*> MapBlockVect;
+
 inline bool blockpos_over_limit(v3s16 p)
 {
        return
index 0d40a659d4003e558b493bdec2606b4736a14072..7bc4bd3a36216886ff436d497e96becd5f679e29 100644 (file)
@@ -133,7 +133,7 @@ void MapSector::deleteBlock(MapBlock *block)
        delete block;
 }
 
-void MapSector::getBlocks(std::list<MapBlock*> &dest)
+void MapSector::getBlocks(MapBlockVect &dest)
 {
        for(std::map<s16, MapBlock*>::iterator bi = m_blocks.begin();
                bi != m_blocks.end(); ++bi)
index dac4ee8d634992d55d933b63e7d587e4b2863bcd..e89247a92e0d1784d4817307385cc35910de5e5f 100644 (file)
@@ -22,11 +22,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "irrlichttypes.h"
 #include "irr_v2d.h"
+#include "mapblock.h"
 #include <ostream>
 #include <map>
-#include <list>
+#include <vector>
 
-class MapBlock;
 class Map;
 class IGameDef;
 
@@ -61,7 +61,7 @@ public:
        
        void deleteBlock(MapBlock *block);
        
-       void getBlocks(std::list<MapBlock*> &dest);
+       void getBlocks(MapBlockVect &dest);
        
        // Always false at the moment, because sector contains no metadata.
        bool differs_from_disk;