News for hackers. Stuff that matters.
Re:Comparing C and C++ usage and performance with a real wor... by khitchdee on Sunday September 03, 2017 @12:04PM (#15162142)
If you ignore the protection mechanisms and the class heirarchy built into C++, then a C++ class is like a C struct that can contain function pointers. For many programs this is all that's needed, and the amount of overhead involved in using such an approach to creating objects is obviously lower. So there's no question C will always be faster. It's only when you need protection and class heirarchies that C++ benefits you. That benefit is mainly one of better code organisation.
Re:Comparing C and C++ usage and performance with a real wor... by nly on Sunday September 03, 2017 @01:48PM (#15162743)
> a C++ class is like a C struct that can contain function pointers.

No, it's not. Calling an ordinary class member function in C++ has exactly the same overhead as calling a function in C. Even virtual functions in C++ are not the same as putting function pointers in a C struct (they live in a separate data structure called the vtable).

> the protection mechanisms and the class heirarchy

All C++ protection mechanisms occur at compile time and have no runtime overhead. Non-virtual inheritance hierarchies have the same overhead as C struct composition (because under the covers the memory layout is the same).

Re:Comparing C and C++ usage and performance with a real wor... by cperciva on Sunday September 03, 2017 @02:03PM (#15162839)
Calling an ordinary class member function in C++ has exactly the same overhead as calling a function in C.

Modern compilers make the same optimization for function pointers which are only ever set to one value, too.

Re:Comparing C and C++ usage and performance with a real wor... by nly on Sunday September 03, 2017 @02:30PM (#15163004)
Hi Colin. This isn't an optimisation though, it's a guarantee. Member function calls are resolved statically at compile-time. Replacing indirect calls off a function pointer with direct calls (devirtualization) is an optimisation that applies to both languages equally and requires whole-program / link-time optimization.
Re:Comparing C and C++ usage and performance with a real wor... by khitchdee on Sunday September 03, 2017 @03:31PM (#15163350)
Doesn't a vtable imply an extra level of indirection? You have to find where the vtable is in the object, then the function within the vtable, right? Is that not slower?
Re:Comparing C and C++ usage and performance with a real wor... by nly on Sunday September 03, 2017 @07:02PM (#15164475)
In common implementations the vtable pointer is always the first word inside the object. Given an arbitrary pointer to an object, the offset of this vtable pointer relative to what you're pointing to is always computed statically at compile time. Unless you're using multiple inheritance, this offset is usually zero because derived object pointers in a single-inheritance hierarchy actually always point to their base.

If you're using multiple inheritance then an object can have multiple vtable pointers, but again which one you need to use is known at compile-time based on which class the virtual function you're calling is declared within, and the type of pointer you have.

Once you have the vtable you then have to locate the function pointer for the function you're calling. Again, this is usually a compile-time constant offset from the start of the vtable. This ceases to be true when you have 'virtual inheritance' (not to be confused with virtual functions), when another indirection to find this function pointer is required.

Here are some examples:

https://godbolt.org/g/N2XcV7

You'll notice that the get_square() function, which returns a member function pointer to the virtual square function, doesn't even return any memory addresses, just metadata and an offset

Re:Comparing C and C++ usage and performance with a real wor... by mcguire on Sunday September 03, 2017 @04:18PM (#15163628)
Yes, although (cache behavior notwithstanding) it's a single pointer indirection that can frequently be optimized away by the compiler.
Re:Comparing C and C++ usage and performance with a real wor... by khitchdee on Sunday September 03, 2017 @08:21PM (#15164761)
My point is simply this -- adding protection mechanisms and inheritance to classes neccesitates adding more complexity to the structure used to represent them (such as a vtable) which does add performance overheads. If you dont need those features, you can go leaner and faster with a C structure that includes function pointers to give you the basic packaging of data and functions that an object has.
Re:Comparing C and C++ usage and performance with a real wor... by andrepd on Sunday September 03, 2017 @02:03PM (#15162836)
That is absolutely false. C++ member functions are zero-cost abstractions, i.e. they have the same cost as any other function call. Member functions are _not_ function pointers that reside inside the struct. They don't take up space, they don't need dereferencing to call. They are just "syntatic sugar" to group functions more logically.
Re:Comparing C and C++ usage and performance with a real wor... by inetknght on Sunday September 03, 2017 @02:36PM (#15163033)
> Member functions are _not_ function pointers that reside inside the struct.

...as long as they're not virtual functions, this is correct. Add a virtual table and this is less correct (but the optimizer may still make it correct if it can prove the types match).

Re:Comparing C and C++ usage and performance with a real wor... by andrepd on Sunday September 03, 2017 @02:42PM (#15163066)
Yes, that is correct, but then again virtual functions give you new functionality, namely dynamic dispatching. Stick with static inheritance and you won't have this overhead.
Re:Comparing C and C++ usage and performance with a real wor... by khitchdee on Sunday September 03, 2017 @02:41PM (#15163058)
Please check your facts. C++ member functions are not as simple as function pointers inside structs.
Re:Comparing C and C++ usage and performance with a real wor... by Narishma on Monday September 04, 2017 @03:31AM (#15166404)
You're right, they are simpler in fact. They have the same cost as free-standing functions.
Re:Comparing C and C++ usage and performance with a real wor... by khitchdee on Monday September 04, 2017 @03:38AM (#15166426)
Right. As is speech -- free.
Re:Comparing C and C++ usage and performance with a real wor... by jcelerier on Sunday September 03, 2017 @01:54PM (#15162786)
> So there's no question C will always be faster

Except that the poster above you shown that the opposite was true. The exact same program was written multiple times with C structs and with C++ classes and,

"Except for the non-inline unbundled monostate in C++, every non-inline C++ implementation outperformed every non-inline C implementation."

Re:Comparing C and C++ usage and performance with a real wor... by ric129 on Sunday September 03, 2017 @03:09PM (#15163234)
Only virtual functions behave as you described, and even them don't exactly cause a function pointer to be stored in an object.
Re:Comparing C and C++ usage and performance with a real wor... by humanrebar on Sunday September 03, 2017 @12:38PM (#15162348)
> It's only when you need protection and class heirarchies that C++ benefits you.

Well, need is a strong word. You might not need better correctness while still coming out way ahead by using C++.

Re:Comparing C and C++ usage and performance with a real wor... by khitchdee on Sunday September 03, 2017 @12:47PM (#15162422)
Protection implies a much more complex structure to represent an object and class heirarchies and inheritance imply the need for a runtime. Both these overheads come at a cost. It's the nature of the program you are wriitng that determines whether you will come out ahead. If you were writing a codec, say, you would not use C++.
Re:Comparing C and C++ usage and performance with a real wor... by humanrebar on Sunday September 03, 2017 @01:57PM (#15162804)
Not necessarily. A trivial scope guard initialized with a lambda has basically no overhead versus the equivalent be-really-careful approach in C.
Re:Comparing C and C++ usage and performance with a real wor... by khitchdee on Sunday September 03, 2017 @02:54PM (#15163131)
That would be cruelty to the cat dancing on the hot tin roof.
Re:Comparing C and C++ usage and performance with a real wor... by Joky on Sunday September 03, 2017 @02:52PM (#15163120)
Can you clarify what runtime needs would a class hierarchy have in C++ that a correctly structured C program wouldn't have?
Re:Comparing C and C++ usage and performance with a real wor... by khitchdee on Sunday September 03, 2017 @02:57PM (#15163149)
Well, if you override the implementation of a function in a subclass, the runtime has to determine that and load it in at runtime, when you instantiate an object of the subclass. In C there is no runtime.
Re:Comparing C and C++ usage and performance with a real wor... by psyc on Sunday September 03, 2017 @07:36PM (#15164604)
This is done at compile time. The call is indirect, which only means the call destination is decoupled from the generated calling code. This does not entail the runtime loading anything.

http://www.geeksforgeeks.org/virtual-functions-and-runtime-p...

Re:Comparing C and C++ usage and performance with a real wor... by khitchdee on Sunday September 03, 2017 @08:25PM (#15164776)
OK. Wouldn't that indirection add an extra instruction, each time you called a function?
Re:Comparing C and C++ usage and performance with a real wor... by KayEss on Monday September 04, 2017 @03:51AM (#15166477)
Yes, but if your design requires the virtual function then you'll be using a function pointer in the C implementation as well, which has the same indirection.
Re:Comparing C and C++ usage and performance with a real wor... by khitchdee on Monday September 04, 2017 @04:35AM (#15166659)
If your design requires inheritance or virtual functions, C++ is the right choice. But in many cases, when it doesn't, if you still use C++, you'll pay the price of an extra indirection and a much larger memory structure to hold your objects. If your function were processing the inner loop of a video codec, that would unneccesarily slow you down
Re:Comparing C and C++ usage and performance with a real wor... by bstamour on Monday September 04, 2017 @11:30AM (#15168883)
Unless you explicitly type `virtual`, your C++ classes will have the exact same overhead as C structs. Even with inheritance. The memory layout of

    struct A { int x; };
    struct B : A { int y; };
is the same as if you had written

    struct B { int x; int y; };
Public/private/protected inheritance and access control do not add overhead. It's literally only if you opt in by typing `virtual` do you get class hierarchy overhead.
Re:Comparing C and C++ usage and performance with a real wor... by khitchdee on Monday September 04, 2017 @12:21PM (#15169186)
Public/private/protected inheritance and access control do not add overhead

That's interesting. So does the compiler just put the functions in different parts of the vtable to remember the access control rules. There's no such thing as a free lunch and you're adding information here -- has to be stored somewhere.

Re:Comparing C and C++ usage and performance with a real wor... by bstamour on Monday September 04, 2017 @04:43PM (#15170745)
Access control rules are all checked at compile time. There's literally nothing to store. If you want proof, check the output of your compiler. The only thing a non-virtual struct/class might do is reorder member variables if they're of different access controls, but if you're just using a C-style struct but with private member variables and public non-virtual member functions, it has literally the same memory layout as it would in C.
Re:Comparing C and C++ usage and performance with a real wor... by khitchdee on Monday September 04, 2017 @05:25PM (#15170947)
OK. That makes sense. So it's only a compiler overhead
Re:Comparing C and C++ usage and performance with a real wor... by psyc on Monday September 04, 2017 @10:58AM (#15168643)
You only pay for the indirection for virtual functions. You don't pay it simply for choosing C++. There is no "much larger memory structure" either. The vtable is per class. The per object cost is one pointer. C++ compilers are pretty smart.