Skip to content

Commit e4b09ee

Browse files
Improve load times by skipping serialization of entities when unecessary. (#2596) (#2683)
* A hack to greatly improve load times I was investigating gazebosim/gazebo_test_cases#1576 , in my investigation it came to my notice that `sdf::Element` takes forever to destroy (We should open a ticket somewhere about this). If we are skipping serialization we might as well not create and destroy an SDF Element. This hack greatly speeds up the load time for gazebo. Signed-off-by: Arjo Chakravarty <arjoc@intrinsic.ai> Signed-off-by: Ian Chen <ichen@openrobotics.org> Co-authored-by: Ian Chen <ichen@openrobotics.org> (cherry picked from commit 1a88131)
1 parent 1bbb645 commit e4b09ee

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

include/gz/sim/components/Model.hh

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,18 @@ namespace serializers
7575
}
7676
}
7777

78-
_out << "<?xml version=\"1.0\" ?>"
79-
<< "<sdf version='" << SDF_PROTOCOL_VERSION << "'>"
80-
<< (skip ? std::string() : modelElem->ToString(""))
81-
<< "</sdf>";
78+
if (!skip)
79+
{
80+
_out << "<?xml version=\"1.0\" ?>"
81+
<< "<sdf version='" << SDF_PROTOCOL_VERSION << "'>"
82+
<< modelElem->ToString("")
83+
<< "</sdf>";
84+
85+
}
86+
else
87+
{
88+
_out << "";
89+
}
8290
return _out;
8391
}
8492

@@ -89,13 +97,18 @@ namespace serializers
8997
public: static std::istream &Deserialize(std::istream &_in,
9098
sdf::Model &_model)
9199
{
92-
sdf::Root root;
93100
std::string sdf(std::istreambuf_iterator<char>(_in), {});
101+
if (sdf.empty())
102+
{
103+
return _in;
104+
}
94105

106+
// Its super expensive to create an SDFElement for some reason
107+
sdf::Root root;
95108
sdf::Errors errors = root.LoadSdfString(sdf);
96109
if (!root.Model())
97110
{
98-
gzwarn << "Unable to deserialize sdf::Model" << std::endl;
111+
gzwarn << "Unable to deserialize sdf::Model " << sdf<< std::endl;
99112
return _in;
100113
}
101114

0 commit comments

Comments
 (0)