[Bug c/102294] New: structure assignment slower than memberwise initialization

bart.vanassche at gmail dot com gcc-bugzilla@gcc.gnu.org
Sun Sep 12 21:59:18 GMT 2021


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102294

            Bug ID: 102294
           Summary: structure assignment slower than memberwise
                    initialization
           Product: gcc
           Version: 11.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bart.vanassche at gmail dot com
  Target Milestone: ---

Created attachment 51444
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51444&action=edit
Test program that illustrates the issue

The output of the attached test program is as follows for an Intel Core i7-4790
CPU (3.6 GHz) when compiled with -O2:
$ ~/test/bio_init 
Elapsed time: 0.874763 s
Elapsed time: 0.480335 s
Elapsed time: 0.733273 s

The above output shows that bio_init2() runs faster than bio_init3() and that
bio_init3() runs faster than bio_init1(). bio_init3() uses structure assignment
to initialize struct bio while bio_init2() uses memberwise initialization.
bio_init1() uses memset(). To me it was a big surprise to see that bio_init3()
is slower than bio_init2(). Apparently clang generates better code:

$ clang -O2 -o bio_init-clang bio_init.c
$ ./bio_init-clang 

Elapsed time: 0.446804 s
Elapsed time: 0.455009 s
Elapsed time: 0.407392 s

Can gcc be modified such that bio_init3() runs at least as fast as bio_init2()?

The bio_init[123]() source code comes from the Linux kernel. Optimization level
-O2 has been chosen because that is what the Linux kernel uses.


More information about the Gcc-bugs mailing list