From d1fff288f4f5f578987acfabb6303fbe641c0b61 Mon Sep 17 00:00:00 2001 From: Arjo Chakravarty Date: Tue, 19 Nov 2024 04:46:50 +0800 Subject: [PATCH] Improve load times by skipping serialization of entities when unecessary. (#2596) * 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 * Remove magic word Signed-off-by: Arjo Chakravarty * Send empty string instead of using sentinel value. Signed-off-by: Arjo Chakravarty * Style Signed-off-by: Arjo Chakravarty * fix custom sensor system example build (#2649) Signed-off-by: Ian Chen * remove stray change Signed-off-by: Arjo Chakravarty --------- 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; }