Optimize Server::sendMetadataChanged a bit

The distance check also never worked as intended, now fixed.
This commit is contained in:
sfan5 2022-05-26 14:40:00 +02:00
parent f195db2d14
commit 261a8db9dd
2 changed files with 21 additions and 20 deletions

@ -891,7 +891,7 @@ void Server::AsyncRunStep(bool initial_step)
// We'll log the amount of each // We'll log the amount of each
Profiler prof; Profiler prof;
std::list<v3s16> node_meta_updates; std::unordered_set<v3s16> node_meta_updates;
while (!m_unsent_map_edit_queue.empty()) { while (!m_unsent_map_edit_queue.empty()) {
MapEditEvent* event = m_unsent_map_edit_queue.front(); MapEditEvent* event = m_unsent_map_edit_queue.front();
@ -918,9 +918,7 @@ void Server::AsyncRunStep(bool initial_step)
case MEET_BLOCK_NODE_METADATA_CHANGED: { case MEET_BLOCK_NODE_METADATA_CHANGED: {
prof.add("MEET_BLOCK_NODE_METADATA_CHANGED", 1); prof.add("MEET_BLOCK_NODE_METADATA_CHANGED", 1);
if (!event->is_private_change) { if (!event->is_private_change) {
// Don't send the change yet. Collect them to eliminate dupes. node_meta_updates.emplace(event->p);
node_meta_updates.remove(event->p);
node_meta_updates.push_back(event->p);
} }
if (MapBlock *block = m_env->getMap().getBlockNoCreateNoEx( if (MapBlock *block = m_env->getMap().getBlockNoCreateNoEx(
@ -973,7 +971,7 @@ void Server::AsyncRunStep(bool initial_step)
} }
// Send all metadata updates // Send all metadata updates
if (node_meta_updates.size()) if (!node_meta_updates.empty())
sendMetadataChanged(node_meta_updates); sendMetadataChanged(node_meta_updates);
} }
@ -2290,12 +2288,12 @@ void Server::sendAddNode(v3s16 p, MapNode n, std::unordered_set<u16> *far_player
} }
} }
void Server::sendMetadataChanged(const std::list<v3s16> &meta_updates, float far_d_nodes) void Server::sendMetadataChanged(const std::unordered_set<v3s16> &positions, float far_d_nodes)
{ {
float maxd = far_d_nodes * BS;
NodeMetadataList meta_updates_list(false); NodeMetadataList meta_updates_list(false);
std::vector<session_t> clients = m_clients.getClientIDs(); std::ostringstream os(std::ios::binary);
std::vector<session_t> clients = m_clients.getClientIDs();
ClientInterface::AutoLock clientlock(m_clients); ClientInterface::AutoLock clientlock(m_clients);
for (session_t i : clients) { for (session_t i : clients) {
@ -2303,18 +2301,20 @@ void Server::sendMetadataChanged(const std::list<v3s16> &meta_updates, float far
if (!client) if (!client)
continue; continue;
ServerActiveObject *player = m_env->getActiveObject(i); ServerActiveObject *player = getPlayerSAO(i);
v3f player_pos = player ? player->getBasePosition() : v3f(); v3s16 player_pos;
if (player)
player_pos = floatToInt(player->getBasePosition(), BS);
for (const v3s16 &pos : meta_updates) { for (const v3s16 pos : positions) {
NodeMetadata *meta = m_env->getMap().getNodeMetadata(pos); NodeMetadata *meta = m_env->getMap().getNodeMetadata(pos);
if (!meta) if (!meta)
continue; continue;
v3s16 block_pos = getNodeBlockPos(pos); v3s16 block_pos = getNodeBlockPos(pos);
if (!client->isBlockSent(block_pos) || (player && if (!client->isBlockSent(block_pos) ||
player_pos.getDistanceFrom(intToFloat(pos, BS)) > maxd)) { player_pos.getDistanceFrom(pos) > far_d_nodes) {
client->SetBlockNotSent(block_pos); client->SetBlockNotSent(block_pos);
continue; continue;
} }
@ -2326,14 +2326,15 @@ void Server::sendMetadataChanged(const std::list<v3s16> &meta_updates, float far
continue; continue;
// Send the meta changes // Send the meta changes
std::ostringstream os(std::ios::binary); os.str("");
meta_updates_list.serialize(os, client->serialization_version, false, true, true); meta_updates_list.serialize(os, client->serialization_version, false, true, true);
std::ostringstream oss(std::ios::binary); std::string raw = os.str();
compressZlib(os.str(), oss); os.str("");
compressZlib(raw, os);
NetworkPacket pkt(TOCLIENT_NODEMETA_CHANGED, 0); NetworkPacket pkt(TOCLIENT_NODEMETA_CHANGED, 0, i);
pkt.putLongString(oss.str()); pkt.putLongString(os.str());
m_clients.send(i, 0, &pkt, true); Send(&pkt);
meta_updates_list.clear(); meta_updates_list.clear();
} }

@ -492,7 +492,7 @@ private:
std::unordered_set<u16> *far_players = nullptr, std::unordered_set<u16> *far_players = nullptr,
float far_d_nodes = 100, bool remove_metadata = true); float far_d_nodes = 100, bool remove_metadata = true);
void sendMetadataChanged(const std::list<v3s16> &meta_updates, void sendMetadataChanged(const std::unordered_set<v3s16> &positions,
float far_d_nodes = 100); float far_d_nodes = 100);
// Environment and Connection must be locked when called // Environment and Connection must be locked when called