Skip to content

Commit 9c635f2

Browse files
nerzhulest31
authored andcommittedSep 19, 2015
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.
1 parent fe99494 commit 9c635f2

File tree

3 files changed

+36
-46
lines changed

3 files changed

+36
-46
lines changed
 

Diff for: ‎src/environment.cpp

+17-19
Original file line numberDiff line numberDiff line change
@@ -1318,12 +1318,11 @@ u16 ServerEnvironment::addActiveObject(ServerActiveObject *object)
13181318
Finds out what new objects have been added to
13191319
inside a radius around a position
13201320
*/
1321-
void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius,
1321+
void ServerEnvironment::getAddedActiveObjects(Player *player, s16 radius,
13221322
s16 player_radius,
13231323
std::set<u16> &current_objects,
1324-
std::set<u16> &added_objects)
1324+
std::queue<u16> &added_objects)
13251325
{
1326-
v3f pos_f = intToFloat(pos, BS);
13271326
f32 radius_f = radius * BS;
13281327
f32 player_radius_f = player_radius * BS;
13291328

@@ -1339,18 +1338,19 @@ void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius,
13391338
*/
13401339
for(std::map<u16, ServerActiveObject*>::iterator
13411340
i = m_active_objects.begin();
1342-
i != m_active_objects.end(); ++i)
1343-
{
1341+
i != m_active_objects.end(); ++i) {
13441342
u16 id = i->first;
1343+
13451344
// Get object
13461345
ServerActiveObject *object = i->second;
13471346
if(object == NULL)
13481347
continue;
1348+
13491349
// Discard if removed or deactivating
13501350
if(object->m_removed || object->m_pending_deactivation)
13511351
continue;
13521352

1353-
f32 distance_f = object->getBasePosition().getDistanceFrom(pos_f);
1353+
f32 distance_f = object->getBasePosition().getDistanceFrom(player->getPosition());
13541354
if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
13551355
// Discard if too far
13561356
if (distance_f > player_radius_f && player_radius_f != 0)
@@ -1364,20 +1364,19 @@ void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius,
13641364
if(n != current_objects.end())
13651365
continue;
13661366
// Add to added_objects
1367-
added_objects.insert(id);
1367+
added_objects.push(id);
13681368
}
13691369
}
13701370

13711371
/*
13721372
Finds out what objects have been removed from
13731373
inside a radius around a position
13741374
*/
1375-
void ServerEnvironment::getRemovedActiveObjects(v3s16 pos, s16 radius,
1375+
void ServerEnvironment::getRemovedActiveObjects(Player *player, s16 radius,
13761376
s16 player_radius,
13771377
std::set<u16> &current_objects,
1378-
std::set<u16> &removed_objects)
1378+
std::queue<u16> &removed_objects)
13791379
{
1380-
v3f pos_f = intToFloat(pos, BS);
13811380
f32 radius_f = radius * BS;
13821381
f32 player_radius_f = player_radius * BS;
13831382

@@ -1399,28 +1398,27 @@ void ServerEnvironment::getRemovedActiveObjects(v3s16 pos, s16 radius,
13991398
u16 id = *i;
14001399
ServerActiveObject *object = getActiveObject(id);
14011400

1402-
if(object == NULL){
1403-
infostream<<"ServerEnvironment::getRemovedActiveObjects():"
1404-
<<" object in current_objects is NULL"<<std::endl;
1405-
removed_objects.insert(id);
1401+
if (object == NULL) {
1402+
infostream << "ServerEnvironment::getRemovedActiveObjects():"
1403+
<< " object in current_objects is NULL" << std::endl;
1404+
removed_objects.push(id);
14061405
continue;
14071406
}
14081407

1409-
if(object->m_removed || object->m_pending_deactivation)
1410-
{
1411-
removed_objects.insert(id);
1408+
if (object->m_removed || object->m_pending_deactivation) {
1409+
removed_objects.push(id);
14121410
continue;
14131411
}
14141412

1415-
f32 distance_f = object->getBasePosition().getDistanceFrom(pos_f);
1413+
f32 distance_f = object->getBasePosition().getDistanceFrom(player->getPosition());
14161414
if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
14171415
if (distance_f <= player_radius_f || player_radius_f == 0)
14181416
continue;
14191417
} else if (distance_f <= radius_f)
14201418
continue;
14211419

14221420
// Object is no longer visible
1423-
removed_objects.insert(id);
1421+
removed_objects.push(id);
14241422
}
14251423
}
14261424

Diff for: ‎src/environment.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -265,19 +265,19 @@ class ServerEnvironment : public Environment
265265
Find out what new objects have been added to
266266
inside a radius around a position
267267
*/
268-
void getAddedActiveObjects(v3s16 pos, s16 radius,
268+
void getAddedActiveObjects(Player *player, s16 radius,
269269
s16 player_radius,
270270
std::set<u16> &current_objects,
271-
std::set<u16> &added_objects);
271+
std::queue<u16> &added_objects);
272272

273273
/*
274274
Find out what new objects have been removed from
275275
inside a radius around a position
276276
*/
277-
void getRemovedActiveObjects(v3s16 pos, s16 radius,
277+
void getRemovedActiveObjects(Player* player, s16 radius,
278278
s16 player_radius,
279279
std::set<u16> &current_objects,
280-
std::set<u16> &removed_objects);
280+
std::queue<u16> &removed_objects);
281281

282282
/*
283283
Get the next message emitted by some active object.

Diff for: ‎src/server.cpp

+15-23
Original file line numberDiff line numberDiff line change
@@ -680,10 +680,9 @@ void Server::AsyncRunStep(bool initial_step)
680680
radius *= MAP_BLOCKSIZE;
681681
player_radius *= MAP_BLOCKSIZE;
682682

683-
for(std::map<u16, RemoteClient*>::iterator
683+
for (std::map<u16, RemoteClient*>::iterator
684684
i = clients.begin();
685-
i != clients.end(); ++i)
686-
{
685+
i != clients.end(); ++i) {
687686
RemoteClient *client = i->second;
688687

689688
// If definitions and textures have not been sent, don't
@@ -692,27 +691,23 @@ void Server::AsyncRunStep(bool initial_step)
692691
continue;
693692

694693
Player *player = m_env->getPlayer(client->peer_id);
695-
if(player==NULL)
696-
{
694+
if(player == NULL) {
697695
// This can happen if the client timeouts somehow
698696
/*infostream<<"WARNING: "<<__FUNCTION_NAME<<": Client "
699697
<<client->peer_id
700698
<<" has no associated player"<<std::endl;*/
701699
continue;
702700
}
703-
v3s16 pos = floatToInt(player->getPosition(), BS);
704701

705-
std::set<u16> removed_objects;
706-
std::set<u16> added_objects;
707-
m_env->getRemovedActiveObjects(pos, radius, player_radius,
702+
std::queue<u16> removed_objects;
703+
std::queue<u16> added_objects;
704+
m_env->getRemovedActiveObjects(player, radius, player_radius,
708705
client->m_known_objects, removed_objects);
709-
m_env->getAddedActiveObjects(pos, radius, player_radius,
706+
m_env->getAddedActiveObjects(player, radius, player_radius,
710707
client->m_known_objects, added_objects);
711708

712709
// Ignore if nothing happened
713-
if(removed_objects.empty() && added_objects.empty())
714-
{
715-
//infostream<<"active objects: none changed"<<std::endl;
710+
if (removed_objects.empty() && added_objects.empty()) {
716711
continue;
717712
}
718713

@@ -723,12 +718,9 @@ void Server::AsyncRunStep(bool initial_step)
723718
// Handle removed objects
724719
writeU16((u8*)buf, removed_objects.size());
725720
data_buffer.append(buf, 2);
726-
for(std::set<u16>::iterator
727-
i = removed_objects.begin();
728-
i != removed_objects.end(); ++i)
729-
{
721+
while (!removed_objects.empty()) {
730722
// Get object
731-
u16 id = *i;
723+
u16 id = removed_objects.front();
732724
ServerActiveObject* obj = m_env->getActiveObject(id);
733725

734726
// Add to data buffer for sending
@@ -740,17 +732,15 @@ void Server::AsyncRunStep(bool initial_step)
740732

741733
if(obj && obj->m_known_by_count > 0)
742734
obj->m_known_by_count--;
735+
removed_objects.pop();
743736
}
744737

745738
// Handle added objects
746739
writeU16((u8*)buf, added_objects.size());
747740
data_buffer.append(buf, 2);
748-
for(std::set<u16>::iterator
749-
i = added_objects.begin();
750-
i != added_objects.end(); ++i)
751-
{
741+
while (!added_objects.empty()) {
752742
// Get object
753-
u16 id = *i;
743+
u16 id = added_objects.front();
754744
ServerActiveObject* obj = m_env->getActiveObject(id);
755745

756746
// Get object type
@@ -778,6 +768,8 @@ void Server::AsyncRunStep(bool initial_step)
778768

779769
if(obj)
780770
obj->m_known_by_count++;
771+
772+
added_objects.pop();
781773
}
782774

783775
u32 pktSize = SendActiveObjectRemoveAdd(client->peer_id, data_buffer);

0 commit comments

Comments
 (0)
Please sign in to comment.