ros2cs icon indicating copy to clipboard operation
ros2cs copied to clipboard

Possible memory allocation optimization at Utils.cs.

Open guts117 opened this issue 6 months ago • 0 comments

Thank you for this great project.

I see that there is an error string that is being created even when we have RCL_RET_OK at:

https://github.com/RobotecAI/ros2cs/blob/6a4203124f5cc07ecaba5f50185bcc5144118f64/src/ros2cs/ros2cs_core/utils/Utils.cs#L48

A small change in a fork of the repo helped me get 0 allocations in ROS2ForUnity dependent code. It could go upto 0.9 KBs per class per frame which was very high. (Unity Profiler)

The change looks like this:

    internal static string GetRclErrorString(int ret)
    {
      ...
      string errorString = ret > 0 ? PtrToString(errorStringPtr) : String.Empty;
      ...
    }

I haven't looked much inside the codebase but I assume there is some sort of error message stack in the C side that gets converted to C# string. If I am correct the message needs to be popped otherwise it won't be coherent. So, this change shouldn't cause any issues? 🤔

Am I correct in my assessment?

guts117 avatar Jan 27 '24 11:01 guts117