Access to virtual functions much slower

Discussion in 'C++' started by parag, Nov 17, 2010.

  1. parag

    parag Guest

    For the a given hierarchy

    class A{
    virtual bool isUselessFunc() = 0;
    };


    class B : public A {
    bool isUselessFunc() {
    return true;
    }
    };

    class C : public B
    {
    bool OtherFunc() {
    return false;
    }
    };


    Today if we have a pointer to a Object of Class C , ( in a pointer of
    A )
    C * c = new C ():
    A* a = c;

    I need to acces c->isUselessFunc() many times in a code , I see that
    in the collect report , it is one of the top most used functions.
    Is there any way we could reduce this footprint, but having all the
    class deriving from Class B have their own non virtual version ???
    parag, Nov 17, 2010
    #1
    1. Advertising

  2. On 11/17/2010 12:41 AM, parag wrote:
    > For the a given hierarchy
    >
    > class A{
    > virtual bool isUselessFunc() = 0;
    > };
    >
    >
    > class B : public A {
    > bool isUselessFunc() {
    > return true;
    > }
    > };
    >
    > class C : public B
    > {
    > bool OtherFunc() {
    > return false;
    > }
    > };
    >
    >
    > Today if we have a pointer to a Object of Class C , ( in a pointer of
    > A )
    > C * c = new C ():
    > A* a = c;
    >
    > I need to acces c->isUselessFunc() many times in a code , I see that
    > in the collect report ,


    What's "the collect report"? I am just not familiar with the term, sorry.

    > it is one of the top most used functions.
    > Is there any way we could reduce this footprint, but having all the
    > class deriving from Class B have their own non virtual version ???


    You see that it's the most used. Do you see that it's the bottleneck,
    or are you just guessing? Are you trying to improve the performance of
    something that matters or something that doesn't really contribute to
    the run time?

    First of all, you need to *measure* performance, not guess.

    If your model requires polymorphic behavior, you're stuck with virtual
    functions, I'm afraid. In all fairness, it's just a couple extra
    lookups in a table, and usually it's quite fast (a few CPU instructions
    if anything).

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 17, 2010
    #2
    1. Advertising

  3. parag wrote:
    > For the a given hierarchy
    >
    > class A{
    > virtual bool isUselessFunc() = 0;
    > };
    >
    >
    > class B : public A {
    > bool isUselessFunc() {
    > return true;
    > }
    > };
    >
    > class C : public B
    > {
    > bool OtherFunc() {
    > return false;
    > }
    > };
    >
    >
    > Today if we have a pointer to a Object of Class C , ( in a pointer of
    > A )
    > C * c = new C ():
    > A* a = c;
    >
    > I need to acces c->isUselessFunc() many times in a code , I see that
    > in the collect report , it is one of the top most used functions.
    > Is there any way we could reduce this footprint, but having all the
    > class deriving from Class B have their own non virtual version ???


    I am not sure to have understood your English, but perhaps what you are
    searching for is to replace the following call:
    c->isUselessFunc()
    with the following one:
    c->B::isUselessFunc()
    The latter call avoids the virtual function call overhead, and it may be
    inlined to avoid the function call at all.

    --
    Carlo Milanesi
    http://carlomilanesi.wordpress.com/
    Carlo Milanesi, Nov 17, 2010
    #3
  4. parag

    Duc Le Guest

    On 11/17/2010 9:21 AM, Carlo Milanesi wrote:
    >
    > I am not sure to have understood your English, but perhaps what you are
    > searching for is to replace the following call:
    > c->isUselessFunc()
    > with the following one:
    > c->B::isUselessFunc()
    > The latter call avoids the virtual function call overhead, and it may be
    > inlined to avoid the function call at all.
    >
    > --
    > Carlo Milanesi
    > http://carlomilanesi.wordpress.com/


    c->B::isUselessfunc() would only work if c is actually derived from B.
    Since c is statically typed to A*, it may or may not have been derived
    from B.

    Duc Le
    Duc Le, Nov 18, 2010
    #4
  5. On 11/18/2010 1:53 PM, Duc Le wrote:
    > On 11/17/2010 9:21 AM, Carlo Milanesi wrote:
    >>
    >> I am not sure to have understood your English, but perhaps what you are
    >> searching for is to replace the following call:
    >> c->isUselessFunc()
    >> with the following one:
    >> c->B::isUselessFunc()
    >> The latter call avoids the virtual function call overhead, and it may be
    >> inlined to avoid the function call at all.
    >>
    >> --
    >> Carlo Milanesi
    >> http://carlomilanesi.wordpress.com/

    >
    > c->B::isUselessfunc() would only work if c is actually derived from B.
    > Since c is statically typed to A*, it may or may not have been derived
    > from B.


    Uh... No, in the original post 'c' is declared as C*.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 18, 2010
    #5
  6. parag

    Duc Le Guest

    On 11/18/2010 12:59 PM, Victor Bazarov wrote:
    > On 11/18/2010 1:53 PM, Duc Le wrote:
    >> On 11/17/2010 9:21 AM, Carlo Milanesi wrote:
    >>>
    >>> I am not sure to have understood your English, but perhaps what you are
    >>> searching for is to replace the following call:
    >>> c->isUselessFunc()
    >>> with the following one:
    >>> c->B::isUselessFunc()
    >>> The latter call avoids the virtual function call overhead, and it may be
    >>> inlined to avoid the function call at all.
    >>>
    >>> --
    >>> Carlo Milanesi
    >>> http://carlomilanesi.wordpress.com/

    >>
    >> c->B::isUselessfunc() would only work if c is actually derived from B.
    >> Since c is statically typed to A*, it may or may not have been derived
    >> from B.

    >
    > Uh... No, in the original post 'c' is declared as C*.
    >
    > V


    My bad, you are right, I thought it was (A *).
    Duc Le, Nov 19, 2010
    #6
    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. =?Utf-8?B?ZGhucml2ZXJzaWRl?=

    out of process session state - how much slower?

    =?Utf-8?B?ZGhucml2ZXJzaWRl?=, Oct 28, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    392
    JIMCO Software
    Oct 29, 2005
  2. Andre Charbonneau

    XPath queries getting slower and slower...

    Andre Charbonneau, Feb 15, 2005, in forum: Java
    Replies:
    0
    Views:
    534
    Andre Charbonneau
    Feb 15, 2005
  3. Mark
    Replies:
    20
    Views:
    1,623
    Dave O'Hearn
    Dec 27, 2004
  4. Emin
    Replies:
    4
    Views:
    398
    Paul McGuire
    Jan 12, 2007
  5. Replies:
    27
    Views:
    521
    Gabriel Genellina
    Jun 14, 2007
Loading...

Share This Page