From af92e29bfde868daba5ffe6a023eae246f046732 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 19 Nov 2024 11:56:50 +0100 Subject: [PATCH] Improve load times by skipping serialization of entities when unecessary. (#2596) (#2682) * A hack to greatly improve load times I was investigating https://github.com/gazebosim/gazebo_test_cases/issues/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 Signed-off-by: Ian Chen Co-authored-by: Ian Chen (cherry picked from commit 1a881310cfe2e1c97bdb7d606cf973eebc17a6c1) --- include/gz/sim/components/Model.hh | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/include/gz/sim/components/Model.hh b/include/gz/sim/components/Model.hh index decd6776ed..524581f6c0 100644 --- a/include/gz/sim/components/Model.hh +++ b/include/gz/sim/components/Model.hh @@ -75,10 +75,18 @@ namespace serializers } } - _out << "" - << "" - << (skip ? std::string() : modelElem->ToString("")) - << ""; + if (!skip) + { + _out << "" + << "" + << modelElem->ToString("") + << ""; + + } + else + { + _out << ""; + } return _out; } @@ -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(_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; }