Skip to content
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

Use ComponentState::PeriodicChange in UpdateState to avoid forcing full scene update #486

Merged
merged 6 commits into from
Jan 15, 2021

Conversation

ivanpauno
Copy link
Contributor

When trying to implement primary-secondary async stepping in #481, I found out that there was almost no speed-up in doing that because the primary was running quite slowly.

After some profiling I realized that the SceneBroadcaster::PostUpdate step was much slower than in an standalone simulation because a full scene update was always forced.

This PR modifies EntityComponentManager::SetState(const ignition::msgs::SerializedStateMap &_stateMsg) so when a component is updated a ComponentState::PeriodicChange change is set instead of a ComponentState::OneTimeChange, avoiding to regenerate a full scene all the time.

With this change, the distributed_levels example works ~4 times faster when using a distributed simulation.

I'm not sure if using ComponentState::PeriodicChange is always correct, so we can consider the following alternatives:

  • NetworkManagerPrimary::Step is not the only consumer of EntityComponentManager::SetState.
    If for the other consumers doing a ComponentState::OneTimeChange is not a problem and for NetworkManagerPrimary::Step using ComponentState::PeriodicChange is always ok, I could add an extra argument to the method.
  • If for some components updates ComponentState::OneTimeChange make more sense and for others ComponentState::PeriodicChange make more sense, the we should add that information to the SerializedStateMap message.

@ivanpauno ivanpauno added the enhancement New feature or request label Dec 14, 2020
@ivanpauno ivanpauno requested a review from mjcarroll December 14, 2020 17:00
@ivanpauno ivanpauno self-assigned this Dec 14, 2020
@ivanpauno ivanpauno requested a review from chapulina as a code owner December 14, 2020 17:00
@github-actions github-actions bot added the 🔮 dome Ignition Dome label Dec 14, 2020
@mjcarroll
Copy link
Contributor

With this change, the distributed_levels example works ~4 times faster when using a distributed simulation.

Four times faster than a standalone sim, or the previous implementation?

@ivanpauno
Copy link
Contributor Author

Four times faster than a standalone sim, or the previous implementation?

Than the previous implementation.

I don't think that the simple distributed_levels example can ever run faster in "distributed mode" than in "standalone mode", because the world is extremely simple.

@ivanpauno
Copy link
Contributor Author

Some actual numbers in my machine using the distributed_levels world

  • Current master, standalone: RTF ~ 1000%
  • Current master, distributed: RTF ~ 40%
  • This PR, distributed: RTF ~ 150%

Using the distributed_levels world but replacing the sphere and the box with two EXPLORER_X1_SENSOR_CONFIG_2.

  • Current master, standalone: RTF ~ 150%
  • Current master, distributed: RTF ~ 18%
  • This PR, distributed: RTF ~ 70%
  • async stepping without this change, distributed: RTF ~ 20%
  • async stepping with this change, distributed: RTF ~ 120%

Note: The RTF numbers above is an "average" I made from visually inspecting the RTF factor in the GUI, so don't get them too seriously ... it's just an rough approximation to understand the performance benefit.

@nkoenig
Copy link
Contributor

nkoenig commented Dec 17, 2020

Could this be changed to target citadel --> ign-gazebo3?

@ivanpauno
Copy link
Contributor Author

ivanpauno commented Dec 17, 2020

Could this be changed to target citadel --> ign-gazebo3?

I took a quick look and targeting ign-gazebo3 branch doesn't look hard.
I will update the target branch tomorrow.

(edit) tomorrow, not on Monday 😂

@ivanpauno
Copy link
Contributor Author

@nkoenig do you mean only this PR or also #481?

@ivanpauno ivanpauno force-pushed the ivanpauno/dont-force-full-scene-update branch from 68cdcb7 to c8a432f Compare December 18, 2020 14:49
@ivanpauno ivanpauno requested a review from azeey as a code owner December 18, 2020 14:49
@ivanpauno ivanpauno changed the base branch from ign-gazebo4 to ign-gazebo3 December 18, 2020 14:49
@ivanpauno
Copy link
Contributor Author

Updated base branch to ign-gazebo3, I can open another one targeting ign-gazebo4 if desired.
@nkoenig let me know if you want a version of #481 targeting ign-gazebo3 too.

@codecov
Copy link

codecov bot commented Dec 18, 2020

Codecov Report

Merging #486 (6c57ab2) into main (d392223) will increase coverage by 0.05%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #486      +/-   ##
==========================================
+ Coverage   77.56%   77.62%   +0.05%     
==========================================
  Files         211      211              
  Lines       11579    11582       +3     
==========================================
+ Hits         8981     8990       +9     
+ Misses       2598     2592       -6     
Impacted Files Coverage Δ
src/EntityComponentManager.cc 84.52% <100.00%> (+0.02%) ⬆️
src/network/NetworkManagerSecondary.cc 81.25% <100.00%> (+0.60%) ⬆️
src/SimulationRunner.cc 94.76% <0.00%> (+1.16%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 42e4dc4...6c57ab2. Read the comment docs.

@ivanpauno
Copy link
Contributor Author

@mjcarroll @chapulina friendly ping.
I'm not sure if the approach of this PR is correct or not, so it would be great to have feedback to know if we want to explore some of the alternatives mentioned in the PR description or if this is okay.

@ivanpauno ivanpauno added 🏰 citadel Ignition Citadel and removed 🔮 dome Ignition Dome labels Dec 21, 2020
@chapulina
Copy link
Contributor

Nice catch. My concern is that important one-time changes may be missed if we make all changes period. For example, the color of a light is changed in one secondary, and another secondary misses it and keeps seeing the old color.

we should add that information to the SerializedStateMap message.

Yeah I think this could work. We'd have to target this PR at Edifice though (main), because changing a message breaks ABI.

@ivanpauno ivanpauno force-pushed the ivanpauno/dont-force-full-scene-update branch from 57f121f to 93d1ea7 Compare December 22, 2020 16:56
@ivanpauno ivanpauno changed the base branch from ign-gazebo3 to main December 22, 2020 16:56
@ivanpauno
Copy link
Contributor Author

Yeah I think this could work. We'd have to target this PR at Edifice though (main), because changing a message breaks ABI.

Done.
I only added one is_periodic_change field to the whole message, I could've added one field per component but I don't see the benefit of doing that.

Each time I change the base branch I bring more reviewers 😂

@ivanpauno ivanpauno removed the request for review from azeey December 22, 2020 16:59
@ivanpauno ivanpauno added the 🏢 edifice Ignition Edifice label Dec 22, 2020
@chapulina
Copy link
Contributor

I only added one is_periodic_change field to the whole message

Is there a PR to ign-msgs too?

I could've added one field per component but I don't see the benefit of doing that.

This is related to something @luca-della-vedova was looking into recently. They have a use case using animated actors which could take advantage of more granular OneTimeChanges, so that the state is only updated for the entities / components that had those changes. In very large worlds, it can be quite expensive to update thousands of components because one of them changed. So I think there could be value in having that field at the component level, but that may bring its own overhead.

@ivanpauno
Copy link
Contributor Author

Is there a PR to ign-msgs too?

Now there is 😄 gazebosim/gz-msgs#119 .

This is related to something @luca-della-vedova was looking into recently. They have a use case using animated actors which could take advantage of more granular OneTimeChanges, so that the state is only updated for the entities / components that had those changes

But in that case, the changes that aren't needed can be omitted from the state message, right?
I'm not sure if more granularity is useful in the message itself ...

@chapulina
Copy link
Contributor

But in that case, the changes that aren't needed can be omitted from the state message, right?

Yeah good point, they shouldn't be there at all when they aren't needed. I'm just trying to think now of any downsides to marking periodic changes as one time changes. The receiving end of that state message won't be able to tell apart which are the one time changes. Are you concerned about the overhead of marking every single component?

In any case, I think it's worth it trying the current approach. We can always add more granularity later if needed.

@chapulina chapulina added the needs upstream release Blocked by a release of an upstream library label Dec 22, 2020
@chapulina
Copy link
Contributor

Now that this requires ign-msgs's main branch, this is needed before it can be merged: gazebo-tooling/release-tools#362

@ivanpauno
Copy link
Contributor Author

The receiving end of that state message won't be able to tell apart which are the one time changes

Yes that's true, if there's a potential benefit of having that information in the message maybe it's better to add it now.
AFAIS there's no code currently telling apart periodic changes from one time changes, the only thing that seems to be used is HasOneTimeComponentChanges() (there's no code calling ComponentState()).

Are you concerned about the overhead of marking every single component?

It will add some overhead, but probably minimal. It requires N lookups to this std::map while iterating the components to add to the message.

Signed-off-by: Ivan Santiago Paunovic <[email protected]>
Signed-off-by: Ivan Santiago Paunovic <[email protected]>
Copy link
Contributor

@chapulina chapulina left a comment

Choose a reason for hiding this comment

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

there's no code currently telling apart periodic changes from one time changes

So far 😉 But we may be changing this soon for performance. In any case, I think this may not affect the internals of the State / SetState calls.


This looks ok for me, but we need to wait for the transition to ign-msgs7 to be complete before merging (gazebo-tooling/release-tools#362).

Signed-off-by: Ivan Santiago Paunovic <[email protected]>
@nkoenig nkoenig changed the base branch from main to ign-gazebo5 December 23, 2020 23:34
@chapulina chapulina changed the base branch from ign-gazebo5 to main January 5, 2021 00:28
@chapulina chapulina added the performance Runtime performance label Jan 5, 2021
@nkoenig
Copy link
Contributor

nkoenig commented Jan 7, 2021

Updated base branch to ign-gazebo3, I can open another one targeting ign-gazebo4 if desired.
@nkoenig let me know if you want a version of #481 targeting ign-gazebo3 too.

Sorry for the late reply, I meant both.

@ivanpauno
Copy link
Contributor Author

Sorry for the late reply, I meant both.

I have pushed a branch with #481 rebased for citadel
https://github.com/ignitionrobotics/ign-gazebo/tree/ivanpauno/secondary-async-stepping-and-primary-acknowledges-citadel and similar for the ign-imgui tool https://github.com/ivanpauno/ign_imgui/tree/citadel.
I'm still testing that though, I will comment again after checking it still works.

The instructions to build, run the simulation and see the results are here, feel free to ask if that's not clear.

PS: #481 already includes an optimization similar to this PR, so this one doesn't need to be rebased to ign-gazebo3 (and the approach taken here depeneds on gazebosim/gz-msgs#119).

@ivanpauno
Copy link
Contributor Author

I'm still testing that though, I will comment again after checking it still works.

Tested, the citadel branches are working too.

@chapulina
Copy link
Contributor

re: citadel: This needs to target Edifice because of the new message field.

@chapulina
Copy link
Contributor

Let's get this in! We still have a couple months until the Edifice release to revert in case we notice something weird.

@chapulina chapulina removed the needs upstream release Blocked by a release of an upstream library label Jan 15, 2021
@chapulina chapulina merged commit d5f96fb into main Jan 15, 2021
@chapulina chapulina deleted the ivanpauno/dont-force-full-scene-update branch January 15, 2021 22:35
iche033 added a commit that referenced this pull request Jan 26, 2021
chapulina pushed a commit that referenced this pull request Jan 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏢 edifice Ignition Edifice enhancement New feature or request performance Runtime performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants