Why are interop calls (P/Invoke and COM interop) slower compared to a managed instance call?
P/Invoke (Platform Invoke) and COM interop calls in .NET Compact Framework are significantly (~5-6 times) slower than regular managed calls. Although the overall performance penalty largely depends on types marshaled between managed and native code (marshalling overhead), there is also a common overhead, primarily due to some internal work preceding and following every platform call. This work is needed to notify the runtime that the call must be GC (Garbage Collector) preemptable to avoid the GC from being blocked until the interop call is completed. This is why it’s important to maximize the amount of work performed inside each interop call and avoid multiple frequent invocations.
Related Questions
- How much slower will each instance of the Distributed Folding client in a dual-CPU computer run than a single instance of the Distributed Folding client on a single processor?
- How will stormwater in the area be managed, particularly in the instance of heavy rainfall and king tides?
- Why are interop calls (P/Invoke and COM interop) slower compared to a managed instance call?