-
Notifications
You must be signed in to change notification settings - Fork 342
Improve Model Sdf deserialization performance #3082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: gz-sim10
Are you sure you want to change the base?
Conversation
Signed-off-by: Ian Chen <[email protected]>
Signed-off-by: Ian Chen <[email protected]>
Signed-off-by: Ian Chen <[email protected]>
Signed-off-by: Ian Chen <[email protected]>
661b82c
to
051bf6b
Compare
Testing this with the Jetty demo world and gazebosim/sdformat#1589, I see that this PR still improves performance. The initial load time dropped from 33 seconds to 22 seconds on my machine, and resuming to play state from paused state reduced from 17 to 3 seconds. |
changed from draft to ready status because I think this PR still helps with performance, especially when changing from paused to play state in the jetty demo world. |
Thanks Ian! Just to clarify Im assuming we are sure that serialization and deserialization only ever happen in a single threaded context |
Not sure about serialization but this changes only the deserialization part. In the current gz-sim use case, I believe deserialization only happens in a single thread. Deserialization happens when the gui receives a state from the server, i.e. in Line 299 in fefd16b
Deserialize gz-sim/src/EntityComponentManager.cc Line 1981 in fefd16b
The state callback function does not happen in multiple threads. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our tests do rely on multi-threading and this patch would introduce a flake in our tests.
include/gz/sim/components/Model.hh
Outdated
// once. | ||
// https://github.com/gazebosim/sdformat/issues/1478 | ||
sdf::Errors errors; | ||
static sdf::SDFPtr sdfParsed; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Signed-off-by: Ian Chen <[email protected]>
include/gz/sim/components/Model.hh
Outdated
// Read an SDF string, and store the result in sdfParsed. | ||
if (!sdf::readString(sdf, config, sdfParsed, errors)) | ||
sdf::Root root; | ||
std::mutex mutex; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would also have to be static
as well since, otherwise, each instance of the component will have it's own mutex.
std::mutex mutex; | |
static std::mutex mutex; |
Signed-off-by: Ian Chen <[email protected]>
Signed-off-by: Ian Chen <[email protected]>
// https://github.com/gazebosim/sdformat/issues/1478 | ||
sdf::Errors errors; | ||
static std::mutex mutex; | ||
static sdf::SDFPtr sdfParsed; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simply adding static to a mutex may or maynot work. I can see failures on mac OS where the mutex is throwing an exception in our tests. Apparently according to this issue on another project, this may have to do with destruction. I dont think itll impact UX but it'll make our tests more flaky.
In hind-sight perhaps #3070 may be a better option for future releases of gazebo ( GZ K and up)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw the test failure and haven't been able to resolve it yet. I wasn't able to reproduce the crash locally on my mac. I'm also testing different workarounds and rerunning homebrew builds and will see if I have any luck.
Yeah, we can retarget #3070 to main
as it causes a behavior change.
🦟 Bug fix
Summary
Alternative to #3070. Instead of completely skipping the model serialization / materialization step, this PR uses a workaround to speed up the deserialization process. This is done by implementing a modified version of sdf::Root::LoadSdfString. It uses a static
sdf::SDFPtr
object to avoid multiple expensivesdf::init
calls. Note that this workaround is similar to the one done in sdf/src/pasrer.cc.We could also implement this workaround directly in sdformat but this I chose to implement it here to reduce the impact of this change during the code freeze period.
For the Jetty demo world , the load times are:
Checklist
codecheck
passed (See contributing)Generated-by: Remove this if GenAI was not used.
Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining
Signed-off-by
andGenerated-by
messages.