RTTI overhead

Discussion in 'C++' started by Agoston Bejo, Jan 5, 2005.

  1. Agoston Bejo

    Agoston Bejo Guest

    Hello there,
    I would like to know what overheads there are to think of when using RTTI. I
    understand that enabling RTTI increases the sizes of the classes, but not
    the objects themselves. This doesn't sound too bad.
    What I'm worried about is whether there is a general performance overhead
    caused by enabling RTTI, such as enabling exceptions makes a C++ program
    slower even when there are no exceptions actually used.
    Are there similar considerations in the case of RTTI?

    I am using VC++7.1, but I'm also interested about this in general.

    Thx, Agoston
     
    Agoston Bejo, Jan 5, 2005
    #1
    1. Advertising

  2. Agoston Bejo

    Ron Natalie Guest

    Agoston Bejo wrote:
    > Hello there,
    > I would like to know what overheads there are to think of when using RTTI. I
    > understand that enabling RTTI increases the sizes of the classes, but not
    > the objects themselves. This doesn't sound too bad.


    Polymorphic objects (those with virtual functions) are slightly larger
    typically because they contain a pointer to the class specific information
    (vtable pointer).

    > What I'm worried about is whether there is a general performance overhead
    > caused by enabling RTTI, such as enabling exceptions makes a C++ program
    > slower even when there are no exceptions actually used.
    > Are there similar considerations in the case of RTTI?
    >

    As far as C++ is concerned there is no such thing as turning RTTI off.
    This is a defect in your compiler. And boy if you ever access a function
    requiring RTTI you'll blow up at runtime (believe me I know).

    C++ is very careful NOT to incur performance hits on features you are't
    using, so if you have a non-polymorphic class (no virtual functions), then
    the overhead disappears. Once you have a virtual function, which really
    needs the overhead to work, then all the other RTTI-related features like
    dynamic cast and dynamic typeid will work with that class.
     
    Ron Natalie, Jan 5, 2005
    #2
    1. Advertising

  3. Agoston Bejo wrote:
    > I would like to know what overheads there are to think of when using RTTI. I
    > understand that enabling RTTI increases the sizes of the classes, but not
    > the objects themselves. This doesn't sound too bad.
    > What I'm worried about is whether there is a general performance overhead
    > caused by enabling RTTI, such as enabling exceptions makes a C++ program
    > slower even when there are no exceptions actually used.
    > Are there similar considerations in the case of RTTI?
    >
    > I am using VC++7.1, but I'm also interested about this in general.


    If you can create two programs where one _uses_ RTTI and the other does
    not, and otherwise they are _absolutely_ the same, then you can measure
    the _real_ overhead. However, logically, that's impossible. If your code
    does not use RTTI, why enable RTTI? If after you enable RTTI in your
    program that does not use it you notice that it's slower, the you've
    created something to measure, but what value does that measurement have?

    Also, keep in mind, such overheads cannot be made generic, since they
    depend greatly on the compiler/platform/libraries used. If I somehow
    measure the "overhead" of using RTTI in my program/compiler combination,
    what knowledge can you derive from that for your program/compiler
    combination?

    See my point?

    Victor
     
    Victor Bazarov, Jan 5, 2005
    #3
  4. Agoston Bejo

    Rolf Magnus Guest

    Agoston Bejo wrote:

    > Hello there,
    > I would like to know what overheads there are to think of when using RTTI.
    > I understand that enabling RTTI increases the sizes of the classes, but
    > not the objects themselves. This doesn't sound too bad.
    > What I'm worried about is whether there is a general performance overhead
    > caused by enabling RTTI, such as enabling exceptions makes a C++ program
    > slower even when there are no exceptions actually used.
    > Are there similar considerations in the case of RTTI?


    Basically, it depends on the compiler, but usually no, there is no overhead.
     
    Rolf Magnus, Jan 5, 2005
    #4
  5. Agoston Bejo wrote:

    > Hello there,
    > I would like to know what overheads there are to think of when using RTTI. I
    > understand that enabling RTTI increases the sizes of the classes, but not
    > the objects themselves. This doesn't sound too bad.
    > What I'm worried about is whether there is a general performance overhead
    > caused by enabling RTTI, such as enabling exceptions makes a C++ program
    > slower even when there are no exceptions actually used.
    > Are there similar considerations in the case of RTTI?



    "Enabling" exceptions in nowadays implementations should incur no
    overheads (unless an exception is thrown in which case what should be
    considered as overhead is not clear).


    RTTI means "run-time type identification. That means you have to perform
    run-time checks to find the type of an object. These checks are the
    overhead.


    In C++, there is the rule "what you do not use, you do not pay for". So
    if you do not use RTTI you do not pay any run-time checks cost.


    Thus the phrase "enable" does not make sense.




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Jan 5, 2005
    #5
  6. Agoston Bejo

    Rolf Magnus Guest

    Ioannis Vranos wrote:

    > Agoston Bejo wrote:
    >
    >> Hello there,
    >> I would like to know what overheads there are to think of when using
    >> RTTI. I understand that enabling RTTI increases the sizes of the classes,
    >> but not the objects themselves. This doesn't sound too bad.
    >> What I'm worried about is whether there is a general performance overhead
    >> caused by enabling RTTI, such as enabling exceptions makes a C++ program
    >> slower even when there are no exceptions actually used.
    >> Are there similar considerations in the case of RTTI?

    >
    >
    > "Enabling" exceptions in nowadays implementations should incur no
    > overheads (unless an exception is thrown in which case what should be
    > considered as overhead is not clear).


    Maybe it shouldn't but often, it does incur an overhead in code size, which
    indirectly affects performance through higher cache load.

    > In C++, there is the rule "what you do not use, you do not pay for". So
    > if you do not use RTTI you do not pay any run-time checks cost.
    >
    >
    > Thus the phrase "enable" does not make sense.


    Well, if you disable it, you'll get a compiler error if you try to use it,
    since it's not enabled. At least that's how gcc does it.
     
    Rolf Magnus, Jan 5, 2005
    #6
  7. Agoston Bejo

    Ron Natalie Guest

    Rolf Magnus wrote:

    >
    > Well, if you disable it, you'll get a compiler error if you try to use it,
    > since it's not enabled. At least that's how gcc does it.
    >


    Not with visual studio, you get a run-time exception. Real handy.
     
    Ron Natalie, Jan 5, 2005
    #7
  8. Agoston Bejo

    Rolf Magnus Guest

    Victor Bazarov wrote:

    > If you can create two programs where one _uses_ RTTI and the other does
    > not, and otherwise they are _absolutely_ the same, then you can measure
    > the _real_ overhead. However, logically, that's impossible. If your code
    > does not use RTTI, why enable RTTI?


    It's the other way round. If RTTI imposes no overhead, why bother disabling
    it and risk potential problems with e.g. libraries your program uses?
    If there is a significant difference, it might be worth finding out for each
    target compiler and each used library if you can disable RTTI.
     
    Rolf Magnus, Jan 5, 2005
    #8
  9. Agoston Bejo

    Agoston Bejo Guest

    "Ioannis Vranos" <> az alábbiakat írta a következo
    hírüzenetben: 1104955152.262790@athnrd02...
    > Agoston Bejo wrote:
    >
    > "Enabling" exceptions in nowadays implementations should incur no
    > overheads (unless an exception is thrown in which case what should be
    > considered as overhead is not clear).


    To my best knowledge, this is not true. That is the main point. I read that
    enabling exceptions gets the compiler to generate additional (additional
    compared to exceptional-disabled code) code for implementing unwind
    semantics, which does make the program run some 20-40% slower (I don't
    remember the exact amount) even if not a single throw statement occurs in
    your program. That's why you have the possibility to turn it off, I think.
     
    Agoston Bejo, Jan 6, 2005
    #9
  10. Agoston Bejo

    Mike Hewson Guest

    Agoston Bejo wrote:
    > "Ioannis Vranos" <> az alábbiakat írta a következo
    >>"Enabling" exceptions in nowadays implementations should incur no
    >>overheads (unless an exception is thrown in which case what should be
    >>considered as overhead is not clear).

    >
    >
    > To my best knowledge, this is not true. That is the main point. I read that
    > enabling exceptions gets the compiler to generate additional (additional
    > compared to exceptional-disabled code) code for implementing unwind
    > semantics, which does make the program run some 20-40% slower (I don't
    > remember the exact amount) even if not a single throw statement occurs in
    > your program. That's why you have the possibility to turn it off, I think.


    <musing>

    I thought 'overhead' meant time( cycles ) and/or space ( bytes ). If
    nothing else thermodynamics prevents something for nothing - information
    ( = energy ) included. Any 'throw' has to know where it's caught,
    directly or otherwise, whatever else happens - stack unwinding,
    destructors et al. It's a potential execution path 'to the side' of
    one's code. No doubt compilers ( and their writers ) differ in skill as
    applied to this. After all the Standard only requires 'as if' behaviour,
    not performance per se ( to my knowledge ).

    With a compiler enabled for exceptions, I'd be dissapointed with any
    overhead if I'd never used catch, throw etc. Having said that, I might
    well be implying it sometimes via #includes, libraries ... without
    knowing it.

    <end musing>

    --

    Cheers
    --
    Hewson::Mike
    "This letter is longer than usual because I lack the time to make it
    shorter" - Blaise Pascal
     
    Mike Hewson, Jan 6, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Harrison

    Re: RTTI

    John Harrison, Jul 14, 2003, in forum: C++
    Replies:
    2
    Views:
    453
    John Harrison
    Jul 14, 2003
  2. Alf P. Steinbach

    Re: RTTI

    Alf P. Steinbach, Jul 14, 2003, in forum: C++
    Replies:
    0
    Views:
    468
    Alf P. Steinbach
    Jul 14, 2003
  3. Steven Lien

    About RTTI

    Steven Lien, Aug 19, 2003, in forum: C++
    Replies:
    4
    Views:
    601
    Ivan Vecerina
    Aug 19, 2003
  4. BillyO
    Replies:
    2
    Views:
    490
    dslater
    Sep 30, 2003
  5. tirath
    Replies:
    3
    Views:
    710
    Ivan Vecerina
    Oct 12, 2003
Loading...

Share This Page