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. (backport #2596) (#2684)

* Improve load times by skipping serialization of entities when unecessary. (#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]>
Co-authored-by: Arjo Chakravarty <[email protected]>
Co-authored-by: Arjo Chakravarty <[email protected]>
  • Loading branch information
3 people authored Nov 20, 2024
1 parent a7ea4ac commit 8022579
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 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,19 @@ 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())
{
ignwarn << "Unable to deserialize sdf::Model" << std::endl;
ignwarn << "Unable to deserialize sdf::Model: " << sdf
<< std::endl;
return _in;
}

Expand Down

0 comments on commit 8022579

Please sign in to comment.