Skip to content

Commit 471e567

Browse files
committed
Value copy / allocation optimizations mostly in server, SAO and serialize code
1 parent 2fd5f38 commit 471e567

16 files changed

Lines changed: 52 additions & 64 deletions

src/client/game.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2806,7 +2806,7 @@ void Game::handleClientEvent_SetSky(ClientEvent *event, CameraOrientation *cam)
28062806
// Shows the mesh skybox
28072807
sky->setVisible(true);
28082808
// Update mesh based skybox colours if applicable.
2809-
sky->setSkyColors(*event->set_sky);
2809+
sky->setSkyColors(event->set_sky->sky_color);
28102810
sky->setHorizonTint(
28112811
event->set_sky->fog_sun_tint,
28122812
event->set_sky->fog_moon_tint,

src/client/sky.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -907,9 +907,9 @@ void Sky::setStarCount(u16 star_count, bool force_update)
907907
}
908908
}
909909

910-
void Sky::setSkyColors(const SkyboxParams sky)
910+
void Sky::setSkyColors(const SkyColor &sky_color)
911911
{
912-
m_sky_params.sky_color = sky.sky_color;
912+
m_sky_params.sky_color = sky_color;
913913
}
914914

915915
void Sky::setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,

src/client/sky.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Sky : public scene::ISceneNode
9494
m_bgcolor = bgcolor;
9595
m_skycolor = skycolor;
9696
}
97-
void setSkyColors(const SkyboxParams sky);
97+
void setSkyColors(const SkyColor &sky_color);
9898
void setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
9999
std::string use_sun_tint);
100100
void setInClouds(bool clouds) { m_in_clouds = clouds; }

src/content/mods.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ std::map<std::string, ModSpec> getModsInPath(
167167
return result;
168168
}
169169

170-
std::vector<ModSpec> flattenMods(std::map<std::string, ModSpec> mods)
170+
std::vector<ModSpec> flattenMods(const std::map<std::string, ModSpec> &mods)
171171
{
172172
std::vector<ModSpec> result;
173173
for (const auto &it : mods) {

src/content/mods.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ std::map<std::string, ModSpec> getModsInPath(
6868
const std::string &path, bool part_of_modpack = false);
6969

7070
// replaces modpack Modspecs with their content
71-
std::vector<ModSpec> flattenMods(std::map<std::string, ModSpec> mods);
71+
std::vector<ModSpec> flattenMods(const std::map<std::string, ModSpec> &mods);
7272

7373
// a ModConfiguration is a subset of installed mods, expected to have
7474
// all dependencies fullfilled, so it can be used as a list of mods to

src/script/cpp_api/s_node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ struct EnumString ScriptApiNode::es_NodeBoxType[] =
9494
};
9595

9696
bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
97-
ServerActiveObject *puncher, PointedThing pointed)
97+
ServerActiveObject *puncher, const PointedThing &pointed)
9898
{
9999
SCRIPTAPI_PRECHECKHEADER
100100

src/script/cpp_api/s_node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ScriptApiNode
3636
virtual ~ScriptApiNode() = default;
3737

3838
bool node_on_punch(v3s16 p, MapNode node,
39-
ServerActiveObject *puncher, PointedThing pointed);
39+
ServerActiveObject *puncher, const PointedThing &pointed);
4040
bool node_on_dig(v3s16 p, MapNode node,
4141
ServerActiveObject *digger);
4242
void node_on_construct(v3s16 p, MapNode node);

src/server.cpp

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -718,34 +718,35 @@ void Server::AsyncRunStep(bool initial_step)
718718
std::unordered_map<u16, std::vector<ActiveObjectMessage>*> buffered_messages;
719719

720720
// Get active object messages from environment
721+
ActiveObjectMessage aom(0);
722+
u32 aom_count = 0;
721723
for(;;) {
722-
ActiveObjectMessage aom = m_env->getActiveObjectMessage();
723-
if (aom.id == 0)
724+
if (!m_env->getActiveObjectMessage(&aom))
724725
break;
725726

726727
std::vector<ActiveObjectMessage>* message_list = nullptr;
727-
std::unordered_map<u16, std::vector<ActiveObjectMessage>* >::iterator n;
728-
n = buffered_messages.find(aom.id);
728+
auto n = buffered_messages.find(aom.id);
729729
if (n == buffered_messages.end()) {
730730
message_list = new std::vector<ActiveObjectMessage>;
731731
buffered_messages[aom.id] = message_list;
732-
}
733-
else {
732+
} else {
734733
message_list = n->second;
735734
}
736-
message_list->push_back(aom);
735+
message_list->push_back(std::move(aom));
736+
aom_count++;
737737
}
738738

739-
m_aom_buffer_counter->increment(buffered_messages.size());
739+
m_aom_buffer_counter->increment(aom_count);
740740

741741
m_clients.lock();
742742
const RemoteClientMap &clients = m_clients.getClientList();
743743
// Route data to every client
744+
std::string reliable_data, unreliable_data;
744745
for (const auto &client_it : clients) {
746+
reliable_data.clear();
747+
unreliable_data.clear();
745748
RemoteClient *client = client_it.second;
746749
PlayerSAO *player = getPlayerSAO(client->peer_id);
747-
std::string reliable_data;
748-
std::string unreliable_data;
749750
// Go through all objects in message buffer
750751
for (const auto &buffered_message : buffered_messages) {
751752
// If object does not exist or is not known by client, skip it
@@ -770,19 +771,15 @@ void Server::AsyncRunStep(bool initial_step)
770771
client->m_known_objects.end())
771772
continue;
772773
}
773-
// Compose the full new data with header
774-
std::string new_data;
775-
// Add object id
776-
char buf[2];
777-
writeU16((u8*)&buf[0], aom.id);
778-
new_data.append(buf, 2);
779-
// Add data
780-
new_data += serializeString(aom.datastring);
781-
// Add data to buffer
782-
if (aom.reliable)
783-
reliable_data += new_data;
784-
else
785-
unreliable_data += new_data;
774+
775+
// Add full new data to appropriate buffer
776+
std::string &buffer = aom.reliable ? reliable_data : unreliable_data;
777+
char idbuf[2];
778+
writeU16((u8*) idbuf, aom.id);
779+
// u16 id
780+
// std::string data
781+
buffer.append(idbuf, sizeof(idbuf));
782+
buffer.append(serializeString(aom.datastring));
786783
}
787784
}
788785
/*

src/server/luaentity_sao.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
119119
m_properties_sent = true;
120120
std::string str = getPropertyPacket();
121121
// create message and add to list
122-
ActiveObjectMessage aom(getId(), true, str);
123-
m_messages_out.push(aom);
122+
m_messages_out.emplace(getId(), true, str);
124123
}
125124

126125
// If attached, check that our parent is still there. If it isn't, detach.
@@ -228,16 +227,14 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
228227
m_animation_sent = true;
229228
std::string str = generateUpdateAnimationCommand();
230229
// create message and add to list
231-
ActiveObjectMessage aom(getId(), true, str);
232-
m_messages_out.push(aom);
230+
m_messages_out.emplace(getId(), true, str);
233231
}
234232

235233
if (!m_animation_speed_sent) {
236234
m_animation_speed_sent = true;
237235
std::string str = generateUpdateAnimationSpeedCommand();
238236
// create message and add to list
239-
ActiveObjectMessage aom(getId(), true, str);
240-
m_messages_out.push(aom);
237+
m_messages_out.emplace(getId(), true, str);
241238
}
242239

243240
if (!m_bone_position_sent) {
@@ -247,17 +244,15 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
247244
std::string str = generateUpdateBonePositionCommand((*ii).first,
248245
(*ii).second.X, (*ii).second.Y);
249246
// create message and add to list
250-
ActiveObjectMessage aom(getId(), true, str);
251-
m_messages_out.push(aom);
247+
m_messages_out.emplace(getId(), true, str);
252248
}
253249
}
254250

255251
if (!m_attachment_sent) {
256252
m_attachment_sent = true;
257253
std::string str = generateUpdateAttachmentCommand();
258254
// create message and add to list
259-
ActiveObjectMessage aom(getId(), true, str);
260-
m_messages_out.push(aom);
255+
m_messages_out.emplace(getId(), true, str);
261256
}
262257
}
263258

src/server/player_sao.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ void PlayerSAO::step(float dtime, bool send_recommended)
223223
m_properties_sent = true;
224224
std::string str = getPropertyPacket();
225225
// create message and add to list
226-
ActiveObjectMessage aom(getId(), true, str);
227-
m_messages_out.push(aom);
226+
m_messages_out.emplace(getId(), true, str);
228227
m_env->getScriptIface()->player_event(this, "properties_changed");
229228
}
230229

@@ -324,10 +323,8 @@ void PlayerSAO::step(float dtime, bool send_recommended)
324323

325324
if (!m_attachment_sent) {
326325
m_attachment_sent = true;
327-
std::string str = generateUpdateAttachmentCommand();
328326
// create message and add to list
329-
ActiveObjectMessage aom(getId(), true, str);
330-
m_messages_out.push(aom);
327+
m_messages_out.emplace(getId(), true, generateUpdateAttachmentCommand());
331328
}
332329
}
333330

0 commit comments

Comments
 (0)