public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Do definitions in headers still help optimization?
@ 2024-01-12 14:07 Licht Martin Werner
  2024-01-12 14:49 ` LIU Hao
  0 siblings, 1 reply; 5+ messages in thread
From: Licht Martin Werner @ 2024-01-12 14:07 UTC (permalink / raw)
  To: gcc-help

Dear all,

Does keeping definitions of functions in header files still help the 
compiler optimize your code? Or is that advice outdated?

The answer used to be a certain yes when all optimizations were 
exclusive to the translation unit. By now, link-time optimization has 
been a thing for a while, and one might imagine that defining functions 
in header does not make a difference for optimization anymore.

(Of course, placing functions in header files or asking for link-time 
optimization will affect the compilation speed and memory use)

Discussions of this question seem to be based on hearsay, so I'd rather 
hope for an answer straight from the source. Thanks.

Martin



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Do definitions in headers still help optimization?
  2024-01-12 14:07 Do definitions in headers still help optimization? Licht Martin Werner
@ 2024-01-12 14:49 ` LIU Hao
  2024-01-12 16:01   ` Xi Ruoyao
  0 siblings, 1 reply; 5+ messages in thread
From: LIU Hao @ 2024-01-12 14:49 UTC (permalink / raw)
  To: Licht Martin Werner, gcc-help


[-- Attachment #1.1: Type: text/plain, Size: 878 bytes --]

在 2024/1/12 22:07, Licht Martin Werner via Gcc-help 写道:
> The answer used to be a certain yes when all optimizations were exclusive to the translation unit. 
> By now, link-time optimization has been a thing for a while, and one might imagine that defining 
> functions in header does not make a difference for optimization anymore.
> 
> (Of course, placing functions in header files or asking for link-time optimization will affect the 
> compilation speed and memory use)
> 
> Discussions of this question seem to be based on hearsay, so I'd rather hope for an answer straight 
> from the source. Thanks.

I'm still having a project which usually compiles and links in two minutes, but with LTO would take 
hours to link. I can give a link if you are interested.

I'd like to say that LTO is still not an option for me.


-- 
Best regards,
LIU Hao


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Do definitions in headers still help optimization?
  2024-01-12 14:49 ` LIU Hao
@ 2024-01-12 16:01   ` Xi Ruoyao
  2024-01-12 17:58     ` LIU Hao
  0 siblings, 1 reply; 5+ messages in thread
From: Xi Ruoyao @ 2024-01-12 16:01 UTC (permalink / raw)
  To: LIU Hao, Licht Martin Werner, gcc-help

在 2024-01-12星期五的 22:49 +0800,LIU Hao via Gcc-help写道:
> 在 2024/1/12 22:07, Licht Martin Werner via Gcc-help 写道:
> > The answer used to be a certain yes when all optimizations were exclusive to the translation unit. 
> > By now, link-time optimization has been a thing for a while, and one might imagine that defining 
> > functions in header does not make a difference for optimization anymore.
> > 
> > (Of course, placing functions in header files or asking for link-time optimization will affect the 
> > compilation speed and memory use)
> > 
> > Discussions of this question seem to be based on hearsay, so I'd rather hope for an answer straight 
> > from the source. Thanks.
> 
> I'm still having a project which usually compiles and links in two minutes, but with LTO would take 
> hours to link. I can give a link if you are interested.
> 
> I'd like to say that LTO is still not an option for me.

And LTO cannot cross shared library boundary.  It even cannot cross
static library boundary if the static library is intended to be
distributed in binary form: a static library containing LTO bitcode
will just blow up if attempting to "link" against it using a different
compiler or even a different version of the compiler.  So LTO-enabled
distros build packages containing static libraries with -ffat-lto-
objects and then strip the LTO bitcode away from the static libraries.

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Do definitions in headers still help optimization?
  2024-01-12 16:01   ` Xi Ruoyao
@ 2024-01-12 17:58     ` LIU Hao
  2024-01-14 10:44       ` Xi Ruoyao
  0 siblings, 1 reply; 5+ messages in thread
From: LIU Hao @ 2024-01-12 17:58 UTC (permalink / raw)
  To: Xi Ruoyao, Licht Martin Werner, gcc-help


[-- Attachment #1.1: Type: text/plain, Size: 1071 bytes --]

在 2024/1/13 0:01, Xi Ruoyao 写道:
> And LTO cannot cross shared library boundary.  It even cannot cross
> static library boundary if the static library is intended to be
> distributed in binary form: a static library containing LTO bitcode
> will just blow up if attempting to "link" against it using a different
> compiler or even a different version of the compiler.  So LTO-enabled
> distros build packages containing static libraries with -ffat-lto-
> objects and then strip the LTO bitcode away from the static libraries.

I don't think that's the case for me. I'm building a shared library against only system libraries. 
My observation is basically

1) Link time grows dramatically.
    [less than 2 minutes => more than 1 hour]
2) Binary size also grows dramatically.
    [1.9 MiB => 9.0 MiB]
3) The overall performance improvement is not worth the cost.
    [31382.685 seconds => 29882.761 seconds] (-4.779%)
4) Function calls use much more stack space and are more likely to cause
    stack overflows.


-- 
Best regards,
LIU Hao


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Do definitions in headers still help optimization?
  2024-01-12 17:58     ` LIU Hao
@ 2024-01-14 10:44       ` Xi Ruoyao
  0 siblings, 0 replies; 5+ messages in thread
From: Xi Ruoyao @ 2024-01-14 10:44 UTC (permalink / raw)
  To: LIU Hao, Licht Martin Werner, gcc-help

On Sat, 2024-01-13 at 01:58 +0800, LIU Hao wrote:
> 在 2024/1/13 0:01, Xi Ruoyao 写道:
> > And LTO cannot cross shared library boundary.  It even cannot cross
> > static library boundary if the static library is intended to be
> > distributed in binary form: a static library containing LTO bitcode
> > will just blow up if attempting to "link" against it using a different
> > compiler or even a different version of the compiler.  So LTO-enabled
> > distros build packages containing static libraries with -ffat-lto-
> > objects and then strip the LTO bitcode away from the static libraries.
> 
> I don't think that's the case for me. I'm building a shared library against only system libraries. 

I'm just complementing your points, not replacing.


-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-01-14 10:44 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-12 14:07 Do definitions in headers still help optimization? Licht Martin Werner
2024-01-12 14:49 ` LIU Hao
2024-01-12 16:01   ` Xi Ruoyao
2024-01-12 17:58     ` LIU Hao
2024-01-14 10:44       ` Xi Ruoyao

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 read-only IMAP folder(s) and NNTP newsgroup(s).