* -ffunction-sections -Wl,--gc-sections trick and class members
@ 2007-04-19 14:38 Shriramana Sharma
2007-04-19 15:13 ` Glynn Clements
0 siblings, 1 reply; 4+ messages in thread
From: Shriramana Sharma @ 2007-04-19 14:38 UTC (permalink / raw)
To: linux-c-programming
I learnt from the good people on this list that usage of:
-fdata-sections -ffunction-sections -Wl,--gc-sections
will ensure that only those sections of object modules get linked to the
target which are actually used in the target.
Recently I was musing on whether I can use this when I recompile the Qt
4 libraries for static linking. But the thing is that the Qt libraries
are already very optimized in this aspect -- in that there is a separate
source file per class and consequently a separate object file per class.
However I was wondering whether even then I could do my
hyper-optimization because I will be using only a small number of
functions in each class. It is highly unlikely that I (or anyone else)
is going to use all member functions of a class.
Then I thought how I could possibly prevent linking a function which was
part of a class definition. To my understanding at runtime whenever a
class is loaded into memory (I'm not talking about the individual
instances) its members including functions are all loaded into memory.
If a particular function is not present for loading, wouldn't that be a
fatal error?
So the long and short of it is, are these options useful to filter out
unused class members or not?
Thanks as always,
Shriramana Sharma.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: -ffunction-sections -Wl,--gc-sections trick and class members
2007-04-19 14:38 -ffunction-sections -Wl,--gc-sections trick and class members Shriramana Sharma
@ 2007-04-19 15:13 ` Glynn Clements
2007-04-26 17:30 ` Shriramana Sharma
0 siblings, 1 reply; 4+ messages in thread
From: Glynn Clements @ 2007-04-19 15:13 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: linux-c-programming
Shriramana Sharma wrote:
> I learnt from the good people on this list that usage of:
>
> -fdata-sections -ffunction-sections -Wl,--gc-sections
>
> will ensure that only those sections of object modules get linked to the
> target which are actually used in the target.
>
> Recently I was musing on whether I can use this when I recompile the Qt
> 4 libraries for static linking. But the thing is that the Qt libraries
> are already very optimized in this aspect -- in that there is a separate
> source file per class and consequently a separate object file per class.
>
> However I was wondering whether even then I could do my
> hyper-optimization because I will be using only a small number of
> functions in each class. It is highly unlikely that I (or anyone else)
> is going to use all member functions of a class.
>
> Then I thought how I could possibly prevent linking a function which was
> part of a class definition. To my understanding at runtime whenever a
> class is loaded into memory (I'm not talking about the individual
> instances) its members including functions are all loaded into memory.
> If a particular function is not present for loading, wouldn't that be a
> fatal error?
>
> So the long and short of it is, are these options useful to filter out
> unused class members or not?
Possibly. C++ methods aren't really any different from C functions in
terms of object code, so it's conceivable that they could be omitted
if the aren't used.
OTOH, I suspect that virtual methods are always "used" when
intialising the virtual method table, so it's less likely that they
could be omitted.
If you want a definite answer, test it.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: -ffunction-sections -Wl,--gc-sections trick and class members
2007-04-19 15:13 ` Glynn Clements
@ 2007-04-26 17:30 ` Shriramana Sharma
2007-05-04 13:31 ` Glynn Clements
0 siblings, 1 reply; 4+ messages in thread
From: Shriramana Sharma @ 2007-04-26 17:30 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 594 bytes --]
Glynn Clements wrote:
> OTOH, I suspect that virtual methods are always "used" when
> intialising the virtual method table, so it's less likely that they
> could be omitted.
>
> If you want a definite answer, test it.
Well I tested it and it is obvious that the trick is able to weed out
unused class members too -- run the contents of the attachment.
But I did not comprehend how you suspect that having virtual function (I
presume methods are the same as functions) will create different
results. Can you make an example based on the example I attach here?
Thanks.
Shriramana Sharma.
[-- Attachment #2: class-member-function-sections.tar.gz --]
[-- Type: application/gzip, Size: 530 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: -ffunction-sections -Wl,--gc-sections trick and class members
2007-04-26 17:30 ` Shriramana Sharma
@ 2007-05-04 13:31 ` Glynn Clements
0 siblings, 0 replies; 4+ messages in thread
From: Glynn Clements @ 2007-05-04 13:31 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: linux-c-programming
Shriramana Sharma wrote:
> > OTOH, I suspect that virtual methods are always "used" when
> > intialising the virtual method table, so it's less likely that they
> > could be omitted.
> >
> > If you want a definite answer, test it.
>
> Well I tested it and it is obvious that the trick is able to weed out
> unused class members too -- run the contents of the attachment.
>
> But I did not comprehend how you suspect that having virtual function (I
> presume methods are the same as functions) will create different
> results.
Non-virtual methods are referenced directly from the object file in
which they are used. If you don't call them, they won't be referenced.
Virtual methods are called through a function pointer in the virtual
method table. The only direct reference to the function will be in the
code which initialised the table. But that initialisation will occur
regardless of whether or not anything calls the method.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2007-05-04 13:31 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-04-19 14:38 -ffunction-sections -Wl,--gc-sections trick and class members Shriramana Sharma
2007-04-19 15:13 ` Glynn Clements
2007-04-26 17:30 ` Shriramana Sharma
2007-05-04 13:31 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).