Skip to content

Conversation

@saikishor
Copy link
Member

This PR adds a few new methods that could be really helpful if we want to catch the information of the exported interfaces to improve the performance of the setting and getting of handles

@saikishor saikishor added backport-jazzy Triggers PR backport to ROS 2 jazzy. backport-kilted Triggers PR backport to ROS 2 kilted. labels Nov 18, 2025
@saikishor saikishor force-pushed the add/state_and_command_interface_handle_getters branch from 81b73e1 to 3472e37 Compare November 18, 2025 22:32
@codecov
Copy link

codecov bot commented Nov 18, 2025

Codecov Report

❌ Patch coverage is 46.42857% with 45 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.33%. Comparing base (fd12e90) to head (85d03d5).

Files with missing lines Patch % Lines
...ardware_interface/hardware_component_interface.hpp 55.17% 14 Missing and 12 partials ⚠️
...re_interface/include/hardware_interface/handle.hpp 26.92% 18 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2831      +/-   ##
==========================================
- Coverage   89.51%   89.33%   -0.18%     
==========================================
  Files         152      152              
  Lines       18020    18065      +45     
  Branches     1468     1476       +8     
==========================================
+ Hits        16130    16139       +9     
- Misses       1304     1330      +26     
- Partials      586      596      +10     
Flag Coverage Δ
unittests 89.33% <46.42%> (-0.18%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...re_interface/include/hardware_interface/handle.hpp 77.01% <26.92%> (-8.91%) ⬇️
...ardware_interface/hardware_component_interface.hpp 73.00% <55.17%> (-5.02%) ⬇️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@saikishor saikishor marked this pull request as draft November 19, 2025 08:55
Copy link
Member

@christophfroehlich christophfroehlich left a comment

Choose a reason for hiding this comment

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

You suggest storing the interface handles once inside the hardware components, instead of having to lookup the maps at every set/get call?

@saikishor
Copy link
Member Author

You suggest storing the interface handles once inside the hardware components, instead of having to lookup the maps at every set/get call?

Yes, this way the users will be able to cache the command interfaces and will be able to implement the logic they want. I think this way we have less execution time, so you might not need to do a lookup everytime.

@saikishor
Copy link
Member Author

However, I'm rethinking some part of API right now. I'll update with more API

@saikishor saikishor marked this pull request as ready for review November 19, 2025 17:29
Copy link
Member

@christophfroehlich christophfroehlich left a comment

Choose a reason for hiding this comment

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

The API seems to be very useful from the user perspective, but I'm not sure about the compiler optimization I commented below..

Comment on lines +830 to +832
T state;
get_state<T>(get_state_interface_handle(interface_name), state);
return state;
Copy link
Member

Choose a reason for hiding this comment

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

Why not leaving also T get_state(const StateInterface::SharedPtr & interface_handle) and use this here instead? I don't know how the compiler will be able to optimize that, so please ignore if this is not relevant.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think it is significant for most simple datatypes we support

Copy link
Member

Choose a reason for hiding this comment

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

shall we ask the @copilot ? :D

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure

Copy link
Member

Choose a reason for hiding this comment

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

I had a private chat with it ;)

but the difference in runtime is mostly theoretical

Copy link
Member Author

Choose a reason for hiding this comment

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

Lol! So you are convinced?

@mhubii
Copy link

mhubii commented Nov 19, 2025

Thanks @saikishor! This should address #2816

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enhances the HardwareComponentInterface by adding methods to retrieve state and command interface handles, along with overloaded getter/setter methods that accept handles directly. These additions enable caching of interface handles to improve performance by avoiding repeated map lookups during interface access operations.

Key Changes:

  • Added get_state_interface_handle() and get_command_interface_handle() methods to retrieve interface handles by name
  • Introduced overloaded get_state(), set_state(), get_command(), and set_command() methods that accept interface handles directly
  • Added get_value() methods in the Handle class that use output parameters instead of return values for better performance with large data types

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
hardware_interface/include/hardware_interface/hardware_component_interface.hpp Added handle retrieval methods and handle-based getter/setter overloads with ensure_get/ensure_set parameters for state and command interfaces
hardware_interface/include/hardware_interface/handle.hpp Added get_value() methods with output parameters to support thread-safe value retrieval without copying return values

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

* @param lock The lock to access the value.
* @param value The variable to store the retrieved value.
* @return true if the value is retrieved successfully, false otherwise.
* @note The method is thread-safe and non-blocking.
Copy link

Copilot AI Nov 19, 2025

Choose a reason for hiding this comment

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

The documentation states the method is 'non-blocking', but the method uses std::shared_lock which can block if a writer holds the mutex. Consider clarifying this as 'non-blocking only if the lock is successfully acquired' or removing the non-blocking claim.

Suggested change
* @note The method is thread-safe and non-blocking.
* @note The method is thread-safe and non-blocking only if the lock is already acquired.
* Acquiring the lock itself may block if a writer holds the mutex.

Copilot uses AI. Check for mistakes.
* @tparam T The type of the value to be retrieved.
* @param value The variable to store the retrieved value.
* @return true if the value is retrieved successfully, false otherwise.
* @note The method is thread-safe and non-blocking.
Copy link

Copilot AI Nov 19, 2025

Choose a reason for hiding this comment

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

The documentation incorrectly states the method is 'non-blocking'. The method uses std::try_to_lock which makes it non-blocking in terms of lock acquisition, but it returns false if the lock cannot be acquired rather than guaranteeing non-blocking behavior. The documentation should clarify that the method attempts to acquire the lock without blocking and returns false if unsuccessful.

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

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

Come onnnnn copilot 😅😅

Copy link
Member

Choose a reason for hiding this comment

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

it has a point in being more specific what "retrieved successfully" means?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think so. Atleast, I wrote it as per my understanding

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-jazzy Triggers PR backport to ROS 2 jazzy. backport-kilted Triggers PR backport to ROS 2 kilted.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants