Skip to content

Commit fd70f4f

Browse files
committedFeb 17, 2015
Replace std::list by std::vector into ClientMap::updateDrawList, Map::timerUpdate and ServerMap::save().
This will speedup the loop reading into those functions
1 parent 3c91ad8 commit fd70f4f

File tree

5 files changed

+32
-42
lines changed

5 files changed

+32
-42
lines changed
 

Diff for: ‎src/clientmap.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ void ClientMap::updateDrawList(video::IVideoDriver* driver)
247247
continue;
248248
}
249249

250-
std::list< MapBlock * > sectorblocks;
250+
MapBlockVect sectorblocks;
251251
sector->getBlocks(sectorblocks);
252252

253253
/*
@@ -256,8 +256,8 @@ void ClientMap::updateDrawList(video::IVideoDriver* driver)
256256

257257
u32 sector_blocks_drawn = 0;
258258

259-
std::list< MapBlock * >::iterator i;
260-
for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++)
259+
for(MapBlockVect::iterator i = sectorblocks.begin();
260+
i != sectorblocks.end(); i++)
261261
{
262262
MapBlock *block = *i;
263263

Diff for: ‎src/map.cpp

+23-35
Original file line numberDiff line numberDiff line change
@@ -1440,23 +1440,20 @@ void Map::timerUpdate(float dtime, float unload_timeout,
14401440

14411441
bool all_blocks_deleted = true;
14421442

1443-
std::list<MapBlock*> blocks;
1443+
MapBlockVect blocks;
14441444
sector->getBlocks(blocks);
14451445

1446-
for(std::list<MapBlock*>::iterator i = blocks.begin();
1447-
i != blocks.end(); ++i)
1448-
{
1446+
for(MapBlockVect::iterator i = blocks.begin();
1447+
i != blocks.end(); ++i) {
14491448
MapBlock *block = (*i);
14501449

14511450
block->incrementUsageTimer(dtime);
14521451

1453-
if(block->refGet() == 0 && block->getUsageTimer() > unload_timeout)
1454-
{
1452+
if(block->refGet() == 0 && block->getUsageTimer() > unload_timeout) {
14551453
v3s16 p = block->getPos();
14561454

14571455
// Save if modified
1458-
if (block->getModified() != MOD_STATE_CLEAN && save_before_unloading)
1459-
{
1456+
if (block->getModified() != MOD_STATE_CLEAN && save_before_unloading) {
14601457
modprofiler.add(block->getModifiedReason(), 1);
14611458
if (!saveBlock(block))
14621459
continue;
@@ -1471,15 +1468,13 @@ void Map::timerUpdate(float dtime, float unload_timeout,
14711468

14721469
deleted_blocks_count++;
14731470
}
1474-
else
1475-
{
1471+
else {
14761472
all_blocks_deleted = false;
14771473
block_count_all++;
14781474
}
14791475
}
14801476

1481-
if(all_blocks_deleted)
1482-
{
1477+
if(all_blocks_deleted) {
14831478
sector_deletion_queue.push_back(si->first);
14841479
}
14851480
}
@@ -2982,8 +2977,7 @@ std::string ServerMap::getBlockFilename(v3s16 p)
29822977
void ServerMap::save(ModifiedState save_level)
29832978
{
29842979
DSTACK(__FUNCTION_NAME);
2985-
if(m_map_saving_enabled == false)
2986-
{
2980+
if(m_map_saving_enabled == false) {
29872981
infostream<<"WARNING: Not saving map, saving disabled."<<std::endl;
29882982
return;
29892983
}
@@ -2992,8 +2986,7 @@ void ServerMap::save(ModifiedState save_level)
29922986
infostream<<"ServerMap: Saving whole map, this can take time."
29932987
<<std::endl;
29942988

2995-
if(m_map_metadata_changed || save_level == MOD_STATE_CLEAN)
2996-
{
2989+
if(m_map_metadata_changed || save_level == MOD_STATE_CLEAN) {
29972990
saveMapMeta();
29982991
}
29992992

@@ -3008,30 +3001,27 @@ void ServerMap::save(ModifiedState save_level)
30083001
bool save_started = false;
30093002

30103003
for(std::map<v2s16, MapSector*>::iterator i = m_sectors.begin();
3011-
i != m_sectors.end(); ++i)
3012-
{
3004+
i != m_sectors.end(); ++i) {
30133005
ServerMapSector *sector = (ServerMapSector*)i->second;
30143006
assert(sector->getId() == MAPSECTOR_SERVER);
30153007

3016-
if(sector->differs_from_disk || save_level == MOD_STATE_CLEAN)
3017-
{
3008+
if(sector->differs_from_disk || save_level == MOD_STATE_CLEAN) {
30183009
saveSectorMeta(sector);
30193010
sector_meta_count++;
30203011
}
3021-
std::list<MapBlock*> blocks;
3012+
3013+
MapBlockVect blocks;
30223014
sector->getBlocks(blocks);
30233015

3024-
for(std::list<MapBlock*>::iterator j = blocks.begin();
3025-
j != blocks.end(); ++j)
3026-
{
3016+
for(MapBlockVect::iterator j = blocks.begin();
3017+
j != blocks.end(); ++j) {
30273018
MapBlock *block = *j;
30283019

30293020
block_count_all++;
30303021

3031-
if(block->getModified() >= (u32)save_level)
3032-
{
3022+
if(block->getModified() >= (u32)save_level) {
30333023
// Lazy beginSave()
3034-
if(!save_started){
3024+
if(!save_started) {
30353025
beginSave();
30363026
save_started = true;
30373027
}
@@ -3049,15 +3039,15 @@ void ServerMap::save(ModifiedState save_level)
30493039
}
30503040
}
30513041
}
3042+
30523043
if(save_started)
30533044
endSave();
30543045

30553046
/*
30563047
Only print if something happened or saved whole map
30573048
*/
30583049
if(save_level == MOD_STATE_CLEAN || sector_meta_count != 0
3059-
|| block_count != 0)
3060-
{
3050+
|| block_count != 0) {
30613051
infostream<<"ServerMap: Written: "
30623052
<<sector_meta_count<<" sector metadata files, "
30633053
<<block_count<<" block files"
@@ -3085,14 +3075,12 @@ void ServerMap::listAllLoadedBlocks(std::vector<v3s16> &dst)
30853075
{
30863076
MapSector *sector = si->second;
30873077

3088-
std::list<MapBlock*> blocks;
3078+
MapBlockVect blocks;
30893079
sector->getBlocks(blocks);
30903080

3091-
for(std::list<MapBlock*>::iterator i = blocks.begin();
3092-
i != blocks.end(); ++i)
3093-
{
3094-
MapBlock *block = (*i);
3095-
v3s16 p = block->getPos();
3081+
for(MapBlockVect::iterator i = blocks.begin();
3082+
i != blocks.end(); ++i) {
3083+
v3s16 p = (*i)->getPos();
30963084
dst.push_back(p);
30973085
}
30983086
}

Diff for: ‎src/mapblock.h

+2
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,8 @@ class MapBlock /*: public NodeContainer*/
614614
int m_refcount;
615615
};
616616

617+
typedef std::vector<MapBlock*> MapBlockVect;
618+
617619
inline bool blockpos_over_limit(v3s16 p)
618620
{
619621
return

Diff for: ‎src/mapsector.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void MapSector::deleteBlock(MapBlock *block)
133133
delete block;
134134
}
135135

136-
void MapSector::getBlocks(std::list<MapBlock*> &dest)
136+
void MapSector::getBlocks(MapBlockVect &dest)
137137
{
138138
for(std::map<s16, MapBlock*>::iterator bi = m_blocks.begin();
139139
bi != m_blocks.end(); ++bi)

Diff for: ‎src/mapsector.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2222

2323
#include "irrlichttypes.h"
2424
#include "irr_v2d.h"
25+
#include "mapblock.h"
2526
#include <ostream>
2627
#include <map>
27-
#include <list>
28+
#include <vector>
2829

29-
class MapBlock;
3030
class Map;
3131
class IGameDef;
3232

@@ -61,7 +61,7 @@ class MapSector
6161

6262
void deleteBlock(MapBlock *block);
6363

64-
void getBlocks(std::list<MapBlock*> &dest);
64+
void getBlocks(MapBlockVect &dest);
6565

6666
// Always false at the moment, because sector contains no metadata.
6767
bool differs_from_disk;

0 commit comments

Comments
 (0)
Please sign in to comment.