[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: c++ question: code duplication across classes
From:       Thiago Macieira <thiago () kde ! org>
Date:       2006-09-20 8:06:51
Message-ID: 200609201006.51432.thiago () kde ! org
[Download RAW message or body]

[Attachment #2 (multipart/signed)]


Benoît Jacob wrote:
>is the overhead for virtual methods a speed overhead ? 

The overhead of a virtual method is an indirect jump (instead of a direct 
one). This used to be a problem because it would throw off all prediction 
done in modern CPUs, because they didn't predict indirect branches. I 
don't know if that's still true.

So, if you can, avoid virtuals if speed is an issue.

>or only some 
> memory taken per instantiation? Also : the vtable doesn't inflate the
> sizeof of the class, right ? (well i guess the latter question is
> stupid but I'd rather be sure :) )

For a simple class,
sizeof(vtable) = k + sizeof (void (*)()) * N

where k is a constant and N is the number of virtual functions. Generally, 
k isn't larger than 8-16 bytes. This can get a lot more complex if 
secondary virtual tables are necessary (multiple inheritance, virtual 
inheritance), but isn't the case here.

In the case of a template class, you have to remember that the virtual 
table's instantiation causes the instantiation of ALL virtual functions 
in each DSO module that used them.

-- 
  Thiago Macieira  -  thiago (AT) macieira.info - thiago (AT) kde.org
    PGP/GPG: 0x6EF45358; fingerprint:
    E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358

[Attachment #5 (application/pgp-signature)]

>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic