Performance gain by avoiding arguments check is very small (less then 1%).
Amit
amitchoudhary0523@gmail.com
Wed Oct 1 13:53:07 GMT 2025
>
> On Tue, Sep 30, 2025, 8:48 PM Sam James <sam@gentoo.org> wrote:
>
> > Amit <amitchoudhary0523@gmail.com> writes:
> >
> > > On Tue, Sep 30, 2025, 2:23 AM Wilco Dijkstra <Wilco.Dijkstra@arm.com>
> > wrote:
> > >
> > > Hi Amit,
> > >
> > > Modern compilers will optimize your example code away. You can see that
> > > there are no checks at all in the main function:
> > https://godbolt.org/z/aeeTx6zqM
> > >
> > > Even if it wasn't optimized, it wouldn't be possible to measure any
> > difference.
> > > Running an empty main() in a loop will just measure shell overheads.
> > Your
> > > results show it takes a few million cycles per iteration, so an extra
> > check cannot
> > > be measured. What you're really measuring is the varying turbo
> > frequency of
> > > your laptop...
> > >
> > > Benchmarking is hard, much harder than you think. It is done by
> > creating a
> > > tight loop around any code you want to measure. You have to use inputs
> > that
> > > are representative of real usage (ideally from traces from actual
> > workloads).
> > > For timing we use a high-resolution (nano-second) timer. You can look at
> > > recent benchmarks in benchtests/ that show how it should be done.
> > >
> > > Cheers,
> > > Wilco
> > >
> > > I am not going to get into any discussion because I have learned that
> > glibc people are defensive people and they will
> > > defend their wrongdoings.
> >
> > I think Wilco's reply was respectful and professional. It's also
> > accurate. I don't see it as defensive at all.
> >
> > >
> > > ------------
> >
""""I don't know why Wilco is saying the wrong things.""""
I had compiled my code using the following command: "gcc measure_time.c"
My gcc version is "gcc version 12.2.0 (Debian 12.2.0-14+deb12u1)".
"ldd --version" returns the following: ldd (Debian GLIBC 2.36-9+deb12u13) 2.36
But it didn't make the main() function empty.
I had generated the assembly code of the program using the following
command: "gcc -S measure_time.c"
I am pasting the assembly code of the main function below:
==================
main:
.LFB7:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
leaq .LC0(%rip), %rax
movq %rax, %rdi
call get_strlen
movl %eax, -4(%rbp)
movl $0, %edi
call exit@PLT
.cfi_endproc
=================
You can see the following instruction in the main function: "call get_strlen"
""""So, Wilco is wrong in saying that even if the code wasn't
optimized, even then the main function will be empty. But this is not
the case. The main function is not empty and it calls get_strlen() and
this means that the experiment of seeing the overhead of arguments
checking was done properly. Also, it looks like Wilco didn't see my
shell script. The program is called 100,000 times and then the time
values are printed. So, why was Wilco in a hurry?""""
I am pasting Wilco's words below:
-------------
> > > Even if it wasn't optimized, it wouldn't be possible to measure any
> > difference.
> > > Running an empty main() in a loop will just measure shell overheads.
---------------
""""So, I don't know why Wilco is saying the wrong things.""""
""""Did he get stumped by the results and he tried to defend
glibc/POSIX by saying the wrong things?""""
---------------------------------------------------------------------------------------------------------
More information about the Libc-alpha
mailing list