patterncsharpMinor
Interop: Return Parameter Method as Void
Viewed 0 times
interopmethodreturnvoidparameter
Problem
For interop purpose, this is something that I always do (C#):
where
Now, is this a good practice? Or is it better to do the interop in this way:
I prefer the first one as it is easier to read and understand, but I afraid that it comes with performance penalty when the return object is a large, consuming memory one.
public static extern BigObject InteropWithCPlusPlus();where
BigObject is ( you guess it) a big object, it's not something small like int, or double.Now, is this a good practice? Or is it better to do the interop in this way:
public static extern void InteropWithCPlusPlus(ref BigObject bigObject);I prefer the first one as it is easier to read and understand, but I afraid that it comes with performance penalty when the return object is a large, consuming memory one.
Solution
I don't think there would be too much of a difference. You have to use a reference anyway, because non-native types are stored as references. On x86 processors, returns are placed in register A, which is limited length. So, your C++ program would be either returning a reference to
The area where I can see a performance hit is in memory allocation. If you are making that call, using the object, and then discarding it, it would be faster to create on single instance in your C# code and use that rather than allocating memory every time. You'll get diminishing returns for small numbers of calls, though; unless you have a couple thousand in a given program run, you probably won't notice the hit either way.
BigObject or modifying an existing instance of it.The area where I can see a performance hit is in memory allocation. If you are making that call, using the object, and then discarding it, it would be faster to create on single instance in your C# code and use that rather than allocating memory every time. You'll get diminishing returns for small numbers of calls, though; unless you have a couple thousand in a given program run, you probably won't notice the hit either way.
Context
StackExchange Code Review Q#708, answer score: 4
Revisions (0)
No revisions yet.