ros2_rust icon indicating copy to clipboard operation
ros2_rust copied to clipboard

Make take_boxed() create messages directly on the heap

Open nnmm opened this issue 2 years ago • 3 comments

Currently, the implementation of take_boxed() will place the message on the stack during intermediate steps, even though the intent of this function is to avoid that, since large messages might overflow the stack.

This requires avoiding any Box::new() calls, see https://github.com/rust-lang/rust/issues/53827

nnmm avatar Oct 08 '22 14:10 nnmm

Hi, I'm considering contribution to this issue. However, I need some hints how to start. AFAIK this and this Box::new functions have to be replaced with another one (which? from_raw?). Also I've found the following comment in take_boxed()

        // TODO: This will still use the stack in general. Change signature of
        // from_rmw_message to allow placing the result in a Box directly.

How should the signature look like? I assume that it should return a pointer, but how to achieve that? It'd be great, if you could explain that to me. Regards

Nizerlak avatar Nov 07 '22 13:11 Nizerlak

Hi! I've wanted to make a PR for this one as well but I've been having some issues with setting up the environment.

As far as I understand from the discussion in https://github.com/rust-lang/rust/issues/53827 the safest way would be to use vec! together with into_boxed_slice since vec! apparently avoids the stack allocation. I'm not sure if this still requires the signature change that is mentioned in the comment.

@Nizerlak will probably have a PR before me anyway :)

asterycs avatar Nov 07 '22 22:11 asterycs

I've actually already looked into this a while back, so I pushed my work-in-progress branch in case one of you wants to take over: https://github.com/ros2-rust/ros2_rust/compare/main...take_boxed

Let me know if that helps. The main problem I was facing was that the msg_idiomatic.rs.em template code becomes very large with this change, perhaps there's a way to avoid it though.

nnmm avatar Nov 12 '22 22:11 nnmm