eliminate unused virtual functions
Asked Answered
P

2

3

To eliminate unused (ordinary) function I can use: -ffunction-sections, -fdata-section and --gc-sections. and it works.

I know that using polymorphism, function are 'late-binding' so I suppose there is no way to decide which of the function can be remove during linkage process.

But I am using pure virtual function to force class which inherits to implement some function. Then in code I am using objects (not pointer/reference to object, so I am not using polymorphism).

pseudo code:

class BASE {
    ...
    virtual void do_sth() = 0;
    virtual void do_sth_else() = 0;
    ...
};

class C1 : BASE {
    ...
    void do_sth() { //some code }
    void do_sth_else() { //some code }
}

main()
{
    //the do_sth_else function is never used in main
    C1 obj1;
    obj.do_sth();
}

Is there some method to eliminate this unused functions (do_sth_else) during linkage process? Maybe I misunderstood something. and because of that I think there should be a way to remove this unused function. If so please explain me why, when I am NOT using pointers with virtual function there is no way to "get rid" of polymorphic overhead. :)

FYI: This code is mainly for learning purpose.

Papillote answered 2/7, 2013 at 19:6 Comment(3)
If you are not using pointers or references and essentially no dynamic dispatch then why are you using virtual at all? Seems you misunderstand virtual. When to mark a function in C++ as a virtual? should be a good read.Clair
I agree with you about 'dummy' use of polymorphic. I understand polymorphic, but I wanted to force class that inherit to implement some of the function. I know that other solution is to use template instead in that kind of case. In other words I rather wanna use virtual function without polymorphic. Why I am using it is not very important I rather want to learn and understand why I am not able to get rid of this function during linkage.Papillote
You seem to have forgotten to give main a return type. GCC should be able to "devirtualize" those function calls, since the dynamic type of the object is known, but without something like -fwhole-program it's unlikely to discard unused virtual functions, because another translation unit might need their definitions.Emporium
P
4

Thanks to Jonathan Wakely I started digging and I found gcc options:

-fvtable-gc Emit special relocations for vtables and virtual function references so that the linker can identify unused virtual functions and zero out vtable slots that refer to them. This is most useful with -ffunction-sections and -Wl,--gc-sections, in order to also discard the functions themselves.

But it is not supported in GCCv4.7.1

Papillote answered 3/7, 2013 at 10:27 Comment(1)
See also replacement for “fvtable-gc” in GCCPalaeozoology
M
-1

For learning purpose I suggest you to learn the semantics of language elements and learn to use them for their purpose. I.e. use virtuals there yo want polymorphism and leave them alone otherwise.

Worrying about things like the amount of dead code left in by the linker can be safely left to 5-10 years ahead or forever.

And optimization improves every year, so even if today yo could find 0.01% of the image as possible waste by the time you get to production it may be gone just by itself.

Monograph answered 2/7, 2013 at 19:47 Comment(1)
I know that optimization improves often than every year and other compilers than gcc handle some cases in deferent way... I am usually working with uC and C code. I am doing some experiment with C++ and trying to focus on C++ overhead in some special conditions like this one. But thanks for your answerPapillote

© 2022 - 2024 — McMap. All rights reserved.