Search code examples
c++cperformanceprogramming-languageswrapper

Performance of C-Wrapper DLL for C++ in custom language


Currently I´m developing my own programming language for learning purpose, since I want to support C++ Code and an ABI for C++ is not supported by all compilers (afaik) the idea was to implement support for the C ABI and create wrapper dlls for C++ in C.

Edit: I´m asking for runtime overhead.

The question is how much overhead will I get if basically everything i do with an C++ object is routed through this wrapper dll? Or are there other solutions which will work for all C++ compiler?

The dlls would use code like in the comments:

MyClass a = new MyClass; // void* a_handle = MyClass_new();
a.Foobar(5);             // MyClass_Foobar(a_handle, 5);

Solution

  • The correct answer to, "Will this perform okay?" is often, "Test it out and measure it."

    In general, though, I'd expect the performance penalty of an extra function call to be negligible. It ought to be less than virtual function dispatch overhead, and less than or equal to the cost of binding an interface in one language to another, and both of these are costs that people regularly incur without worrying much about.

    Short of picking a particular compiler's ABI (Itanium, Visual C++, and GCC are all standard or mostly standard on their respective platforms), I don't know of another way to handle C++ ABI issues. Your approach of using C wrappers is used by other projects as well. MathGL, for example, does this, and it goes one step further, providing C++ headers containing all-inline classes that do nothing but call the C interface, so that they can be used regardless of the C++ ABI in use.