1

I am calling a two C++ function calls from C# my code is below.

[DllImport("A.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "FuncA"), System.Security.SuppressUnmanagedCodeSecurity]
    public static extern void FuncA(UInt64 functionID);

In C++ code is :

EXTERN_C void STDMETHODCALLTYPE FuncA(UINT_PTR functionId)
{
    return;
}

This function is being called from C# to C++ for about 2 million times. Without this function call my web request is getting completed in 5.9 seconds.. And with this function call i am getting 7.1 seconds..

Approx 15% overhead. Already i have used "SuppressUnmanagedCodeSecurity" by seeing a post, which reduced the overhead from 30% to 15 % .. But is there any other way to reducing this 15% overhead.??

Update1:

The function ID needs to be sent to C++ for each and every function calls of C#. The C++ function is not an empty function. It needs to store the function IDs in a STL and another thread will process it. I am doing a .NET profiler sort of thing. I need to profile each and every function calls . This FuncA C++ function will get called from injected helper functions.

Thanks,

./Rahul

valiano
  • 16,433
  • 7
  • 64
  • 79
Sel_va
  • 588
  • 5
  • 25
  • 3
    Make 1 call to a C++ method that does this call 2M times? – H H Jun 09 '15 at 07:31
  • @rahul Have you ever thought that simply calling an empty method 2 million of times could really take 1.2 seconds? How much time would take to call an equivalent empty C# method from another assembly? – xanatos Jun 09 '15 at 07:36

2 Answers2

1

As a longshot you can create a managed C++ CLI static library which calls the unmanaged C++ functions with lower performance decrease and add as reference to the C++/CLI library in your C# project. The C# application can then make managed method calls to the C++ CLI library linked, which in turn can make unmanaged method calls. Although this results in some indirection, it may provide some performance increase.

fatihk
  • 7,789
  • 1
  • 26
  • 48
  • +1 for the idea, but I doubt it will become faster with an extra stack frame in between. Could you elaborate on why a managed c++ call has less performance decrease? maybe by compare the resulting IL side-by-side? Since the method is void and is passing a 32bit value (stack) type, I figured the original code would be as fast as it can get. There's also more hassle with static libs ([modules](https://msdn.microsoft.com/en-us/library/1s46f83c%28v=vs.120%29.aspx)), Visual Studio has never supported them, so manually calling the command-line compiler (csc.exe) would be needful. Still worth trying. – Louis Somers Jun 10 '15 at 23:56
0

What does the C++ function do? Is it really an empty method? Is it a necessary step in your chain of actions to perform?

Perhaps it is faster to rewrite this method to C#, to incorporate it in the workflow there, without having to invoke external (probably unsafe) methods.

If the action being performed by the C++ code has nothing to do with the rest of your flow, perhaps you might win some time by having it run in a background worker, or a separate thread, so your C# code doesn't have to wait for the code to run. I suppose this might be a valid option, since your method returns void.

Perhaps a little more information about why and how might help us to find a better suited answer.

Melvin
  • 344
  • 1
  • 8