Article View: comp.lang.c
Article #16Re: will it be optimized?
From: fir
Date: Wed, 07 Oct 2026 08:09
Date: Wed, 07 Oct 2026 08:09
194 lines
6245 bytes
6245 bytes
Andrey Tarasevich pisze:
> On Mon 10/5/2026 5:24 PM, fir wrote:
>> but the question is if i would use
>>
>> f(O*a)
>> {
>> O o = *a;
>> //... o.something here
>> }
>>
>>
>> will it be optimized or not?
> It depends on the implementation. Optimizations are based on pattern
> matching. If someone in the compiler development team though of this
> specific pattern, they might've decided to implement it. If not, then it
> is not there. They could even resort to literal translation of your
> assignment into physical memory copying even if there's no real reason
> for doing so.
>
> 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.
>
what if you would also add a piece of code which uses it ?
becuse imo it should be added becouse without the code of
calller its not full example imo
it should be like
main()
{
Struct S s = foo();
}
in my understanding gcc optimises quite deeply BUT i never know if some
rigid official language rules or some specific cases hard to follow
without more thinking do not stop some obvious optimisation..
what you say its kkinda sad but imo its needed to add this caller code
also you could test this my more specific example
typedef struct {
float field1;
float field2;
float field3;
float field4;
float field5;`
float field6;
float field7;
float field8;
float field9;
float field10;
float field11;
float field12;
} BigStructure;
boo(BigStructure* o)
{
(*o).field2+=9.0;
}
foo(BigStructure* o)
{
BigSTructure a = *o;
a.field7+=4;
boo(&a);
*o=a;
}
main()
{
BigStructure o;
foo(&o);
}
relying on ocmpiler its overally sad situation (becouse it can fail)
overally it seems just to be prbably c fault here, though i would need
to focus a bit to say why
BS foo(BS o)
{
return BS;
}
it just seems
C is particularly bad suited to working with large structures passed as
function arguments.
(note if my sentence has dot and stars with big letter its made by ai
(like by translation form polish later edited)
some could say foo(BS*o) is okay but the syntax inside if o is big and
you acces many fields is horrible
HOW TO MEND IT?
c++ reference is sorta way or yet better something that iw as saying
already few years ago
there should be no passing by value for big structures
BS foo(BS o)
{
return BS;
}
just kinda hiden(/not explicit like in c++) reference
probably only for read when pasing and only for write when
returning
it could eventually by rethinked if there are no some complications with
it as today im not quite able to deeper focus as it seems
(for example in syntax above as one reference is for read and one for
write syntax suggest it may point to two objects and there is question
if it has some sense and shoud be allowed or if not and if so it
shouldnt be this way)
Technical Details
Message-ID:
<11a4un2$2lump$1@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-mailReferences:
<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: fir <profesor.fir@gmail.com> Newsgroups: comp.lang.c Subject: Re: will it be optimized? Date: Wed, 7 Oct 2026 10:09:03 +0200 Organization: A noiseless patient Spider Lines: 194 Message-ID: <11a4un2$2lump$1@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:09:07 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2816729"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19k/6+bIi4ysqhuksbiwgOeGXtB8XxmYco="; posting-host="f07c9b093bd94440653930cbb1e1ee13" User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0 SeaMonkey/2.53.24 Cancel-Lock: sha1:9QfJblNYhhByKIKjpDJ7qtcqyV8= sha256:C5jxtojOXSRN3s3DrT+BnvZ35IjjlfZ5GmCrQmcoN6g= sha1:S4Xipj8E2Lp57Pw0NEgQmd9BP24= sha256:iApotkuqxvHO1sNrRNaKRS+VXhqrhOBIou89rdVKQ04= In-Reply-To: <11a4qor$2kj95$1@dont-email.me>