ClientInterface::getClientIDs doesn't need a std::list. Use a std::vector for better perfs

This commit is contained in:
Loic Blot 2015-03-04 16:30:24 +01:00
parent 7e088fdfe3
commit 2066655aae
4 changed files with 57 additions and 71 deletions

View File

@ -560,9 +560,9 @@ ClientInterface::~ClientInterface()
} }
} }
std::list<u16> ClientInterface::getClientIDs(ClientState min_state) std::vector<u16> ClientInterface::getClientIDs(ClientState min_state)
{ {
std::list<u16> reply; std::vector<u16> reply;
JMutexAutoLock clientslock(m_clients_mutex); JMutexAutoLock clientslock(m_clients_mutex);
for(std::map<u16, RemoteClient*>::iterator for(std::map<u16, RemoteClient*>::iterator
@ -596,20 +596,22 @@ void ClientInterface::UpdatePlayerList()
{ {
if (m_env != NULL) if (m_env != NULL)
{ {
std::list<u16> clients = getClientIDs(); std::vector<u16> clients = getClientIDs();
m_clients_names.clear(); m_clients_names.clear();
if(!clients.empty()) if(!clients.empty())
infostream<<"Players:"<<std::endl; infostream<<"Players:"<<std::endl;
for(std::list<u16>::iterator
for(std::vector<u16>::iterator
i = clients.begin(); i = clients.begin();
i != clients.end(); ++i) i != clients.end(); ++i) {
{
Player *player = m_env->getPlayer(*i); Player *player = m_env->getPlayer(*i);
if(player==NULL)
if (player == NULL)
continue; continue;
infostream<<"* "<<player->getName()<<"\t";
infostream << "* " << player->getName() << "\t";
{ {
JMutexAutoLock clientslock(m_clients_mutex); JMutexAutoLock clientslock(m_clients_mutex);
@ -617,6 +619,7 @@ void ClientInterface::UpdatePlayerList()
if(client != NULL) if(client != NULL)
client->PrintInfo(infostream); client->PrintInfo(infostream);
} }
m_clients_names.push_back(player->getName()); m_clients_names.push_back(player->getName());
} }
} }

View File

@ -388,7 +388,7 @@ public:
void step(float dtime); void step(float dtime);
/* get list of active client id's */ /* get list of active client id's */
std::list<u16> getClientIDs(ClientState min_state=CS_Active); std::vector<u16> getClientIDs(ClientState min_state=CS_Active);
/* get list of client player names */ /* get list of client player names */
std::vector<std::string> getPlayerNames(); std::vector<std::string> getPlayerNames();

View File

@ -851,10 +851,9 @@ void Server::handleCommand_ChatMessage(NetworkPacket* pkt)
else { else {
actionstream << "CHAT: " << wide_to_narrow(line)<<std::endl; actionstream << "CHAT: " << wide_to_narrow(line)<<std::endl;
std::list<u16> clients = m_clients.getClientIDs(); std::vector<u16> clients = m_clients.getClientIDs();
for (std::list<u16>::iterator for (std::vector<u16>::iterator i = clients.begin();
i = clients.begin();
i != clients.end(); ++i) { i != clients.end(); ++i) {
if (*i != pkt->getPeerId()) if (*i != pkt->getPeerId())
SendChatMessage(*i, line); SendChatMessage(*i, line);

View File

@ -1374,16 +1374,14 @@ void Server::setInventoryModified(const InventoryLocation &loc)
void Server::SetBlocksNotSent(std::map<v3s16, MapBlock *>& block) void Server::SetBlocksNotSent(std::map<v3s16, MapBlock *>& block)
{ {
std::list<u16> clients = m_clients.getClientIDs(); std::vector<u16> clients = m_clients.getClientIDs();
m_clients.Lock(); m_clients.Lock();
// Set the modified blocks unsent for all the clients // Set the modified blocks unsent for all the clients
for (std::list<u16>::iterator for (std::vector<u16>::iterator i = clients.begin();
i = clients.begin();
i != clients.end(); ++i) { i != clients.end(); ++i) {
RemoteClient *client = m_clients.lockedGetClientNoEx(*i); if (RemoteClient *client = m_clients.lockedGetClientNoEx(*i))
if (client != NULL)
client->SetBlocksNotSent(block); client->SetBlocksNotSent(block);
} }
m_clients.Unlock(); m_clients.Unlock();
} }
@ -1954,15 +1952,15 @@ s32 Server::playSound(const SimpleSoundSpec &spec,
} }
else else
{ {
std::list<u16> clients = m_clients.getClientIDs(); std::vector<u16> clients = m_clients.getClientIDs();
for(std::list<u16>::iterator for(std::vector<u16>::iterator
i = clients.begin(); i != clients.end(); ++i) i = clients.begin(); i != clients.end(); ++i) {
{
Player *player = m_env->getPlayer(*i); Player *player = m_env->getPlayer(*i);
if(!player) if(!player)
continue; continue;
if(pos_exists){
if(pos_exists) {
if(player->getPosition().getDistanceFrom(pos) > if(player->getPosition().getDistanceFrom(pos) >
params.max_hear_distance) params.max_hear_distance)
continue; continue;
@ -1970,6 +1968,7 @@ s32 Server::playSound(const SimpleSoundSpec &spec,
dst_clients.push_back(*i); dst_clients.push_back(*i);
} }
} }
if(dst_clients.empty()) if(dst_clients.empty())
return -1; return -1;
@ -2025,9 +2024,8 @@ void Server::sendRemoveNode(v3s16 p, u16 ignore_id,
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_REMOVENODE, 6); NetworkPacket* pkt = new NetworkPacket(TOCLIENT_REMOVENODE, 6);
*pkt << p; *pkt << p;
std::list<u16> clients = m_clients.getClientIDs(); std::vector<u16> clients = m_clients.getClientIDs();
for(std::list<u16>::iterator for(std::vector<u16>::iterator i = clients.begin();
i = clients.begin();
i != clients.end(); ++i) { i != clients.end(); ++i) {
if(far_players) { if(far_players) {
// Get player // Get player
@ -2055,22 +2053,16 @@ void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id,
float maxd = far_d_nodes*BS; float maxd = far_d_nodes*BS;
v3f p_f = intToFloat(p, BS); v3f p_f = intToFloat(p, BS);
std::list<u16> clients = m_clients.getClientIDs(); std::vector<u16> clients = m_clients.getClientIDs();
for(std::list<u16>::iterator for(std::vector<u16>::iterator i = clients.begin();
i = clients.begin(); i != clients.end(); ++i) {
i != clients.end(); ++i)
{
if(far_players) if(far_players) {
{
// Get player // Get player
Player *player = m_env->getPlayer(*i); if(Player *player = m_env->getPlayer(*i)) {
if(player)
{
// If player is far away, only set modified blocks not sent // If player is far away, only set modified blocks not sent
v3f player_pos = player->getPosition(); v3f player_pos = player->getPosition();
if(player_pos.getDistanceFrom(p_f) > maxd) if(player_pos.getDistanceFrom(p_f) > maxd) {
{
far_players->push_back(*i); far_players->push_back(*i);
continue; continue;
} }
@ -2102,12 +2094,10 @@ void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id,
void Server::setBlockNotSent(v3s16 p) void Server::setBlockNotSent(v3s16 p)
{ {
std::list<u16> clients = m_clients.getClientIDs(); std::vector<u16> clients = m_clients.getClientIDs();
m_clients.Lock(); m_clients.Lock();
for(std::list<u16>::iterator for(std::vector<u16>::iterator i = clients.begin();
i = clients.begin(); i != clients.end(); ++i) {
i != clients.end(); ++i)
{
RemoteClient *client = m_clients.lockedGetClientNoEx(*i); RemoteClient *client = m_clients.lockedGetClientNoEx(*i);
client->SetBlockNotSent(p); client->SetBlockNotSent(p);
} }
@ -2153,13 +2143,11 @@ void Server::SendBlocks(float dtime)
{ {
ScopeProfiler sp(g_profiler, "Server: selecting blocks for sending"); ScopeProfiler sp(g_profiler, "Server: selecting blocks for sending");
std::list<u16> clients = m_clients.getClientIDs(); std::vector<u16> clients = m_clients.getClientIDs();
m_clients.Lock(); m_clients.Lock();
for(std::list<u16>::iterator for(std::vector<u16>::iterator i = clients.begin();
i = clients.begin(); i != clients.end(); ++i) {
i != clients.end(); ++i)
{
RemoteClient *client = m_clients.lockedGetClientNoEx(*i, CS_Active); RemoteClient *client = m_clients.lockedGetClientNoEx(*i, CS_Active);
if (client == NULL) if (client == NULL)
@ -2630,26 +2618,24 @@ void Server::DeleteClient(u16 peer_id, ClientDeletionReason reason)
Print out action Print out action
*/ */
{ {
if(player != NULL && reason != CDR_DENY) if(player != NULL && reason != CDR_DENY) {
{
std::ostringstream os(std::ios_base::binary); std::ostringstream os(std::ios_base::binary);
std::list<u16> clients = m_clients.getClientIDs(); std::vector<u16> clients = m_clients.getClientIDs();
for(std::list<u16>::iterator for(std::vector<u16>::iterator i = clients.begin();
i = clients.begin(); i != clients.end(); ++i) {
i != clients.end(); ++i)
{
// Get player // Get player
Player *player = m_env->getPlayer(*i); Player *player = m_env->getPlayer(*i);
if(!player) if(!player)
continue; continue;
// Get name of player // Get name of player
os<<player->getName()<<" "; os << player->getName() << " ";
} }
actionstream<<player->getName()<<" " actionstream << player->getName() << " "
<<(reason==CDR_TIMEOUT?"times out.":"leaves game.") << (reason == CDR_TIMEOUT ? "times out." : "leaves game.")
<<" List of players: "<<os.str()<<std::endl; << " List of players: " << os.str() << std::endl;
} }
} }
{ {
@ -2723,10 +2709,9 @@ std::wstring Server::getStatusString()
// Information about clients // Information about clients
bool first = true; bool first = true;
os<<L", clients={"; os<<L", clients={";
std::list<u16> clients = m_clients.getClientIDs(); std::vector<u16> clients = m_clients.getClientIDs();
for(std::list<u16>::iterator i = clients.begin(); for(std::vector<u16>::iterator i = clients.begin();
i != clients.end(); ++i) i != clients.end(); ++i) {
{
// Get player // Get player
Player *player = m_env->getPlayer(*i); Player *player = m_env->getPlayer(*i);
// Get name of player // Get name of player
@ -2735,12 +2720,12 @@ std::wstring Server::getStatusString()
name = narrow_to_wide(player->getName()); name = narrow_to_wide(player->getName());
// Add name to information string // Add name to information string
if(!first) if(!first)
os<<L", "; os << L", ";
else else
first = false; first = false;
os<<name; os << name;
} }
os<<L"}"; os << L"}";
if(((ServerMap*)(&m_env->getMap()))->isSavingEnabled() == false) if(((ServerMap*)(&m_env->getMap()))->isSavingEnabled() == false)
os<<std::endl<<L"# Server: "<<" WARNING: Map saving is disabled."; os<<std::endl<<L"# Server: "<<" WARNING: Map saving is disabled.";
if(g_settings->get("motd") != "") if(g_settings->get("motd") != "")
@ -2763,11 +2748,10 @@ bool Server::checkPriv(const std::string &name, const std::string &priv)
void Server::reportPrivsModified(const std::string &name) void Server::reportPrivsModified(const std::string &name)
{ {
if(name == ""){ if(name == "") {
std::list<u16> clients = m_clients.getClientIDs(); std::vector<u16> clients = m_clients.getClientIDs();
for(std::list<u16>::iterator for(std::vector<u16>::iterator i = clients.begin();
i = clients.begin(); i != clients.end(); ++i) {
i != clients.end(); ++i){
Player *player = m_env->getPlayer(*i); Player *player = m_env->getPlayer(*i);
reportPrivsModified(player->getName()); reportPrivsModified(player->getName());
} }