Replace std::list by std::vector into ServerMap::listAllLoadableBlocks ServerMap::listAllLoadedBlocks and their database backends.

This adds a speedup on database migration and /clearobjects command
This commit is contained in:
Loic Blot 2015-02-17 14:30:32 +01:00
parent c58d49977d
commit 718bcafd51
13 changed files with 43 additions and 51 deletions

@ -65,7 +65,7 @@ bool Database_Dummy::deleteBlock(v3s16 blockpos)
return true; return true;
} }
void Database_Dummy::listAllLoadableBlocks(std::list<v3s16> &dst) void Database_Dummy::listAllLoadableBlocks(std::vector<v3s16> &dst)
{ {
for(std::map<u64, std::string>::iterator x = m_database.begin(); x != m_database.end(); ++x) for(std::map<u64, std::string>::iterator x = m_database.begin(); x != m_database.end(); ++x)
{ {

@ -36,7 +36,7 @@ public:
virtual bool saveBlock(v3s16 blockpos, std::string &data); virtual bool saveBlock(v3s16 blockpos, std::string &data);
virtual std::string loadBlock(v3s16 blockpos); virtual std::string loadBlock(v3s16 blockpos);
virtual bool deleteBlock(v3s16 blockpos); virtual bool deleteBlock(v3s16 blockpos);
virtual void listAllLoadableBlocks(std::list<v3s16> &dst); virtual void listAllLoadableBlocks(std::vector<v3s16> &dst);
virtual int Initialized(void); virtual int Initialized(void);
~Database_Dummy(); ~Database_Dummy();
private: private:

@ -93,7 +93,7 @@ bool Database_LevelDB::deleteBlock(v3s16 blockpos)
return true; return true;
} }
void Database_LevelDB::listAllLoadableBlocks(std::list<v3s16> &dst) void Database_LevelDB::listAllLoadableBlocks(std::vector<v3s16> &dst)
{ {
leveldb::Iterator* it = m_database->NewIterator(leveldb::ReadOptions()); leveldb::Iterator* it = m_database->NewIterator(leveldb::ReadOptions());
for (it->SeekToFirst(); it->Valid(); it->Next()) { for (it->SeekToFirst(); it->Valid(); it->Next()) {

@ -39,7 +39,7 @@ public:
virtual bool saveBlock(v3s16 blockpos, std::string &data); virtual bool saveBlock(v3s16 blockpos, std::string &data);
virtual std::string loadBlock(v3s16 blockpos); virtual std::string loadBlock(v3s16 blockpos);
virtual bool deleteBlock(v3s16 blockpos); virtual bool deleteBlock(v3s16 blockpos);
virtual void listAllLoadableBlocks(std::list<v3s16> &dst); virtual void listAllLoadableBlocks(std::vector<v3s16> &dst);
virtual int Initialized(void); virtual int Initialized(void);
~Database_LevelDB(); ~Database_LevelDB();
private: private:

@ -147,7 +147,7 @@ bool Database_Redis::deleteBlock(v3s16 blockpos)
return true; return true;
} }
void Database_Redis::listAllLoadableBlocks(std::list<v3s16> &dst) void Database_Redis::listAllLoadableBlocks(std::vector<v3s16> &dst)
{ {
redisReply *reply; redisReply *reply;
reply = (redisReply*) redisCommand(ctx, "HKEYS %s", hash.c_str()); reply = (redisReply*) redisCommand(ctx, "HKEYS %s", hash.c_str());
@ -155,8 +155,7 @@ void Database_Redis::listAllLoadableBlocks(std::list<v3s16> &dst)
throw FileNotGoodException(std::string("redis command 'HKEYS %s' failed: ") + ctx->errstr); throw FileNotGoodException(std::string("redis command 'HKEYS %s' failed: ") + ctx->errstr);
if(reply->type != REDIS_REPLY_ARRAY) if(reply->type != REDIS_REPLY_ARRAY)
throw FileNotGoodException("Failed to get keys from database"); throw FileNotGoodException("Failed to get keys from database");
for(size_t i = 0; i < reply->elements; i++) for(size_t i = 0; i < reply->elements; i++) {
{
assert(reply->element[i]->type == REDIS_REPLY_STRING); assert(reply->element[i]->type == REDIS_REPLY_STRING);
dst.push_back(getIntegerAsBlock(stoi64(reply->element[i]->str))); dst.push_back(getIntegerAsBlock(stoi64(reply->element[i]->str)));
} }

@ -39,7 +39,7 @@ public:
virtual bool saveBlock(v3s16 blockpos, std::string &data); virtual bool saveBlock(v3s16 blockpos, std::string &data);
virtual std::string loadBlock(v3s16 blockpos); virtual std::string loadBlock(v3s16 blockpos);
virtual bool deleteBlock(v3s16 blockpos); virtual bool deleteBlock(v3s16 blockpos);
virtual void listAllLoadableBlocks(std::list<v3s16> &dst); virtual void listAllLoadableBlocks(std::vector<v3s16> &dst);
virtual int Initialized(void); virtual int Initialized(void);
~Database_Redis(); ~Database_Redis();
private: private:

@ -274,12 +274,11 @@ void Database_SQLite3::createDatabase()
} }
void Database_SQLite3::listAllLoadableBlocks(std::list<v3s16> &dst) void Database_SQLite3::listAllLoadableBlocks(std::vector<v3s16> &dst)
{ {
verifyDatabase(); verifyDatabase();
while(sqlite3_step(m_database_list) == SQLITE_ROW) while(sqlite3_step(m_database_list) == SQLITE_ROW) {
{
sqlite3_int64 block_i = sqlite3_column_int64(m_database_list, 0); sqlite3_int64 block_i = sqlite3_column_int64(m_database_list, 0);
v3s16 p = getIntegerAsBlock(block_i); v3s16 p = getIntegerAsBlock(block_i);
//dstream<<"block_i="<<block_i<<" p="<<PP(p)<<std::endl; //dstream<<"block_i="<<block_i<<" p="<<PP(p)<<std::endl;

@ -39,7 +39,7 @@ public:
virtual bool saveBlock(v3s16 blockpos, std::string &data); virtual bool saveBlock(v3s16 blockpos, std::string &data);
virtual std::string loadBlock(v3s16 blockpos); virtual std::string loadBlock(v3s16 blockpos);
virtual bool deleteBlock(v3s16 blockpos); virtual bool deleteBlock(v3s16 blockpos);
virtual void listAllLoadableBlocks(std::list<v3s16> &dst); virtual void listAllLoadableBlocks(std::vector<v3s16> &dst);
virtual int Initialized(void); virtual int Initialized(void);
~Database_SQLite3(); ~Database_SQLite3();
private: private:

@ -20,7 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifndef DATABASE_HEADER #ifndef DATABASE_HEADER
#define DATABASE_HEADER #define DATABASE_HEADER
#include <list> #include <vector>
#include <string> #include <string>
#include "irr_v3d.h" #include "irr_v3d.h"
#include "irrlichttypes.h" #include "irrlichttypes.h"
@ -40,7 +40,7 @@ public:
virtual bool deleteBlock(v3s16 blockpos) = 0; virtual bool deleteBlock(v3s16 blockpos) = 0;
s64 getBlockAsInteger(const v3s16 pos) const; s64 getBlockAsInteger(const v3s16 pos) const;
v3s16 getIntegerAsBlock(s64 i) const; v3s16 getIntegerAsBlock(s64 i) const;
virtual void listAllLoadableBlocks(std::list<v3s16> &dst) = 0; virtual void listAllLoadableBlocks(std::vector<v3s16> &dst) = 0;
virtual int Initialized(void)=0; virtual int Initialized(void)=0;
virtual ~Database() {}; virtual ~Database() {};
}; };

@ -851,11 +851,10 @@ void ServerEnvironment::clearAllObjects()
{ {
infostream<<"ServerEnvironment::clearAllObjects(): " infostream<<"ServerEnvironment::clearAllObjects(): "
<<"Removing all active objects"<<std::endl; <<"Removing all active objects"<<std::endl;
std::list<u16> objects_to_remove; std::vector<u16> objects_to_remove;
for(std::map<u16, ServerActiveObject*>::iterator for(std::map<u16, ServerActiveObject*>::iterator
i = m_active_objects.begin(); i = m_active_objects.begin();
i != m_active_objects.end(); ++i) i != m_active_objects.end(); ++i) {
{
ServerActiveObject* obj = i->second; ServerActiveObject* obj = i->second;
if(obj->getType() == ACTIVEOBJECT_TYPE_PLAYER) if(obj->getType() == ACTIVEOBJECT_TYPE_PLAYER)
continue; continue;
@ -888,15 +887,15 @@ void ServerEnvironment::clearAllObjects()
// Id to be removed from m_active_objects // Id to be removed from m_active_objects
objects_to_remove.push_back(id); objects_to_remove.push_back(id);
} }
// Remove references from m_active_objects // Remove references from m_active_objects
for(std::list<u16>::iterator i = objects_to_remove.begin(); for(std::vector<u16>::iterator i = objects_to_remove.begin();
i != objects_to_remove.end(); ++i) i != objects_to_remove.end(); ++i) {
{
m_active_objects.erase(*i); m_active_objects.erase(*i);
} }
// Get list of loaded blocks // Get list of loaded blocks
std::list<v3s16> loaded_blocks; std::vector<v3s16> loaded_blocks;
infostream<<"ServerEnvironment::clearAllObjects(): " infostream<<"ServerEnvironment::clearAllObjects(): "
<<"Listing all loaded blocks"<<std::endl; <<"Listing all loaded blocks"<<std::endl;
m_map->listAllLoadedBlocks(loaded_blocks); m_map->listAllLoadedBlocks(loaded_blocks);
@ -905,7 +904,7 @@ void ServerEnvironment::clearAllObjects()
<<loaded_blocks.size()<<std::endl; <<loaded_blocks.size()<<std::endl;
// Get list of loadable blocks // Get list of loadable blocks
std::list<v3s16> loadable_blocks; std::vector<v3s16> loadable_blocks;
infostream<<"ServerEnvironment::clearAllObjects(): " infostream<<"ServerEnvironment::clearAllObjects(): "
<<"Listing all loadable blocks"<<std::endl; <<"Listing all loadable blocks"<<std::endl;
m_map->listAllLoadableBlocks(loadable_blocks); m_map->listAllLoadableBlocks(loadable_blocks);
@ -915,9 +914,8 @@ void ServerEnvironment::clearAllObjects()
<<", now clearing"<<std::endl; <<", now clearing"<<std::endl;
// Grab a reference on each loaded block to avoid unloading it // Grab a reference on each loaded block to avoid unloading it
for(std::list<v3s16>::iterator i = loaded_blocks.begin(); for(std::vector<v3s16>::iterator i = loaded_blocks.begin();
i != loaded_blocks.end(); ++i) i != loaded_blocks.end(); ++i) {
{
v3s16 p = *i; v3s16 p = *i;
MapBlock *block = m_map->getBlockNoCreateNoEx(p); MapBlock *block = m_map->getBlockNoCreateNoEx(p);
assert(block); assert(block);
@ -931,9 +929,8 @@ void ServerEnvironment::clearAllObjects()
u32 num_blocks_checked = 0; u32 num_blocks_checked = 0;
u32 num_blocks_cleared = 0; u32 num_blocks_cleared = 0;
u32 num_objs_cleared = 0; u32 num_objs_cleared = 0;
for(std::list<v3s16>::iterator i = loadable_blocks.begin(); for(std::vector<v3s16>::iterator i = loadable_blocks.begin();
i != loadable_blocks.end(); ++i) i != loadable_blocks.end(); ++i) {
{
v3s16 p = *i; v3s16 p = *i;
MapBlock *block = m_map->emergeBlock(p, false); MapBlock *block = m_map->emergeBlock(p, false);
if(!block){ if(!block){
@ -969,9 +966,8 @@ void ServerEnvironment::clearAllObjects()
m_map->unloadUnreferencedBlocks(); m_map->unloadUnreferencedBlocks();
// Drop references that were added above // Drop references that were added above
for(std::list<v3s16>::iterator i = loaded_blocks.begin(); for(std::vector<v3s16>::iterator i = loaded_blocks.begin();
i != loaded_blocks.end(); ++i) i != loaded_blocks.end(); ++i) {
{
v3s16 p = *i; v3s16 p = *i;
MapBlock *block = m_map->getBlockNoCreateNoEx(p); MapBlock *block = m_map->getBlockNoCreateNoEx(p);
assert(block); assert(block);
@ -1542,11 +1538,10 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
*/ */
void ServerEnvironment::removeRemovedObjects() void ServerEnvironment::removeRemovedObjects()
{ {
std::list<u16> objects_to_remove; std::vector<u16> objects_to_remove;
for(std::map<u16, ServerActiveObject*>::iterator for(std::map<u16, ServerActiveObject*>::iterator
i = m_active_objects.begin(); i = m_active_objects.begin();
i != m_active_objects.end(); ++i) i != m_active_objects.end(); ++i) {
{
u16 id = i->first; u16 id = i->first;
ServerActiveObject* obj = i->second; ServerActiveObject* obj = i->second;
// This shouldn't happen but check it // This shouldn't happen but check it
@ -1616,13 +1611,13 @@ void ServerEnvironment::removeRemovedObjects()
// Delete // Delete
if(obj->environmentDeletes()) if(obj->environmentDeletes())
delete obj; delete obj;
// Id to be removed from m_active_objects // Id to be removed from m_active_objects
objects_to_remove.push_back(id); objects_to_remove.push_back(id);
} }
// Remove references from m_active_objects // Remove references from m_active_objects
for(std::list<u16>::iterator i = objects_to_remove.begin(); for(std::vector<u16>::iterator i = objects_to_remove.begin();
i != objects_to_remove.end(); ++i) i != objects_to_remove.end(); ++i) {
{
m_active_objects.erase(*i); m_active_objects.erase(*i);
} }
} }
@ -1666,8 +1661,9 @@ static void print_hexdump(std::ostream &o, const std::string &data)
*/ */
void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s) void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
{ {
if(block==NULL) if(block == NULL)
return; return;
// Ignore if no stored objects (to not set changed flag) // Ignore if no stored objects (to not set changed flag)
if(block->m_static_objects.m_stored.empty()) if(block->m_static_objects.m_stored.empty())
return; return;
@ -1693,17 +1689,14 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
std::list<StaticObject> new_stored; std::list<StaticObject> new_stored;
for(std::list<StaticObject>::iterator for(std::list<StaticObject>::iterator
i = block->m_static_objects.m_stored.begin(); i = block->m_static_objects.m_stored.begin();
i != block->m_static_objects.m_stored.end(); ++i) i != block->m_static_objects.m_stored.end(); ++i) {
{
/*infostream<<"Server: Creating an active object from "
<<"static data"<<std::endl;*/
StaticObject &s_obj = *i; StaticObject &s_obj = *i;
// Create an active object from the data // Create an active object from the data
ServerActiveObject *obj = ServerActiveObject::create ServerActiveObject *obj = ServerActiveObject::create
((ActiveObjectType) s_obj.type, this, 0, s_obj.pos, s_obj.data); ((ActiveObjectType) s_obj.type, this, 0, s_obj.pos, s_obj.data);
// If couldn't create object, store static data back. // If couldn't create object, store static data back.
if(obj==NULL) if(obj == NULL) {
{
errorstream<<"ServerEnvironment::activateObjects(): " errorstream<<"ServerEnvironment::activateObjects(): "
<<"failed to create active object from static object " <<"failed to create active object from static object "
<<"in block "<<PP(s_obj.pos/BS) <<"in block "<<PP(s_obj.pos/BS)

@ -973,16 +973,17 @@ static bool migrate_database(const GameParams &game_params, const Settings &cmd_
return false; return false;
} }
std::list<v3s16> blocks; std::vector<v3s16> blocks;
ServerMap &old_map = ((ServerMap&)server->getMap()); ServerMap &old_map = ((ServerMap&)server->getMap());
old_map.listAllLoadableBlocks(blocks); old_map.listAllLoadableBlocks(blocks);
int count = 0; int count = 0;
new_db->beginSave(); new_db->beginSave();
for (std::list<v3s16>::iterator i = blocks.begin(); i != blocks.end(); i++) { for (std::vector<v3s16>::iterator i = blocks.begin(); i != blocks.end(); i++) {
MapBlock *block = old_map.loadBlock(*i); MapBlock *block = old_map.loadBlock(*i);
if (!block) { if (!block) {
errorstream << "Failed to load block " << PP(*i) << ", skipping it."; errorstream << "Failed to load block " << PP(*i) << ", skipping it.";
} else { }
else {
old_map.saveBlock(block, new_db); old_map.saveBlock(block, new_db);
MapSector *sector = old_map.getSectorNoGenerate(v2s16(i->X, i->Z)); MapSector *sector = old_map.getSectorNoGenerate(v2s16(i->X, i->Z));
sector->deleteBlock(block); sector->deleteBlock(block);

@ -3070,7 +3070,7 @@ void ServerMap::save(ModifiedState save_level)
} }
} }
void ServerMap::listAllLoadableBlocks(std::list<v3s16> &dst) void ServerMap::listAllLoadableBlocks(std::vector<v3s16> &dst)
{ {
if(loadFromFolders()){ if(loadFromFolders()){
errorstream<<"Map::listAllLoadableBlocks(): Result will be missing " errorstream<<"Map::listAllLoadableBlocks(): Result will be missing "
@ -3079,7 +3079,7 @@ void ServerMap::listAllLoadableBlocks(std::list<v3s16> &dst)
dbase->listAllLoadableBlocks(dst); dbase->listAllLoadableBlocks(dst);
} }
void ServerMap::listAllLoadedBlocks(std::list<v3s16> &dst) void ServerMap::listAllLoadedBlocks(std::vector<v3s16> &dst)
{ {
for(std::map<v2s16, MapSector*>::iterator si = m_sectors.begin(); for(std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
si != m_sectors.end(); ++si) si != m_sectors.end(); ++si)

@ -466,8 +466,8 @@ public:
void endSave(); void endSave();
void save(ModifiedState save_level); void save(ModifiedState save_level);
void listAllLoadableBlocks(std::list<v3s16> &dst); void listAllLoadableBlocks(std::vector<v3s16> &dst);
void listAllLoadedBlocks(std::list<v3s16> &dst); void listAllLoadedBlocks(std::vector<v3s16> &dst);
// Saves map seed and possibly other stuff // Saves map seed and possibly other stuff
void saveMapMeta(); void saveMapMeta();
void loadMapMeta(); void loadMapMeta();