Skip to content

Commit ef0aa7d

Browse files
committedJan 13, 2017
Optimize SAO getStaticData by using std::string pointer instead of return copy
Signed-off-by: Loic Blot <loic.blot@unix-experience.fr>
1 parent e2dd96b commit ef0aa7d

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed
 

Diff for: ‎src/content_sao.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version)
521521
return os.str();
522522
}
523523

524-
std::string LuaEntitySAO::getStaticData() const
524+
void LuaEntitySAO::getStaticData(std::string *result) const
525525
{
526526
verbosestream<<FUNCTION_NAME<<std::endl;
527527
std::ostringstream os(std::ios::binary);
@@ -543,7 +543,7 @@ std::string LuaEntitySAO::getStaticData() const
543543
writeV3F1000(os, m_velocity);
544544
// yaw
545545
writeF1000(os, m_yaw);
546-
return os.str();
546+
*result = os.str();
547547
}
548548

549549
int LuaEntitySAO::punch(v3f dir,
@@ -918,10 +918,9 @@ std::string PlayerSAO::getClientInitializationData(u16 protocol_version)
918918
return os.str();
919919
}
920920

921-
std::string PlayerSAO::getStaticData() const
921+
void PlayerSAO::getStaticData(std::string *result) const
922922
{
923-
FATAL_ERROR("Deprecated function (?)");
924-
return "";
923+
FATAL_ERROR("Deprecated function");
925924
}
926925

927926
void PlayerSAO::step(float dtime, bool send_recommended)

Diff for: ‎src/content_sao.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class LuaEntitySAO : public UnitSAO
102102
const std::string &data);
103103
void step(float dtime, bool send_recommended);
104104
std::string getClientInitializationData(u16 protocol_version);
105-
std::string getStaticData() const;
105+
void getStaticData(std::string *result) const;
106106
int punch(v3f dir,
107107
const ToolCapabilities *toolcap=NULL,
108108
ServerActiveObject *puncher=NULL,
@@ -199,7 +199,7 @@ class PlayerSAO : public UnitSAO
199199
void removingFromEnvironment();
200200
bool isStaticAllowed() const { return false; }
201201
std::string getClientInitializationData(u16 protocol_version);
202-
std::string getStaticData() const;
202+
void getStaticData(std::string *result) const;
203203
void step(float dtime, bool send_recommended);
204204
void setBasePosition(const v3f &position);
205205
void setPos(const v3f &pos);

Diff for: ‎src/serverenvironment.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -1694,7 +1694,8 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
16941694
{
16951695
// Add static object to active static list of the block
16961696
v3f objectpos = object->getBasePosition();
1697-
std::string staticdata = object->getStaticData();
1697+
std::string staticdata = "";
1698+
object->getStaticData(&staticdata);
16981699
StaticObject s_obj(object->getType(), objectpos, staticdata);
16991700
// Add to the block where the object is located in
17001701
v3s16 blockpos = getNodeBlockPos(floatToInt(objectpos, BS));
@@ -1980,7 +1981,8 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
19801981
<<std::endl;
19811982
continue;
19821983
}
1983-
std::string staticdata_new = obj->getStaticData();
1984+
std::string staticdata_new = "";
1985+
obj->getStaticData(&staticdata_new);
19841986
StaticObject s_obj(obj->getType(), objectpos, staticdata_new);
19851987
block->m_static_objects.insert(id, s_obj);
19861988
obj->m_static_block = blockpos_o;
@@ -2020,7 +2022,8 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
20202022
if(obj->isStaticAllowed())
20212023
{
20222024
// Create new static object
2023-
std::string staticdata_new = obj->getStaticData();
2025+
std::string staticdata_new = "";
2026+
obj->getStaticData(&staticdata_new);
20242027
StaticObject s_obj(obj->getType(), objectpos, staticdata_new);
20252028

20262029
bool stays_in_same_block = false;

Diff for: ‎src/serverobject.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ class ServerActiveObject : public ActiveObject
119119
when it is created (converted from static to active - actually
120120
the data is the static form)
121121
*/
122-
virtual std::string getStaticData()
122+
virtual void getStaticData(std::string *result)
123123
{
124124
assert(isStaticAllowed());
125-
return "";
125+
*result = "";
126126
}
127127
/*
128128
Return false in here to never save and instead remove object

0 commit comments

Comments
 (0)
Please sign in to comment.