Change many useless std::list into Environment class (m_players), ABMHandler and ActiveBlockList::update

This improve looping performances.
This commit is contained in:
Loic Blot 2015-02-17 16:49:27 +01:00
parent 81b9c023c4
commit 2079462e62
2 changed files with 54 additions and 70 deletions

@ -61,9 +61,8 @@ Environment::Environment():
Environment::~Environment() Environment::~Environment()
{ {
// Deallocate players // Deallocate players
for(std::list<Player*>::iterator i = m_players.begin(); for(std::vector<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i) i != m_players.end(); ++i) {
{
delete (*i); delete (*i);
} }
} }
@ -89,7 +88,7 @@ void Environment::removePlayer(u16 peer_id)
{ {
DSTACK(__FUNCTION_NAME); DSTACK(__FUNCTION_NAME);
for(std::list<Player*>::iterator i = m_players.begin(); for(std::vector<Player*>::iterator i = m_players.begin();
i != m_players.end();) i != m_players.end();)
{ {
Player *player = *i; Player *player = *i;
@ -104,7 +103,7 @@ void Environment::removePlayer(u16 peer_id)
void Environment::removePlayer(const char *name) void Environment::removePlayer(const char *name)
{ {
for (std::list<Player*>::iterator it = m_players.begin(); for (std::vector<Player*>::iterator it = m_players.begin();
it != m_players.end(); ++it) { it != m_players.end(); ++it) {
if (strcmp((*it)->getName(), name) == 0) { if (strcmp((*it)->getName(), name) == 0) {
delete *it; delete *it;
@ -116,9 +115,8 @@ void Environment::removePlayer(const char *name)
Player * Environment::getPlayer(u16 peer_id) Player * Environment::getPlayer(u16 peer_id)
{ {
for(std::list<Player*>::iterator i = m_players.begin(); for(std::vector<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i) i != m_players.end(); ++i) {
{
Player *player = *i; Player *player = *i;
if(player->peer_id == peer_id) if(player->peer_id == peer_id)
return player; return player;
@ -128,9 +126,8 @@ Player * Environment::getPlayer(u16 peer_id)
Player * Environment::getPlayer(const char *name) Player * Environment::getPlayer(const char *name)
{ {
for(std::list<Player*>::iterator i = m_players.begin(); for(std::vector<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i) i != m_players.end(); ++i) {
{
Player *player = *i; Player *player = *i;
if(strcmp(player->getName(), name) == 0) if(strcmp(player->getName(), name) == 0)
return player; return player;
@ -140,15 +137,13 @@ Player * Environment::getPlayer(const char *name)
Player * Environment::getRandomConnectedPlayer() Player * Environment::getRandomConnectedPlayer()
{ {
std::list<Player*> connected_players = getPlayers(true); std::vector<Player*> connected_players = getPlayers(true);
u32 chosen_one = myrand() % connected_players.size(); u32 chosen_one = myrand() % connected_players.size();
u32 j = 0; u32 j = 0;
for(std::list<Player*>::iterator for(std::vector<Player*>::iterator
i = connected_players.begin(); i = connected_players.begin();
i != connected_players.end(); ++i) i != connected_players.end(); ++i) {
{ if(j == chosen_one) {
if(j == chosen_one)
{
Player *player = *i; Player *player = *i;
return player; return player;
} }
@ -159,17 +154,15 @@ Player * Environment::getRandomConnectedPlayer()
Player * Environment::getNearestConnectedPlayer(v3f pos) Player * Environment::getNearestConnectedPlayer(v3f pos)
{ {
std::list<Player*> connected_players = getPlayers(true); std::vector<Player*> connected_players = getPlayers(true);
f32 nearest_d = 0; f32 nearest_d = 0;
Player *nearest_player = NULL; Player *nearest_player = NULL;
for(std::list<Player*>::iterator for(std::vector<Player*>::iterator
i = connected_players.begin(); i = connected_players.begin();
i != connected_players.end(); ++i) i != connected_players.end(); ++i) {
{
Player *player = *i; Player *player = *i;
f32 d = player->getPosition().getDistanceFrom(pos); f32 d = player->getPosition().getDistanceFrom(pos);
if(d < nearest_d || nearest_player == NULL) if(d < nearest_d || nearest_player == NULL) {
{
nearest_d = d; nearest_d = d;
nearest_player = player; nearest_player = player;
} }
@ -177,22 +170,20 @@ Player * Environment::getNearestConnectedPlayer(v3f pos)
return nearest_player; return nearest_player;
} }
std::list<Player*> Environment::getPlayers() std::vector<Player*> Environment::getPlayers()
{ {
return m_players; return m_players;
} }
std::list<Player*> Environment::getPlayers(bool ignore_disconnected) std::vector<Player*> Environment::getPlayers(bool ignore_disconnected)
{ {
std::list<Player*> newlist; std::vector<Player*> newlist;
for(std::list<Player*>::iterator for(std::vector<Player*>::iterator
i = m_players.begin(); i = m_players.begin();
i != m_players.end(); ++i) i != m_players.end(); ++i) {
{
Player *player = *i; Player *player = *i;
if(ignore_disconnected) if(ignore_disconnected) {
{
// Ignore disconnected players // Ignore disconnected players
if(player->peer_id == 0) if(player->peer_id == 0)
continue; continue;
@ -287,7 +278,7 @@ void fillRadiusBlock(v3s16 p0, s16 r, std::set<v3s16> &list)
} }
} }
void ActiveBlockList::update(std::list<v3s16> &active_positions, void ActiveBlockList::update(std::vector<v3s16> &active_positions,
s16 radius, s16 radius,
std::set<v3s16> &blocks_removed, std::set<v3s16> &blocks_removed,
std::set<v3s16> &blocks_added) std::set<v3s16> &blocks_added)
@ -296,7 +287,7 @@ void ActiveBlockList::update(std::list<v3s16> &active_positions,
Create the new list Create the new list
*/ */
std::set<v3s16> newlist = m_forceloaded_list; std::set<v3s16> newlist = m_forceloaded_list;
for(std::list<v3s16>::iterator i = active_positions.begin(); for(std::vector<v3s16>::iterator i = active_positions.begin();
i != active_positions.end(); ++i) i != active_positions.end(); ++i)
{ {
fillRadiusBlock(*i, radius, newlist); fillRadiusBlock(*i, radius, newlist);
@ -395,8 +386,8 @@ bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16
//calculate normalized direction vector //calculate normalized direction vector
v3f normalized_vector = v3f((pos2.X - pos1.X)/distance, v3f normalized_vector = v3f((pos2.X - pos1.X)/distance,
(pos2.Y - pos1.Y)/distance, (pos2.Y - pos1.Y)/distance,
(pos2.Z - pos1.Z)/distance); (pos2.Z - pos1.Z)/distance);
//find out if there's a node on path between pos1 and pos2 //find out if there's a node on path between pos1 and pos2
for (float i = 1; i < distance; i += stepsize) { for (float i = 1; i < distance; i += stepsize) {
@ -421,7 +412,7 @@ void ServerEnvironment::saveLoadedPlayers()
std::string players_path = m_path_world + DIR_DELIM "players"; std::string players_path = m_path_world + DIR_DELIM "players";
fs::CreateDir(players_path); fs::CreateDir(players_path);
for (std::list<Player*>::iterator it = m_players.begin(); for (std::vector<Player*>::iterator it = m_players.begin();
it != m_players.end(); it != m_players.end();
++it) { ++it) {
RemotePlayer *player = static_cast<RemotePlayer*>(*it); RemotePlayer *player = static_cast<RemotePlayer*>(*it);
@ -549,7 +540,7 @@ class ABMHandler
{ {
private: private:
ServerEnvironment *m_env; ServerEnvironment *m_env;
std::map<content_t, std::list<ActiveABM> > m_aabms; std::map<content_t, std::vector<ActiveABM> > m_aabms;
public: public:
ABMHandler(std::list<ABMWithState> &abms, ABMHandler(std::list<ABMWithState> &abms,
float dtime_s, ServerEnvironment *env, float dtime_s, ServerEnvironment *env,
@ -604,10 +595,10 @@ public:
k != ids.end(); k++) k != ids.end(); k++)
{ {
content_t c = *k; content_t c = *k;
std::map<content_t, std::list<ActiveABM> >::iterator j; std::map<content_t, std::vector<ActiveABM> >::iterator j;
j = m_aabms.find(c); j = m_aabms.find(c);
if(j == m_aabms.end()){ if(j == m_aabms.end()){
std::list<ActiveABM> aabmlist; std::vector<ActiveABM> aabmlist;
m_aabms[c] = aabmlist; m_aabms[c] = aabmlist;
j = m_aabms.find(c); j = m_aabms.find(c);
} }
@ -664,14 +655,13 @@ public:
content_t c = n.getContent(); content_t c = n.getContent();
v3s16 p = p0 + block->getPosRelative(); v3s16 p = p0 + block->getPosRelative();
std::map<content_t, std::list<ActiveABM> >::iterator j; std::map<content_t, std::vector<ActiveABM> >::iterator j;
j = m_aabms.find(c); j = m_aabms.find(c);
if(j == m_aabms.end()) if(j == m_aabms.end())
continue; continue;
for(std::list<ActiveABM>::iterator for(std::vector<ActiveABM>::iterator
i = j->second.begin(); i != j->second.end(); i++) i = j->second.begin(); i != j->second.end(); i++) {
{
if(myrand() % i->chance != 0) if(myrand() % i->chance != 0)
continue; continue;
@ -1008,7 +998,7 @@ void ServerEnvironment::step(float dtime)
*/ */
{ {
ScopeProfiler sp(g_profiler, "SEnv: handle players avg", SPT_AVG); ScopeProfiler sp(g_profiler, "SEnv: handle players avg", SPT_AVG);
for(std::list<Player*>::iterator i = m_players.begin(); for(std::vector<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i) i != m_players.end(); ++i)
{ {
Player *player = *i; Player *player = *i;
@ -1031,15 +1021,15 @@ void ServerEnvironment::step(float dtime)
/* /*
Get player block positions Get player block positions
*/ */
std::list<v3s16> players_blockpos; std::vector<v3s16> players_blockpos;
for(std::list<Player*>::iterator for(std::vector<Player*>::iterator
i = m_players.begin(); i = m_players.begin();
i != m_players.end(); ++i) i != m_players.end(); ++i) {
{
Player *player = *i; Player *player = *i;
// Ignore disconnected players // Ignore disconnected players
if(player->peer_id == 0) if(player->peer_id == 0)
continue; continue;
v3s16 blockpos = getNodeBlockPos( v3s16 blockpos = getNodeBlockPos(
floatToInt(player->getPosition(), BS)); floatToInt(player->getPosition(), BS));
players_blockpos.push_back(blockpos); players_blockpos.push_back(blockpos);
@ -1686,7 +1676,7 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
} }
// Activate stored objects // Activate stored objects
std::list<StaticObject> new_stored; std::vector<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) {
@ -1715,10 +1705,9 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
// Clear stored list // Clear stored list
block->m_static_objects.m_stored.clear(); block->m_static_objects.m_stored.clear();
// Add leftover failed stuff to stored list // Add leftover failed stuff to stored list
for(std::list<StaticObject>::iterator for(std::vector<StaticObject>::iterator
i = new_stored.begin(); i = new_stored.begin();
i != new_stored.end(); ++i) i != new_stored.end(); ++i) {
{
StaticObject &s_obj = *i; StaticObject &s_obj = *i;
block->m_static_objects.m_stored.push_back(s_obj); block->m_static_objects.m_stored.push_back(s_obj);
} }
@ -1758,11 +1747,10 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
*/ */
void ServerEnvironment::deactivateFarObjects(bool force_delete) void ServerEnvironment::deactivateFarObjects(bool force_delete)
{ {
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;
assert(obj); assert(obj);
@ -1979,9 +1967,8 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
} }
// 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);
} }
} }
@ -2052,9 +2039,8 @@ void ClientEnvironment::addPlayer(Player *player)
LocalPlayer * ClientEnvironment::getLocalPlayer() LocalPlayer * ClientEnvironment::getLocalPlayer()
{ {
for(std::list<Player*>::iterator i = m_players.begin(); for(std::vector<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i) i != m_players.end(); ++i) {
{
Player *player = *i; Player *player = *i;
if(player->isLocal()) if(player->isLocal())
return (LocalPlayer*)player; return (LocalPlayer*)player;
@ -2310,16 +2296,14 @@ void ClientEnvironment::step(float dtime)
/* /*
Stuff that can be done in an arbitarily large dtime Stuff that can be done in an arbitarily large dtime
*/ */
for(std::list<Player*>::iterator i = m_players.begin(); for(std::vector<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i) i != m_players.end(); ++i) {
{
Player *player = *i; Player *player = *i;
/* /*
Handle non-local players Handle non-local players
*/ */
if(player->isLocal() == false) if(player->isLocal() == false) {
{
// Move // Move
player->move(dtime, this, 100*BS); player->move(dtime, this, 100*BS);

@ -75,8 +75,8 @@ public:
Player * getPlayer(const char *name); Player * getPlayer(const char *name);
Player * getRandomConnectedPlayer(); Player * getRandomConnectedPlayer();
Player * getNearestConnectedPlayer(v3f pos); Player * getNearestConnectedPlayer(v3f pos);
std::list<Player*> getPlayers(); std::vector<Player*> getPlayers();
std::list<Player*> getPlayers(bool ignore_disconnected); std::vector<Player*> getPlayers(bool ignore_disconnected);
u32 getDayNightRatio(); u32 getDayNightRatio();
@ -110,7 +110,7 @@ public:
protected: protected:
// peer_ids in here should be unique, except that there may be many 0s // peer_ids in here should be unique, except that there may be many 0s
std::list<Player*> m_players; std::vector<Player*> m_players;
// Time of day in milli-hours (0-23999); determines day and night // Time of day in milli-hours (0-23999); determines day and night
u32 m_time_of_day; u32 m_time_of_day;
// Time of day in 0...1 // Time of day in 0...1
@ -182,7 +182,7 @@ struct ABMWithState
class ActiveBlockList class ActiveBlockList
{ {
public: public:
void update(std::list<v3s16> &active_positions, void update(std::vector<v3s16> &active_positions,
s16 radius, s16 radius,
std::set<v3s16> &blocks_removed, std::set<v3s16> &blocks_removed,
std::set<v3s16> &blocks_added); std::set<v3s16> &blocks_added);