So I'm a C++ developer who has been playing with C# for a week or two in order to get acquainted with Unity, and I'm curious about the marshalling process used to transfer data for use in C++ native plugins. From what I can tell, the general consensus is that while using effective C++ plugins can be a performance increase for computation-heavy tasks, the marshalling process of transferring data can be significantly slower than the potential gains provided by the C++ flexibility.

I'm familiar with how shared memory works in C++ and it appears C# has the functionality required to use the buffers as well. From what I understand, the process of marshalling is slow due to copying, creating buffers and transferring chunks from one memory segment into another. Even though there's the problem of cache coherency when operating in shared memory, it may be less than the cost incurred by marshalling. I'm sure I'll have to do some experimenting.

Does anyone have thoughts or experience with using shared memory or marshalling in Unity who could weigh in on the question?

  • \$\begingroup\$ Not an answer, but you might want to know that native plugins have become rather unusual in Unity. Performance improvements and feature additions made most use-cases for native plugins obsolete. \$\endgroup\$
    – Philipp
    Oct 9, 2019 at 7:30
  • 1
    \$\begingroup\$ Have you looked into the NativeArray and similar features introduced to help Unity C# scripts exchange data with Burst-compiled jobs running natively? These may be worth evaluating to see if they give any wins over marshalling for your plugin's use case. \$\endgroup\$
    – DMGregory
    Oct 9, 2019 at 9:48
  • \$\begingroup\$ Ah that’s neat @DMGregory thanks for the reference! I haven’t seen that, looks like that might be the winner. I think I’ll try writing the procedure on both ends and compare the performance. \$\endgroup\$
    – Jon
    Oct 9, 2019 at 11:56
  • \$\begingroup\$ @Philipp Can you elaborate on that a bit or maybe pass along some links? I'd hate to move too far in one direction fruitlessly lol \$\endgroup\$
    – Jon
    Oct 9, 2019 at 19:33
  • 1
    \$\begingroup\$ @Jon Maybe we can bounce the question back to you: what is the purpose or problem that's leading you to reach for native plugins as the means or solution? Once we understand what you're trying to do, or what issue you've had with your attempts so far that you're trying to overcome, we can better advise on appropriate ways to do so. \$\endgroup\$
    – DMGregory
    Oct 9, 2019 at 20:57


You must log in to answer this question.

Browse other questions tagged .