Foreign predicates in C++ vs. C, performance overhead

Hello,

I am curious. Is there a (significant) performance overhead when using C++ over C – given that C++ uses subclassing and overloading of operators and perhaps other stuff as well.

If one wants a very performant and lean system, should one specifically choose C over C++?

thanks,

Dan

IMO, C++ is as efficient as C when used properly, but it is way easier to get it slow as lots of stuff happen under the hood that one needs to be aware of when considering efficiency. The SWI-Prolog C++ interface makes life easier, but creates and discards typically more term references than necessary. A C programmer has to deal with them explicitly and may opt for reuse (with its dangers). The C++ interface deals automatically with most error-paths to nicely reclaim resources. Using C hand coded solutions to this is often tedious, but when done properly it might be more efficient (also if you know that certain error paths are simply impossible).

For most code, I guess C++ is a better choice. For Prolog itself I’m not so sure. Most of Prolog’s data are anonymous pointers or integers that need some math to get at the place you need to be. C++ strong typing is probably mostly just a burden.

1 Like

Hi grossdan!

I think, C++ is more efficient than C, when used properly. There is no significant performance overhead when using C++ over C. I’ve read somewhere that calling of methods is about 10% slower than calling a [C] function directly. And all overloading is resolved at compile time, no slowdown.

On the other hand, there are inline functions, which C doesn’t provide. Templates can also be faster then non-template code. For instance, there is the Standard Template Library, which provides fast, high quality container classes (and more). It’s much easier and faster to use one, instead of writing containers by hand.

For more info, I recommend the Stroustrup (“The C++ programming language”).

Cheers,
Volker

Hi Volker,

Many thanks for the response.

My main concern is whether the API implementation in C++ between swi prolog and an external predicate carries an overhead.

One of the reasons I want to develop a set of external predicates is because Prolog is too slow for the task at hand. So, i don’t want to end up with an slow down due to the way the C++ API is implemented, which could be felt when there are hundreds of thousands or perhaps millions of calls.

Dan

I’m not completely sure, but I think there is no C++ API implementation. SWI-Prolog’s foreign language interface is just C. (Of course you can always use the F.L.I. in C++).

If you mean the layer which the SWI-Prolog/C++ Interface builds on top of that, then I don’t know. See Jan’s answer to your original question, as he answers that.

If he doesn’t make it clear to you, politely ask him again. :slight_smile:

Cheers,
Volker

I’m not sure about the C standard, but all compilers I use (gcc, clang, msvc) have inline functions. Modern compilers inline static functions from the same file automatically under some heuristics. Many C++ extensions that are also meaningful for C have been integrated in modern C compilers. I think the main one missing is standardized support for atomic operations, although all modern C compilers provide that as well.

There is SWI-cpp.h, a header only C++ interface. It mainly deals with type conversion to/from Prolog term handles, registration of atoms, functors and predicates and forwarding exceptions. The overhead is minimal, but as I said there are cases where the C programmer who has to do all the hard work explicitly can cleverly combine stuff (and mess up error handling :frowning: ) Error scenarios go through C++ exceptions that are mapped to Prolog exceptions. Never tested how efficient that is, but errors should be rare.

1 Like

Okay.

Sounds (mostly) good. So this is what I’d recommend Daniel to do.

Cheers,
Volker