🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #19

Re: will it be optimized?

#19
From: David Brown
Date: Wed, 07 Oct 2026 08:46
76 lines
3859 bytes
On 07/10/2026 09:01, Andrey Tarasevich wrote:
> 
> On a related node:
> 
> I was quite surprised to discover that GCC treats returning struct types 
> by value overly literally, i.e. fails to optimize it at all. For reasons 
> that defy any rationalization, GCC 16 translates this
> 
>    struct S { char a[1024]; };
> 
>    struct S foo(void)
>    {
>      struct S s = { 0 };
>      s.a[10] = 42;
>      return s;
>    }
> 
> into this (in -O3 mode!)
> 
>    .LC0:
>          .zero   2
>          .zero   1022
>    "foo":
>          push    rbx
>          mov     edx, 1024
>          mov     rbx, rdi
>          mov     esi, OFFSET FLAT:.LC0
>          sub     rsp, 1024
>          mov     rdi, rsp
>          call    "memcpy"
>          mov     rsi, rsp
>          mov     rdi, rbx
>          mov     edx, 1024
>          mov     BYTE PTR [rsp+10], 42
>          call    "memcpy"
>          add     rsp, 1024
>          pop     rbx
>          ret
> 
> https://godbolt.org/z/G89vfsvPa
> 
> Initialization is performed literally by copying from a block of zeros 
> prepared elsewhere (!). Returning is performed by another copying from a 
> local variable into a return area. Weird! Why?
> 
> Clang 21 does this
> 
>    foo:
>          push    rbx
>          mov     rbx, rdi
>          mov     edx, 1024
>          xor     esi, esi
>          call    memset@PLT
>          mov     byte ptr [rbx + 10], 42
>          mov     rax, rbx
>          pop     rbx
>          ret
> 
> https://godbolt.org/z/bx5oM5s1M
> 
> So, it performs a typical C++-style RVO (no `return` copying) and is 
> smart enough to initialize the struct without copying zeros from 
> somewhere else.
> 

That is interesting.  I can think of no reason to suggest that clang's 
code might be incorrect (I've seen a number of occasions where clang has 
generated more efficient code than gcc, but has been lax in adherence to 
C standards or platform ABIs to do so).

When compiling as C++, gcc uses RVO - there is only a single memcpy() 
from a zero block.  When the initialiser for s is changed to the more 
modern "S s = {};" it uses memset rather than copying from a zero block, 
in both C and C++.  But the C version still has the additional memcpy.

Technical Details
Message-ID: <11a50tk$2m3b3$2@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11a1f4c$1g3sg$1@dont-email.me> <11a4qor$2kj95$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: will it be optimized?
Date: Wed, 7 Oct 2026 10:46:44 +0200
Organization: A noiseless patient Spider
Lines: 76
Message-ID: <11a50tk$2m3b3$2@dont-email.me>
References: <11a1f4c$1g3sg$1@dont-email.me> <11a4qor$2kj95$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 07 Oct 2026 08:46:44 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2821475"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+J2m6BHUDBjHcW2e2Wb2fvBzSP7piuu3E="; posting-host="fd0a1bf01e024f2af36d0493face3db6"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:wc+pPG3fDRDWBmzNW+reOGGkD2g= sha256:ZihzWG7RoyBlnhv7KKkNW65c/UfAXvsPS3nMmslrwx8=
	sha1:/73hULV2bBG6OHclz+22911AdVI= sha256:fssKMk3bPcWqBT7QFFzwP0o+9Ln7YtrVb0nQWA3VFYc=
Content-Language: en-GB, nb-NO
In-Reply-To: <11a4qor$2kj95$1@dont-email.me>