Skip to content

Service callback without shared pointer #3078

Open

Description

@oysstu

using SharedPtrCallback = std::function<
void (
std::shared_ptr<typename ServiceT::Request>,
std::shared_ptr<typename ServiceT::Response>
)>;
using SharedPtrWithRequestHeaderCallback = std::function<
void (
std::shared_ptr<rmw_request_id_t>,
std::shared_ptr<typename ServiceT::Request>,
std::shared_ptr<typename ServiceT::Response>
)>;
using SharedPtrDeferResponseCallback = std::function<
void (
std::shared_ptr<rmw_request_id_t>,
std::shared_ptr<typename ServiceT::Request>
)>;
using SharedPtrDeferResponseCallbackWithServiceHandle = std::function<
void (
std::shared_ptr<rclcpp::Service<ServiceT>>,
std::shared_ptr<rmw_request_id_t>,
std::shared_ptr<typename ServiceT::Request>
)>;
std::variant<
std::monostate,
SharedPtrCallback,
SharedPtrWithRequestHeaderCallback,
SharedPtrDeferResponseCallback,
SharedPtrDeferResponseCallbackWithServiceHandle> callback_;
};

Would it be a good idea to support a service callback with a const reference and reference for services? At least for the overload without the request id.

Many services are able to return immediately and does not need the request/response object lifetime to exceed the call site. In general, it's also much cheaper to copy a small struct instead of doing atomic increments/decrements of shared pointers.

Activity

  1. jmachowinski commented on Feb 20, 2026

    @jmachowinski
    Collaborator

    Yes, feel free to open a PR for that ;-)

  2. oysstu commented on Feb 20, 2026

    @oysstu
    ContributorAuthor

    Looking a bit closer, even the subscription taking a reference is just a dereferenced shared pointer. It's shared pointers all the way down to the rmw layer. I'd be rewriting all of rclcpp to make this happen.

    It's possible to do something similar to the subscriptions, just dereferencing the shared pointer, and then user-facing code can start using references even if there's shared pointers under the hood.

  3. jmachowinski commented on Feb 20, 2026

    @jmachowinski
    Collaborator

    any_subscription_callback.hpp should contain an example of how the shared ptr is received internally, and then converted to a const ref for the user callback. You would need to adapt the service callback accordingly.

  4. mini-1235 commented on Oct 5, 2026

    @mini-1235
    Contributor

    @jmachowinski Would you be okay with adding support for callbacks taking const SharedFuture & in async_send_request as well? My clang-tidy's performance-unnecessary-value-param keeps complaining about the current by-value callback, which gets a bit annoying :(

    Looking at the implementation here:

    template<
    typename CallbackT,
    typename std::enable_if<
    rclcpp::function_traits::same_arguments<
    CallbackT,
    CallbackType
    >::value
    >::type * = nullptr
    >
    SharedFutureAndRequestId
    async_send_request(const SharedRequest & request, CallbackT && cb)
    {
    Promise promise;
    auto shared_future = promise.get_future().share();
    auto req_id = async_send_request_impl(
    *request,
    std::make_tuple(
    CallbackType{std::forward<CallbackT>(cb)},
    shared_future,
    std::move(promise)));
    return SharedFutureAndRequestId{std::move(shared_future), req_id};

    I think supporting const SharedFuture & should be safe.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions