2

I have a super high-performance C/C++ data structure (see here!) that I'd like to access and use in my C# program.

Imagine the C/C++ data structure has a public API (get, add, delete, etc).
How can I call these methods lots of times within C# in a high-performance way?

P.S. Before you criticize my use of the phrase "C/C++"...
In my view, C/C++ is distinct from both the C and C++ programming languages. I don't know C++, but rather an extension to C that uses some C++ constructs and can be compiled with a C++ compiler!

Community
  • 1
  • 1
Haywood Jablomey
  • 1,011
  • 1
  • 8
  • 4
  • 4
    I doubt you will achieve anything faster than what is included in the framework. You will encounter a huge performance hit when you are marshaling between managed and unmanaged code and values. – Daniel A. White Jul 21 '10 at 15:27
  • `@Daniel A. White:` Yeah, that's what I'm worried about, especially since the map methods get called _lots_ of times. Is that performance hit inevitable? What if I use the `unsafe` keyword in C#? – Haywood Jablomey Jul 21 '10 at 15:29
  • Daniel is correct. You'd do better to just port the collection to C#. Your C# implementation *could* use `unsafe` but almost certainly does not need to. – Steven Sudit Jul 21 '10 at 15:37
  • Yeah, I think I'm gonna push a bit more code to the C side and minimize the calls that the C# side has to make. – Haywood Jablomey Jul 21 '10 at 15:40

2 Answers2

5

If performance is important to you, then you should avoid crossing the managed/unmanaged boundary "lots of times". Both C# and C++ can be high performance languages but the interop perf costs are not pleasant.

I suggest you write a C library (which could be implemented with C++ constructs as long as the methods are extern C) and call it from the C# code -once!- using P/Invoke. This library can party on your high performance data structure and return some useful information to the C# side.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
  • 1
    That's an interesting idea. I can push a bit more of the C# code into the C part and minimize the calls that I make between them. Thanks! – Haywood Jablomey Jul 21 '10 at 15:34
0

You can use a pipe.

Behrooz
  • 1,696
  • 2
  • 32
  • 54