[comp.lang.c++] The price of inheritance?

gjb@cs.brown.edu (08/13/89)

Some recent work I've been doing with C++ and the InterViews
user-interface toolkit has made me wonder how expensive inheritance
is. I realize that a virtual function is slower than a non-virtual,
but does the depth of the class hierarchy affect speed?

For example, let's say I create:

class A
{
public:
  virtual void Method();
  void Another_Method();
};

class B : public A
{
public:
  virtual void Method();
};

class C : public B
{
public:
  virtual void Method();
}

I realize that calling A::Method() will be more expensive than
calling A::Another_Method(). But is calling B::Method() slower than
A::Method()? And will calling C::Method() be slower still? If so, how
much slower? Twice as slow as B::Method()? Three times as slow as
A::Method()? Of course, I'm only speaking of the overhead involved in
calling the functions -- not the time complexity of the function itself.

Could someone explain how this works? Does anyone have statistics? I'd
love to know. Thanks in advance.

				-Greg
+----------------------------------------------------+
Greg Brail		    Work:   (401)863-7692
Internet: gjb@cs.brown.edu  BITNET: gjb@browncs.bitnet
UUCP:	..uunet!brunix!gjb  Home:   (401)831-3736

zweig@brutus.cs.uiuc.edu (Johnny Zweig) (09/11/89)

gjb@cs.brown.edu writes:

>Some recent work I've been doing with C++ and the InterViews
>user-interface toolkit has made me wonder how expensive inheritance
>is. I realize that a virtual function is slower than a non-virtual,
>but does the depth of the class hierarchy affect speed?

>For example, let's say I create:

> [a class with a virtual function that gets redefined in a subclass...]

> [Will the subclass's virtual function be more expensive to call?]

No. Virtual function tables are (as far as I know or can imagine)
flattened -- each (subsubsub...)class just has a big list of pointers. In
the example, the virtual function would just be the value of the nth
(probably 0th) pointer in the v-table. Same banana in the subclass, or
any other {sub}*classes.

-Johnny

turk@Apple.COM (Ken "Turk" Turkowski) (10/03/89)

In article <1989Sep11.002541.13902@brutus.cs.uiuc.edu> zweig@brutus.cs.uiuc.edu (Johnny Zweig) writes:
>gjb@cs.brown.edu writes:
>
>>Some recent work I've been doing with C++ and the InterViews
>>user-interface toolkit has made me wonder how expensive inheritance
>>is. I realize that a virtual function is slower than a non-virtual,
>>but does the depth of the class hierarchy affect speed?
>> [Will the subclass's virtual function be more expensive to call?]
>
>No. Virtual function tables are (as far as I know or can imagine)
>flattened -- each (subsubsub...)class just has a big list of pointers. In
>the example, the virtual function would just be the value of the nth
>(probably 0th) pointer in the v-table. Same banana in the subclass, or
>any other {sub}*classes.

Constructors, however, get slower and slower, even when there is no
constructor.  It's amazing how often I get the message:

	MyClass::MyClass() too complex for inlining.

when the class and all of its members are doing nothing during their
construction but returning.  The C code is littered with the address
of every object's constructor loaded into a register.
-- 
Ken Turkowski @ Apple Computer, Inc., Cupertino, CA
Internet: turk@apple.com
Applelink: TURKOWSKI1
UUCP: sun!apple!turk