RobotecAI / ros2cs

A C# (.Net) implementation of ros2 client library (rcl), enabling communication between ros2 ecosystem and C#/.Net applications such as Unity3D
Apache License 2.0
91 stars 22 forks source link

Possible memory allocation optimization at Utils.cs. #62

Open guts117 opened 9 months ago

guts117 commented 9 months ago

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?