Skip to content

Commit

Permalink
Improve load times by skipping serialization of entities when unecess…
Browse files Browse the repository at this point in the history
…ary. (#2596)

* 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 <[email protected]>

* Remove magic word

Signed-off-by: Arjo Chakravarty <[email protected]>

* Send empty string instead of using sentinel value.

Signed-off-by: Arjo Chakravarty <[email protected]>

* Style

Signed-off-by: Arjo Chakravarty <[email protected]>

* fix custom sensor system example build (#2649)

Signed-off-by: Ian Chen <[email protected]>

* remove stray change

Signed-off-by: Arjo Chakravarty <[email protected]>

---------

Signed-off-by: Arjo Chakravarty <[email protected]>
Signed-off-by: Ian Chen <[email protected]>
Co-authored-by: Ian Chen <[email protected]>
(cherry picked from commit 1a88131)
  • Loading branch information
arjo129 authored and mergify[bot] committed Nov 19, 2024
1 parent 9889df4 commit d1fff28
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions include/gz/sim/components/Model.hh
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,18 @@ namespace serializers
}
}

_out << "<?xml version=\"1.0\" ?>"
<< "<sdf version='" << SDF_PROTOCOL_VERSION << "'>"
<< (skip ? std::string() : modelElem->ToString(""))
<< "</sdf>";
if (!skip)
{
_out << "<?xml version=\"1.0\" ?>"
<< "<sdf version='" << SDF_PROTOCOL_VERSION << "'>"
<< modelElem->ToString("")
<< "</sdf>";

}
else
{
_out << "";
}
return _out;
}

Expand All @@ -89,13 +97,18 @@ namespace serializers
public: static std::istream &Deserialize(std::istream &_in,
sdf::Model &_model)
{
sdf::Root root;
std::string sdf(std::istreambuf_iterator<char>(_in), {});
if (sdf.empty())
{
return _in;
}

// Its super expensive to create an SDFElement for some reason
sdf::Root root;
sdf::Errors errors = root.LoadSdfString(sdf);
if (!root.Model())
{
gzwarn << "Unable to deserialize sdf::Model" << std::endl;
gzwarn << "Unable to deserialize sdf::Model " << sdf<< std::endl;
return _in;
}

Expand Down

0 comments on commit d1fff28

Please sign in to comment.