-
Notifications
You must be signed in to change notification settings - Fork 412
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
Reserve vector capacities and use emplace_back for constructing vectors #1464
Conversation
Looks like I forgot to run in release for the above jobs. Rerun below in
|
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.
lgtm 👍 this is informative, thanks!
lifecycle_msgs::msg::State state; | ||
state.id = static_cast<uint8_t>(state_machine_.transition_map.states[i].id); |
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 sequence currently does the construction of the state
object, sets the fields, and then push_back
onto the vector (invoking a copy, if I understand properly).
What if instead we did an emplace_back
of the default-constructed object (avoiding the copy), and then set the fields? Do you think that would be any faster? Same question below.
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.
That's a good point. I think for the service callback loops it makes sense to use resize to allocate and construct in one go. Then each field can be assigned to. I think emplace_back would have similar performance to resize (but better than reserve).
For the service callbacks though, the performance differential is minimal relative to a service call in general though. So the benchmarks don't really show an improvement for this service call.
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.
Nice cleanup, I like it.
It looks like the benchmark tests are failing for some reason, though.
Signed-off-by: Stephen Brawner <brawner@gmail.com>
Signed-off-by: Stephen Brawner <brawner@gmail.com>
11f35bc
to
efd067a
Compare
Signed-off-by: Stephen Brawner <brawner@gmail.com>
efd067a
to
012ec64
Compare
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.
The code looks good, but I'd feel slightly better if the CI was run with --packages-above rclcpp_lifecycle
.
…rs (#1464) * Reserve vector capacities and use emplace_back for constructing vectors Signed-off-by: Stephen Brawner <brawner@gmail.com> * Use resize instead of reserve Signed-off-by: Stephen Brawner <brawner@gmail.com> * Remove push_back Signed-off-by: Stephen Brawner <brawner@gmail.com>
…rs (#1464) * Reserve vector capacities and use emplace_back for constructing vectors Signed-off-by: Stephen Brawner <brawner@gmail.com> * Use resize instead of reserve Signed-off-by: Stephen Brawner <brawner@gmail.com> * Remove push_back Signed-off-by: Stephen Brawner <brawner@gmail.com>
…rs (#1464) (#1489) * Reserve vector capacities and use emplace_back for constructing vectors Signed-off-by: Stephen Brawner <brawner@gmail.com> * Use resize instead of reserve Signed-off-by: Stephen Brawner <brawner@gmail.com> * Remove push_back Signed-off-by: Stephen Brawner <brawner@gmail.com> Co-authored-by: brawner <brawner@gmail.com>
While benchmarking, I noticed some good performance improvements with
get_available_states
andget_available_transitions
. By reserving, allocations can be reduced and using emplace_back where possible can save a duplicate construction. The biggest benefits are in theget_available_states()
andget_available_transitions()
method, where there is a 3-5x performance increase.Target branch benchmarks:
This branch:
Signed-off-by: Stephen Brawner brawner@gmail.com