From 2066655aae2022384fc12a10c04dccfd2996f0ac Mon Sep 17 00:00:00 2001
From: Loic Blot <loic.blot@unix-experience.fr>
Date: Wed, 4 Mar 2015 16:30:24 +0100
Subject: [PATCH] ClientInterface::getClientIDs doesn't need a std::list. Use a
 std::vector for better perfs

---
 src/clientiface.cpp                   |  19 +++--
 src/clientiface.h                     |   2 +-
 src/network/packethandlers/server.cpp |   5 +-
 src/server.cpp                        | 102 +++++++++++---------------
 4 files changed, 57 insertions(+), 71 deletions(-)

diff --git a/src/clientiface.cpp b/src/clientiface.cpp
index 6180cf5da..126979897 100644
--- a/src/clientiface.cpp
+++ b/src/clientiface.cpp
@@ -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);
 
 	for(std::map<u16, RemoteClient*>::iterator
@@ -596,20 +596,22 @@ void ClientInterface::UpdatePlayerList()
 {
 	if (m_env != NULL)
 		{
-		std::list<u16> clients = getClientIDs();
+		std::vector<u16> clients = getClientIDs();
 		m_clients_names.clear();
 
 
 		if(!clients.empty())
 			infostream<<"Players:"<<std::endl;
-		for(std::list<u16>::iterator
+
+		for(std::vector<u16>::iterator
 			i = clients.begin();
-			i != clients.end(); ++i)
-		{
+			i != clients.end(); ++i) {
 			Player *player = m_env->getPlayer(*i);
-			if(player==NULL)
+
+			if (player == NULL)
 				continue;
-			infostream<<"* "<<player->getName()<<"\t";
+
+			infostream << "* " << player->getName() << "\t";
 
 			{
 				JMutexAutoLock clientslock(m_clients_mutex);
@@ -617,6 +619,7 @@ void ClientInterface::UpdatePlayerList()
 				if(client != NULL)
 					client->PrintInfo(infostream);
 			}
+
 			m_clients_names.push_back(player->getName());
 		}
 	}
diff --git a/src/clientiface.h b/src/clientiface.h
index 129d3f861..2f265b128 100644
--- a/src/clientiface.h
+++ b/src/clientiface.h
@@ -388,7 +388,7 @@ public:
 	void step(float dtime);
 
 	/* 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 */
 	std::vector<std::string> getPlayerNames();
diff --git a/src/network/packethandlers/server.cpp b/src/network/packethandlers/server.cpp
index aeaa2f3f3..62ce7eb55 100644
--- a/src/network/packethandlers/server.cpp
+++ b/src/network/packethandlers/server.cpp
@@ -851,10 +851,9 @@ void Server::handleCommand_ChatMessage(NetworkPacket* pkt)
 		else {
 			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
-				i = clients.begin();
+			for (std::vector<u16>::iterator i = clients.begin();
 				i != clients.end(); ++i) {
 				if (*i != pkt->getPeerId())
 					SendChatMessage(*i, line);
diff --git a/src/server.cpp b/src/server.cpp
index 51b90ce45..2587c2d89 100644
--- a/src/server.cpp
+++ b/src/server.cpp
@@ -1374,16 +1374,14 @@ void Server::setInventoryModified(const InventoryLocation &loc)
 
 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();
 	// Set the modified blocks unsent for all the clients
-	for (std::list<u16>::iterator
-		 i = clients.begin();
+	for (std::vector<u16>::iterator i = clients.begin();
 		 i != clients.end(); ++i) {
-			RemoteClient *client = m_clients.lockedGetClientNoEx(*i);
-			if (client != NULL)
+			if (RemoteClient *client = m_clients.lockedGetClientNoEx(*i))
 				client->SetBlocksNotSent(block);
-		}
+	}
 	m_clients.Unlock();
 }
 
@@ -1954,15 +1952,15 @@ s32 Server::playSound(const SimpleSoundSpec &spec,
 	}
 	else
 	{
-		std::list<u16> clients = m_clients.getClientIDs();
+		std::vector<u16> clients = m_clients.getClientIDs();
 
-		for(std::list<u16>::iterator
-				i = clients.begin(); i != clients.end(); ++i)
-		{
+		for(std::vector<u16>::iterator
+				i = clients.begin(); i != clients.end(); ++i) {
 			Player *player = m_env->getPlayer(*i);
 			if(!player)
 				continue;
-			if(pos_exists){
+
+			if(pos_exists) {
 				if(player->getPosition().getDistanceFrom(pos) >
 						params.max_hear_distance)
 					continue;
@@ -1970,6 +1968,7 @@ s32 Server::playSound(const SimpleSoundSpec &spec,
 			dst_clients.push_back(*i);
 		}
 	}
+
 	if(dst_clients.empty())
 		return -1;
 
@@ -2025,9 +2024,8 @@ void Server::sendRemoveNode(v3s16 p, u16 ignore_id,
 	NetworkPacket* pkt = new NetworkPacket(TOCLIENT_REMOVENODE, 6);
 	*pkt << p;
 
-	std::list<u16> clients = m_clients.getClientIDs();
-	for(std::list<u16>::iterator
-		i = clients.begin();
+	std::vector<u16> clients = m_clients.getClientIDs();
+	for(std::vector<u16>::iterator i = clients.begin();
 		i != clients.end(); ++i) {
 		if(far_players) {
 			// Get player
@@ -2055,22 +2053,16 @@ void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id,
 	float maxd = far_d_nodes*BS;
 	v3f p_f = intToFloat(p, BS);
 
-	std::list<u16> clients = m_clients.getClientIDs();
-	for(std::list<u16>::iterator
-			i = clients.begin();
-			i != clients.end(); ++i)
-		{
+	std::vector<u16> clients = m_clients.getClientIDs();
+	for(std::vector<u16>::iterator i = clients.begin();
+			i != clients.end(); ++i) {
 
-		if(far_players)
-		{
+		if(far_players) {
 			// Get player
-			Player *player = m_env->getPlayer(*i);
-			if(player)
-			{
+			if(Player *player = m_env->getPlayer(*i)) {
 				// If player is far away, only set modified blocks not sent
 				v3f player_pos = player->getPosition();
-				if(player_pos.getDistanceFrom(p_f) > maxd)
-				{
+				if(player_pos.getDistanceFrom(p_f) > maxd) {
 					far_players->push_back(*i);
 					continue;
 				}
@@ -2102,12 +2094,10 @@ void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id,
 
 void Server::setBlockNotSent(v3s16 p)
 {
-	std::list<u16> clients = m_clients.getClientIDs();
+	std::vector<u16> clients = m_clients.getClientIDs();
 	m_clients.Lock();
-	for(std::list<u16>::iterator
-		i = clients.begin();
-		i != clients.end(); ++i)
-	{
+	for(std::vector<u16>::iterator i = clients.begin();
+		i != clients.end(); ++i) {
 		RemoteClient *client = m_clients.lockedGetClientNoEx(*i);
 		client->SetBlockNotSent(p);
 	}
@@ -2153,13 +2143,11 @@ void Server::SendBlocks(float dtime)
 	{
 		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();
-		for(std::list<u16>::iterator
-			i = clients.begin();
-			i != clients.end(); ++i)
-		{
+		for(std::vector<u16>::iterator i = clients.begin();
+			i != clients.end(); ++i) {
 			RemoteClient *client = m_clients.lockedGetClientNoEx(*i, CS_Active);
 
 			if (client == NULL)
@@ -2630,26 +2618,24 @@ void Server::DeleteClient(u16 peer_id, ClientDeletionReason reason)
 			Print out action
 		*/
 		{
-			if(player != NULL && reason != CDR_DENY)
-			{
+			if(player != NULL && reason != CDR_DENY) {
 				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
-					i = clients.begin();
-					i != clients.end(); ++i)
-				{
+				for(std::vector<u16>::iterator i = clients.begin();
+					i != clients.end(); ++i) {
 					// Get player
 					Player *player = m_env->getPlayer(*i);
 					if(!player)
 						continue;
+
 					// Get name of player
-					os<<player->getName()<<" ";
+					os << player->getName() << " ";
 				}
 
-				actionstream<<player->getName()<<" "
-						<<(reason==CDR_TIMEOUT?"times out.":"leaves game.")
-						<<" List of players: "<<os.str()<<std::endl;
+				actionstream << player->getName() << " "
+						<< (reason == CDR_TIMEOUT ? "times out." : "leaves game.")
+						<< " List of players: " << os.str() << std::endl;
 			}
 		}
 		{
@@ -2723,10 +2709,9 @@ std::wstring Server::getStatusString()
 	// Information about clients
 	bool first = true;
 	os<<L", clients={";
-	std::list<u16> clients = m_clients.getClientIDs();
-	for(std::list<u16>::iterator i = clients.begin();
-		i != clients.end(); ++i)
-	{
+	std::vector<u16> clients = m_clients.getClientIDs();
+	for(std::vector<u16>::iterator i = clients.begin();
+		i != clients.end(); ++i) {
 		// Get player
 		Player *player = m_env->getPlayer(*i);
 		// Get name of player
@@ -2735,12 +2720,12 @@ std::wstring Server::getStatusString()
 			name = narrow_to_wide(player->getName());
 		// Add name to information string
 		if(!first)
-			os<<L", ";
+			os << L", ";
 		else
 			first = false;
-		os<<name;
+		os << name;
 	}
-	os<<L"}";
+	os << L"}";
 	if(((ServerMap*)(&m_env->getMap()))->isSavingEnabled() == false)
 		os<<std::endl<<L"# Server: "<<" WARNING: Map saving is disabled.";
 	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)
 {
-	if(name == ""){
-		std::list<u16> clients = m_clients.getClientIDs();
-		for(std::list<u16>::iterator
-				i = clients.begin();
-				i != clients.end(); ++i){
+	if(name == "") {
+		std::vector<u16> clients = m_clients.getClientIDs();
+		for(std::vector<u16>::iterator i = clients.begin();
+				i != clients.end(); ++i) {
 			Player *player = m_env->getPlayer(*i);
 			reportPrivsModified(player->getName());
 		}