Little optimization on getAdded/Removed activeobjects per player loop.

Use std::queue instead of std::set, we don't need such a heavy container.
Don't convert position to int to convert it back to float in the next function.
This commit is contained in:
Loic Blot 2015-08-05 22:29:47 +02:00 committed by est31
parent fe994946b7
commit 9c635f28ac
3 changed files with 36 additions and 46 deletions

@ -1318,12 +1318,11 @@ u16 ServerEnvironment::addActiveObject(ServerActiveObject *object)
Finds out what new objects have been added to Finds out what new objects have been added to
inside a radius around a position inside a radius around a position
*/ */
void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius, void ServerEnvironment::getAddedActiveObjects(Player *player, s16 radius,
s16 player_radius, s16 player_radius,
std::set<u16> &current_objects, std::set<u16> &current_objects,
std::set<u16> &added_objects) std::queue<u16> &added_objects)
{ {
v3f pos_f = intToFloat(pos, BS);
f32 radius_f = radius * BS; f32 radius_f = radius * BS;
f32 player_radius_f = player_radius * BS; f32 player_radius_f = player_radius * BS;
@ -1339,18 +1338,19 @@ void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius,
*/ */
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;
// Get object // Get object
ServerActiveObject *object = i->second; ServerActiveObject *object = i->second;
if(object == NULL) if(object == NULL)
continue; continue;
// Discard if removed or deactivating // Discard if removed or deactivating
if(object->m_removed || object->m_pending_deactivation) if(object->m_removed || object->m_pending_deactivation)
continue; continue;
f32 distance_f = object->getBasePosition().getDistanceFrom(pos_f); f32 distance_f = object->getBasePosition().getDistanceFrom(player->getPosition());
if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) { if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
// Discard if too far // Discard if too far
if (distance_f > player_radius_f && player_radius_f != 0) if (distance_f > player_radius_f && player_radius_f != 0)
@ -1364,7 +1364,7 @@ void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius,
if(n != current_objects.end()) if(n != current_objects.end())
continue; continue;
// Add to added_objects // Add to added_objects
added_objects.insert(id); added_objects.push(id);
} }
} }
@ -1372,12 +1372,11 @@ void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius,
Finds out what objects have been removed from Finds out what objects have been removed from
inside a radius around a position inside a radius around a position
*/ */
void ServerEnvironment::getRemovedActiveObjects(v3s16 pos, s16 radius, void ServerEnvironment::getRemovedActiveObjects(Player *player, s16 radius,
s16 player_radius, s16 player_radius,
std::set<u16> &current_objects, std::set<u16> &current_objects,
std::set<u16> &removed_objects) std::queue<u16> &removed_objects)
{ {
v3f pos_f = intToFloat(pos, BS);
f32 radius_f = radius * BS; f32 radius_f = radius * BS;
f32 player_radius_f = player_radius * BS; f32 player_radius_f = player_radius * BS;
@ -1399,20 +1398,19 @@ void ServerEnvironment::getRemovedActiveObjects(v3s16 pos, s16 radius,
u16 id = *i; u16 id = *i;
ServerActiveObject *object = getActiveObject(id); ServerActiveObject *object = getActiveObject(id);
if(object == NULL){ if (object == NULL) {
infostream<<"ServerEnvironment::getRemovedActiveObjects():" infostream << "ServerEnvironment::getRemovedActiveObjects():"
<<" object in current_objects is NULL"<<std::endl; << " object in current_objects is NULL" << std::endl;
removed_objects.insert(id); removed_objects.push(id);
continue; continue;
} }
if(object->m_removed || object->m_pending_deactivation) if (object->m_removed || object->m_pending_deactivation) {
{ removed_objects.push(id);
removed_objects.insert(id);
continue; continue;
} }
f32 distance_f = object->getBasePosition().getDistanceFrom(pos_f); f32 distance_f = object->getBasePosition().getDistanceFrom(player->getPosition());
if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) { if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
if (distance_f <= player_radius_f || player_radius_f == 0) if (distance_f <= player_radius_f || player_radius_f == 0)
continue; continue;
@ -1420,7 +1418,7 @@ void ServerEnvironment::getRemovedActiveObjects(v3s16 pos, s16 radius,
continue; continue;
// Object is no longer visible // Object is no longer visible
removed_objects.insert(id); removed_objects.push(id);
} }
} }

@ -265,19 +265,19 @@ public:
Find out what new objects have been added to Find out what new objects have been added to
inside a radius around a position inside a radius around a position
*/ */
void getAddedActiveObjects(v3s16 pos, s16 radius, void getAddedActiveObjects(Player *player, s16 radius,
s16 player_radius, s16 player_radius,
std::set<u16> &current_objects, std::set<u16> &current_objects,
std::set<u16> &added_objects); std::queue<u16> &added_objects);
/* /*
Find out what new objects have been removed from Find out what new objects have been removed from
inside a radius around a position inside a radius around a position
*/ */
void getRemovedActiveObjects(v3s16 pos, s16 radius, void getRemovedActiveObjects(Player* player, s16 radius,
s16 player_radius, s16 player_radius,
std::set<u16> &current_objects, std::set<u16> &current_objects,
std::set<u16> &removed_objects); std::queue<u16> &removed_objects);
/* /*
Get the next message emitted by some active object. Get the next message emitted by some active object.

@ -680,10 +680,9 @@ void Server::AsyncRunStep(bool initial_step)
radius *= MAP_BLOCKSIZE; radius *= MAP_BLOCKSIZE;
player_radius *= MAP_BLOCKSIZE; player_radius *= MAP_BLOCKSIZE;
for(std::map<u16, RemoteClient*>::iterator for (std::map<u16, RemoteClient*>::iterator
i = clients.begin(); i = clients.begin();
i != clients.end(); ++i) i != clients.end(); ++i) {
{
RemoteClient *client = i->second; RemoteClient *client = i->second;
// If definitions and textures have not been sent, don't // If definitions and textures have not been sent, don't
@ -692,27 +691,23 @@ void Server::AsyncRunStep(bool initial_step)
continue; continue;
Player *player = m_env->getPlayer(client->peer_id); Player *player = m_env->getPlayer(client->peer_id);
if(player==NULL) if(player == NULL) {
{
// This can happen if the client timeouts somehow // This can happen if the client timeouts somehow
/*infostream<<"WARNING: "<<__FUNCTION_NAME<<": Client " /*infostream<<"WARNING: "<<__FUNCTION_NAME<<": Client "
<<client->peer_id <<client->peer_id
<<" has no associated player"<<std::endl;*/ <<" has no associated player"<<std::endl;*/
continue; continue;
} }
v3s16 pos = floatToInt(player->getPosition(), BS);
std::set<u16> removed_objects; std::queue<u16> removed_objects;
std::set<u16> added_objects; std::queue<u16> added_objects;
m_env->getRemovedActiveObjects(pos, radius, player_radius, m_env->getRemovedActiveObjects(player, radius, player_radius,
client->m_known_objects, removed_objects); client->m_known_objects, removed_objects);
m_env->getAddedActiveObjects(pos, radius, player_radius, m_env->getAddedActiveObjects(player, radius, player_radius,
client->m_known_objects, added_objects); client->m_known_objects, added_objects);
// Ignore if nothing happened // Ignore if nothing happened
if(removed_objects.empty() && added_objects.empty()) if (removed_objects.empty() && added_objects.empty()) {
{
//infostream<<"active objects: none changed"<<std::endl;
continue; continue;
} }
@ -723,12 +718,9 @@ void Server::AsyncRunStep(bool initial_step)
// Handle removed objects // Handle removed objects
writeU16((u8*)buf, removed_objects.size()); writeU16((u8*)buf, removed_objects.size());
data_buffer.append(buf, 2); data_buffer.append(buf, 2);
for(std::set<u16>::iterator while (!removed_objects.empty()) {
i = removed_objects.begin();
i != removed_objects.end(); ++i)
{
// Get object // Get object
u16 id = *i; u16 id = removed_objects.front();
ServerActiveObject* obj = m_env->getActiveObject(id); ServerActiveObject* obj = m_env->getActiveObject(id);
// Add to data buffer for sending // Add to data buffer for sending
@ -740,17 +732,15 @@ void Server::AsyncRunStep(bool initial_step)
if(obj && obj->m_known_by_count > 0) if(obj && obj->m_known_by_count > 0)
obj->m_known_by_count--; obj->m_known_by_count--;
removed_objects.pop();
} }
// Handle added objects // Handle added objects
writeU16((u8*)buf, added_objects.size()); writeU16((u8*)buf, added_objects.size());
data_buffer.append(buf, 2); data_buffer.append(buf, 2);
for(std::set<u16>::iterator while (!added_objects.empty()) {
i = added_objects.begin();
i != added_objects.end(); ++i)
{
// Get object // Get object
u16 id = *i; u16 id = added_objects.front();
ServerActiveObject* obj = m_env->getActiveObject(id); ServerActiveObject* obj = m_env->getActiveObject(id);
// Get object type // Get object type
@ -778,6 +768,8 @@ void Server::AsyncRunStep(bool initial_step)
if(obj) if(obj)
obj->m_known_by_count++; obj->m_known_by_count++;
added_objects.pop();
} }
u32 pktSize = SendActiveObjectRemoveAdd(client->peer_id, data_buffer); u32 pktSize = SendActiveObjectRemoveAdd(client->peer_id, data_buffer);