Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 14 additions & 62 deletions include/gz/sim/components/Model.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,52 +41,17 @@ namespace serializers
/// \param[in] _time Model to stream
/// \return The stream.
public: static std::ostream &Serialize(std::ostream &_out,
const sdf::Model &_model)
const sdf::Model &)
{
sdf::ElementPtr modelElem = _model.Element();
if (!modelElem)
{
gzwarn << "Unable to serialize sdf::Model" << std::endl;
return _out;
}

bool skip = false;
if (modelElem->HasElement("pose"))
{
sdf::ElementPtr poseElem = modelElem->GetElement("pose");
if (poseElem->GetAttribute("relative_to")->GetSet())
{
// Skip serializing models with //pose/@relative_to attribute
// since deserialization will fail. This could be a nested model.
// see https://github.com/gazebosim/gz-sim/issues/1071
// Once https://github.com/gazebosim/sdformat/issues/820 is
// resolved, there should be an API that returns sdf::Errors objects
// instead of printing console msgs so it would be easier to ignore
// specific errors in Deserialize.
static bool warned = false;
if (!warned)
{
gzwarn << "Skipping serialization / deserialization for models "
<< "with //pose/@relative_to attribute."
<< std::endl;
warned = true;
}
skip = true;
}
}

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

}
else
{
_out << "";
}
// Skip serialization of model sdf
// \todo(iche033) It was found that deserialization is
// very expensive, which impacts initial load time of a world with many
// entities. Currently only the server consumes ModelSdf components
// so let's skip serialization until either the deserialization
// performance is improved or when the GUI needs the to use Model SDF
// components.
// see, https://github.com/gazebosim/sdformat/issues/1478
_out << "";
return _out;
}

Expand All @@ -95,24 +60,11 @@ namespace serializers
/// \param[out] _model Model to populate
/// \return The stream.
public: static std::istream &Deserialize(std::istream &_in,
sdf::Model &_model)
sdf::Model &)
{
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 " << sdf<< std::endl;
return _in;
}

_model = *root.Model();
// Its super expensive to create an SDFElement for some reason.
// So seriazliation / deserialization of model sdf is skipped
// https://github.com/gazebosim/sdformat/issues/1478
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove all the code in here as well since we know we won't be deserializing anything.

return _in;
}
};
Expand Down
6 changes: 4 additions & 2 deletions test/integration/components.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1276,8 +1276,10 @@ TEST_F(ComponentsTest, ModelSdf)
std::istringstream istr(ostr.str());
comp2.Deserialize(istr);

EXPECT_EQ("my_model", comp2.Data().Name());
EXPECT_EQ(1u, comp2.Data().LinkCount());
// Model sdf serialization / deserialization is disabled due to perf issue
// https://github.com/gazebosim/sdformat/issues/1478
// EXPECT_EQ("my_model", comp2.Data().Name());
// EXPECT_EQ(1u, comp2.Data().LinkCount());
}

/////////////////////////////////////////////////
Expand Down
Loading