What can I do to improve the performance of interop calls (P/Invoke and COM interop)?
As described above, the overall cost of an interop call is largely dependent upon the number and type of the parameters that must be marshaled between managed and native code. The following points provide some guidance on how to make your interop calls more efficient. General Guidelines Use blittable types where possible. Blittable types have a common representation in both managed and unmanaged memory so no copying or other transformations are necessary. Examples of blittable types include ints, bytes, booleans, characters, and strings. Arrays of blittable types are also blittable. Call granularity is important. Because there is a fixed cost associated with each interop call, it’s much more efficient to make a few “chunky” calls rather than several “chatty” calls. Guidelines for PInvoke(calling from managed code to native code) There is a ‘fast track’ for PInvoke calls that have only simple parameters, such as Int or IntPtr arguments. In addition to basic blittable types, there are ma