Overhead for inline interaction using only primitive types

I am considering porting a small portion of the code in my C # project to C / ASM to improve performance. (This section of code uses a lot of bitwise operations and is one of the few places where there can be a real performance increase using native code.) Then I plan to just call the native function in a separate DLL via P / Invoke. Now the only data that will be transferred between managed and native code will be purely primitive types (bool, int, long, 1D array, etc.). So my question is: will there be significant overhead using P / invoke simply with primitive types? I know that when using more complex types, additional costs arise because they must be distributed (copied / copied), but maybe in my situation they will be relatively efficient (compared to calling the code from the most internal DLL)? If someone could clarify this question for me, explaining the degree of performance benefits / hits and the reasons underlying them, it would be very appreciated. An alternative way to accomplish the whole task would also be welcome, although since C # does not support native assembly / CIL support, I do not believe that it exists.

+4
source share
5 answers

From MSDN ( http://msdn.microsoft.com/en-us/library/aa712982.aspx ):

β€œPInvoke has an overhead of 10 to 30 x86 instructions for each call. In addition to this fixed cost, marshaling creates additional overhead. There are no marshaling costs between blittable types that have the same representation in managed and unmanaged code. For example, there is no cost to translate between int and int32. "

So, this is reasonably cheap, but as always, you have to measure it carefully to make sure you are benefiting from it, and be aware of any maintenance costs. As an aside, I would recommend the C ++ / CLI ("managed" C ++) over P / Invoke for any complex interaction, especially if you like C ++.

+5
source

I seem to recall that for every P / Invoke call, there are at least 30 machine operations. But ignore the theory, project your parameters and choose the fastest.

+1
source

I personally installed a test harness with a simple expression written in C # and unmanaged C ++, and then profile the application to see which delta you're working with.

Something else to consider is that you will introduce a maintenance problem using the application, especially if you have junior level developers who are expected to support the code. Make sure that you know what you get and what you lose depending on performance, as well as on the clarity of the code and maintainability.

As an aside, JIT'd C # code should have performance at the C ++ level with respect to arithmetic operations.

+1
source

You can generate a compiled optimized version of your .NET assembly using ngen on the end-user computer (as part of the installation process).

In my experience, properly formatted C # (e.g. keep selection outside of loops) will work very well.

0
source

This link contains some information: http://www.codeproject.com/Articles/253444/PInvoke-Performance

Also note the performance difference when the [SuppressUnmanagedCodeSecurity] attribute is applied.

0
source

All Articles