Skip to contentSkip to conversation

trivial: the memset operation on a automatic array variable should be optimized out by data initialization

PATCH In review Denis Cheng 10 participants 35 messages Jun 23, 2007 – Jun 25, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 trivial: the memset operation on a automatic array variable should be optimized out by data initialization

From: Denis Cheng <[email protected]>

the explicit memset call could be optimized out by data initialization, thus all the fill working can be done by the compiler implicitly.

and C standard guaranteed all the unspecified data field initialized to zero.

---
After comments in the former threads:
http://lkml.org/lkml/2007/6/18/119
http://lkml.org/lkml/2007/6/18/48

Quoted 9 lines · Jan Engelhardt

The cost is the same. "= {0}" is transformed into a bunch of movs, or a rep mov, (At least for x86), so is equivalent to memset (which will get transformed to __builtin_memset anyway). So I wonder what this really buys.

And, you do not even need the zero. Just write

        ...[MAX_NR_ZONES] = {};

        Jan

I also think this style of zero initialization would be better. so the patch is little different:

x86_64/mm/init.c renamed from x86_64/mm/init.c.orig +2 -2
@@ -406,8 +406,8 @@ void __cpuinit zap_low_mappings(int cpu)
 #ifndef CONFIG_NUMA
 void __init paging_init(void)
 {
-	unsigned long max_zone_pfns[MAX_NR_ZONES];
-	memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
+	unsigned long max_zone_pfns[MAX_NR_ZONES] = {};
+
 	max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
 	max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
 	max_zone_pfns[ZONE_NORMAL] = end_pfn;
#1 lore raw

* From: Denis Cheng
* Newsgroups: linux.kernel
* Date: Fri, 22 Jun 2007 22:15:49 -0700 (PDT)

Quoted 4 lines

From: Denis Cheng <[email protected]>

the explicit memset call could be optimized out by data initialization, thus all the fill working can be done by the compiler implicitly.

Can be optimized and can be done by compiler are just words;

Quoted 1 line

and C standard guaranteed all the unspecified data field initialized to zero.

standards and implementation are on opposite poles of magnet

Quoted 5 lines

---
After comments in the former threads:
http://lkml.org/lkml/2007/6/18/119

i see a patch

Quoted 1 line

http://lkml.org/lkml/2007/6/18/48

same patch.

x86_64/mm/init.c void __cpuinit zap_low_mappings(int cpu) · line 413

Quoted 11 lines
x86_64/mm/init.c renamed from x86_64/mm/init.c.orig +2 -2
@@ -406,8 +406,8 @@ void __cpuinit zap_low_mappings(int cpu)
 #ifndef CONFIG_NUMA
 void __init paging_init(void)
 {
-	unsigned long max_zone_pfns[MAX_NR_ZONES];
-	memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
+	unsigned long max_zone_pfns[MAX_NR_ZONES] = {};
+
 	max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
 	max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
 	max_zone_pfns[ZONE_NORMAL] = end_pfn;

Why not just show actual objdump output on code (maybe with different oxygen atoms used in gcc), rather than *talking* about optimization and standards, hm?

I bet, that will be a key for success. And if you are interested in such optimizations, why not to grep whole source tree for this kind of things? I'm not sure one function in arch/x86_64 is only such ``unoptimized''. And after doing that maybe you will see, that "{}" initializer can be applied not only to integer values (you did init with of *long int*, with *int*, btw), but to structs and others.

Ahh, one more thing about _optimizing_ your time, i.e. not wasting one.

Add to CC list people, who already did reply on you patch. Otherwise you are showing your disrespect for them and hiding from further discussion.

I think you do not, but Linux development not have an automatic system for patch tracking, so you are on your own with your text editor and e-mail client on this. Please take care for your time.

--
frenzy
-o--=O`C
#oo'L O
<___=E M

#2 lore raw
Quoted 6 lines · Oleg Verych

* From: Denis Cheng
* Newsgroups: linux.kernel
* Date: Fri, 22 Jun 2007 22:15:49 -0700 (PDT)

Quoted 2 lines

From: Denis Cheng <[email protected]>

Quoted 8 lines
Quoted 2 lines

the explicit memset call could be optimized out by data initialization, thus all the fill working can be done by the compiler implicitly.

Can be optimized and can be done by compiler are just words;

Quoted 1 line

and C standard guaranteed all the unspecified data field initialized to zero.

standards and implementation are on opposite poles of magnet

Bullshit.

We expect a C compiler, and if a C compiler violates the C standard that's a bug in the compiler that has to be fixed.

And gcc is usually quite good in following the C standard.

Quoted 6 lines
Quoted 5 lines

---
After comments in the former threads:
http://lkml.org/lkml/2007/6/18/119

Quoted 6 lines

i see a patch

Quoted 1 line

http://lkml.org/lkml/2007/6/18/48

same patch.
...

Open your eyes and you'll find thread overviews at the left side of the URLs he gave...

cu
Adrian

Signature · 4 lines
       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed
#3 lore raw
Quoted 1 line · Adrian Bunk

On Sat, Jun 23, 2007 at 09:59:33AM +0200, Oleg Verych wrote:

[]

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

From: Denis Cheng <[email protected]>

the explicit memset call could be optimized out by data initialization, thus all the fill working can be done by the compiler implicitly.

Can be optimized and can be done by compiler are just words;

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

and C standard guaranteed all the unspecified data field initialized to zero.

standards and implementation are on opposite poles of magnet

Bullshit.

We expect a C compiler, and if a C compiler violates the C standard that's a bug in the compiler that has to be fixed.

If you are serious, please consider last kernel headers vs ANSI C discussion, then GNU extensions of the GCC C compiler and relevant "if ICC doesn't support GCC extensions it's ICC's bug". That was about implementation. About standards you are not serious, aren't you? (Please don't see this as for this particular case, but as general viewpoint)

Quoted 10 lines

And gcc is usually quite good in following the C standard.

Quoted 8 lines
Quoted 5 lines

---
After comments in the former threads:
http://lkml.org/lkml/2007/6/18/119

i see a patch

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

http://lkml.org/lkml/2007/6/18/48

same patch.
...

Open your eyes and you'll find thread overviews at the left side of the URLs he gave...

Two threads with *different* URLs but with *same* patch...

Quoted 3 lines

cu
Adrian

Where's constructive context and support of yet another patch author, Adrian?

Quoted 7 lines
Signature · 4 lines
       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed

____

#4 lore raw
Quoted 14 lines · Oleg Verych
Quoted 1 line · Adrian Bunk

On Sat, Jun 23, 2007 at 09:59:33AM +0200, Oleg Verych wrote:

[]

Quoted 11 lines
Quoted 10 lines
Quoted 4 lines

From: Denis Cheng <[email protected]>

the explicit memset call could be optimized out by data initialization, thus all the fill working can be done by the compiler implicitly.

Can be optimized and can be done by compiler are just words;

Quoted 1 line

and C standard guaranteed all the unspecified data field initialized to zero.

standards and implementation are on opposite poles of magnet

Quoted 7 lines
Quoted 4 lines

Bullshit.

We expect a C compiler, and if a C compiler violates the C standard that's a bug in the compiler that has to be fixed.

If you are serious, please consider last kernel headers vs ANSI C discussion,

If only Joerg would tell us where the problem exactly is...

There might be a bug in the kernel header, but this simply has to be fixed.

Quoted 2 lines

then GNU extensions of the GCC C compiler and relevant "if ICC doesn't support GCC extensions it's ICC's bug".

gcc is a C compiler and claims to follow the C standard. The kernel does not claim to be compilable by a plain C compiler.

Spot the difference?

Quoted 4 lines

That was about
implementation. About standards you are not serious, aren't you? (Please don't see this as for this particular case, but as general viewpoint)

And as with many generalizations, that's often wrong...

Quoted 12 lines
Quoted 12 lines

And gcc is usually quite good in following the C standard.

Quoted 10 lines
Quoted 5 lines

---
After comments in the former threads:
http://lkml.org/lkml/2007/6/18/119

i see a patch

Quoted 1 line

http://lkml.org/lkml/2007/6/18/48

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

same patch.
...

Open your eyes and you'll find thread overviews at the left side of the URLs he gave...

Two threads with *different* URLs but with *same* patch...
...

The comments are in the _threads_.
The patches are only the roots of the threads.

cu
Adrian

Signature · 4 lines
       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed
#5 lore raw
Quoted 1 line

gcc is a C compiler and claims to follow the C standard.

Not with the options the kernel build uses. But, close enough -- the differences are really minor stuff.

Segher

author
#6 lore raw
In reply to Oleg Verych
Quoted 3 lines · Oleg Verych

Why not just show actual objdump output on code (maybe with different oxygen atoms used in gcc), rather than *talking* about optimization and standards, hm?

here is the objdump output of the two object files:
As you could see, the older one used 0x38 bytes stack space while the new one used 0x28 bytes,
and the object code is two bytes less,
I think all these benefits are the gcc's __builtin_memset optimization than the explicit call to memset.

$ objdump -d /tmp/init.orig.o|grep -A23 -nw '<paging_init>'
525:0000000000000395 <paging_init>:
526- 395:       48 83 ec 38             sub    $0x38,%rsp
527- 399:       48 8d 54 24 10          lea    0x10(%rsp),%rdx
528- 39e:       fc                      cld
529- 39f:       31 c0                   xor    %eax,%eax
530- 3a1:       48 89 d7                mov    %rdx,%rdi
531- 3a4:       ab                      stos   %eax,%es:(%rdi)
532- 3a5:       ab                      stos   %eax,%es:(%rdi)
533- 3a6:       ab                      stos   %eax,%es:(%rdi)
534- 3a7:       ab                      stos   %eax,%es:(%rdi)
535- 3a8:       ab                      stos   %eax,%es:(%rdi)
536- 3a9:       48 89 7c 24 08          mov    %rdi,0x8(%rsp)
537- 3ae:       ab                      stos   %eax,%es:(%rdi)
538- 3af:       48 c7 44 24 10 00 10    movq   $0x1000,0x10(%rsp)
539- 3b6:       00 00
540- 3b8:       48 c7 44 24 18 00 00    movq   $0x100000,0x18(%rsp)
541- 3bf:       10 00
542- 3c1:       48 8b 05 00 00 00 00    mov    0(%rip),%rax        #
3c8 <paging_init+0x33>
543- 3c8:       48 89 44 24 20          mov    %rax,0x20(%rsp)
544- 3cd:       48 89 d7                mov    %rdx,%rdi
545- 3d0:       e8 00 00 00 00          callq  3d5 <paging_init+0x40>
546- 3d5:       48 83 c4 38             add    $0x38,%rsp
547- 3d9:       c3                      retq
548-
$ objdump -d /tmp/init.new.o|grep -A23 -nw '<paging_init>'
525:0000000000000395 <paging_init>:
526- 395:       48 83 ec 28             sub    $0x28,%rsp
527- 399:       48 89 e7                mov    %rsp,%rdi
528- 39c:       fc                      cld
529- 39d:       31 c0                   xor    %eax,%eax
530- 39f:       ab                      stos   %eax,%es:(%rdi)
531- 3a0:       ab                      stos   %eax,%es:(%rdi)
532- 3a1:       ab                      stos   %eax,%es:(%rdi)
533- 3a2:       ab                      stos   %eax,%es:(%rdi)
534- 3a3:       ab                      stos   %eax,%es:(%rdi)
535- 3a4:       ab                      stos   %eax,%es:(%rdi)
536- 3a5:       48 c7 04 24 00 10 00    movq   $0x1000,(%rsp)
537- 3ac:       00
538- 3ad:       48 c7 44 24 08 00 00    movq   $0x100000,0x8(%rsp)
539- 3b4:       10 00
540- 3b6:       48 8b 05 00 00 00 00    mov    0(%rip),%rax        #
3bd <paging_init+0x28>
541- 3bd:       48 89 44 24 10          mov    %rax,0x10(%rsp)
542- 3c2:       48 89 e7                mov    %rsp,%rdi
543- 3c5:       e8 00 00 00 00          callq  3ca <paging_init+0x35>
544- 3ca:       48 83 c4 28             add    $0x28,%rsp
545- 3ce:       c3                      retq
546-
547-00000000000003cf <alloc_low_page>:
548- 3cf:       41 56                   push   %r14
Quoted 7 lines

I bet, that will be a key for success. And if you are interested in such optimizations, why not to grep whole source tree for this kind of things? I'm not sure one function in arch/x86_64 is only such ``unoptimized''. And after doing that maybe you will see, that "{}" initializer can be applied not only to integer values (you did init with of *long int*, with *int*, btw), but to structs and others.

with '{}' initializer, gcc will fill its memory with zeros.

to other potential points to be optimized, I only see this trivial as the first point, I wonder how people gives comments on this; and if this optimization can be tested correctly, this can be done as an optimization example and I'll try others.

Quoted 6 lines

Ahh, one more thing about _optimizing_ your time, i.e. not wasting one.

Add to CC list people, who already did reply on you patch. Otherwise you are showing your disrespect for them and hiding from further discussion.

Thank you, I know it and I've already subscribed the linux kernel mailing list([email protected]) so that I won't miss any further discussion about it.

Quoted 4 lines

I think you do not, but Linux development not have an automatic system for patch tracking, so you are on your own with your text editor and e-mail client on this. Please take care for your time.

What about that?
Do you mean something such as git by "an automatic system"?

Quoted 7 lines

--
frenzy
-o--=O`C
#oo'L O
<___=E M

Signature · 2 lines
Denis Cheng
Linux Application Developer
#7 lore raw
Quoted 8 lines · rae l
Quoted 3 lines · Oleg Verych

Why not just show actual objdump output on code (maybe with different oxygen atoms used in gcc), rather than *talking* about optimization and standards, hm?

here is the objdump output of the two object files:
As you could see, the older one used 0x38 bytes stack space while the new one used 0x28 bytes,
and the object code is two bytes less,

Actually more: $((0x3d9 - 0x3ce))

Quoted 2 lines

I think all these benefits are the gcc's __builtin_memset optimization than the explicit call to memset.

... or from complex memset() implementation (some chips even didn't do `rep' fast enough somehow). Maybe code like below will be acceptable for both optimizers and maintainers?

|-*-

        unsigned long max_zone_pfns[MAX_NR_ZONES] = {
                [ZONE_DMA] = MAX_DMA_PFN,
                [ZONE_DMA32] = MAX_DMA32_PFN,
                [ZONE_NORMAL] = end_pfn,
                [ZONE_MOVABLE] = 0UL
        };

|-*-

Quoted 1 line
$ objdump -d /tmp/init.orig.o|grep -A23 -nw '<paging_init>'

[]

Quoted 1 line

547- 3d9: c3 retq

[]

Quoted 1 line

545- 3ce: c3 retq

[]

Quoted 8 lines
Quoted 8 lines

I bet, that will be a key for success. And if you are interested in such optimizations, why not to grep whole source tree for this kind of things? I'm not sure one function in arch/x86_64 is only such ``unoptimized''.
And after doing that maybe you will see, that "{}" initializer can be applied not only to integer values (you did init with of *long int*, with *int*, btw), but to structs and others.

Quoted 6 lines

with '{}' initializer, gcc will fill its memory with zeros.

to other potential points to be optimized, I only see this trivial as the first point, I wonder how people gives comments on this; and if this optimization can be tested correctly, this can be done as an optimization example and I'll try others.

Yes, comments and discussion is most important thing. But with such propositions you will be better in the kernel-janitors list.

Quoted 9 lines
Quoted 6 lines

Ahh, one more thing about _optimizing_ your time, i.e. not wasting one.

Add to CC list people, who already did reply on you patch. Otherwise you are showing your disrespect for them and hiding from further discussion.

Thank you, I know it and I've already subscribed the linux kernel mailing list([email protected]) so that I won't miss any further discussion about it.

OK, but news<=>e-mail service, like Gmane is much nicer.

Quoted 6 lines
Quoted 4 lines

I think you do not, but Linux development not have an automatic system for patch tracking, so you are on your own with your text editor and e-mail client on this. Please take care for your time.

What about that?
Do you mean something such as git by "an automatic system"?

That was a side note.
____

#8 lore raw
Quoted 7 lines
Quoted 3 lines

I think all these benefits are the gcc's __builtin_memset optimization than the explicit call to memset.

... or from complex memset() implementation (some chips even didn't do `rep' fast enough somehow). Maybe code like below will be acceptable for both optimizers and maintainers?

we should just alias our memset to the __builtin one, and then provide a generic one from lib/ for the cases gcc needs to do a fallback.

#9 lore raw

memset() with zeroes (Re: [PATCH] trivial: the memset operation on a automatic array variable should be optimized out by data initialization)

Quoted 11 lines · Arjan van de Ven
Quoted 7 lines
Quoted 3 lines

I think all these benefits are the gcc's __builtin_memset optimization than the explicit call to memset.

... or from complex memset() implementation (some chips even didn't do `rep' fast enough somehow). Maybe code like below will be acceptable for both optimizers and maintainers?

we should just alias our memset to the __builtin one, and then provide a generic one from lib/ for the cases gcc needs to do a fallback.

In x86_64 there's infrastructure to check and select right memset(). Therefor it's need, i think.

But if one will took a look at usage, zero memset() optimization becomes obvious, one argument off -- one reg is free from clobbering.

|-*-
flower-:22-rc4-mm2/arch/x86_64$ grep memset -R . | grep "[ 0,]0," | wc -l 42
flower-:22-rc4-mm2/arch/x86_64$ flower-:22-rc4-mm2/arch/x86_64$ cd .. flower-:22-rc4-mm2/arch$ grep memset -R . | grep "[ 0,]0," | wc -l 735
flower-:22-rc4-mm2/arch$
flower-:22-rc4-mm2$ grep memset -R . | grep "[ 0,]0," | wc -l 6679
flower-:22-rc4-mm2$
|-*-
____

#10 lore raw
In reply to Arjan van de Ven
Quoted 2 lines · Arjan van de Ven

we should just alias our memset to the __builtin one, and then provide a generic one from lib/ for the cases gcc needs to do a fallback.

The last time I checked, gcc generated horrible badly performing code for builtin memset/memcpy() when -Os is specified.

-ben

Signature · 2 lines
"Time is of no importance, Mr. President, only life is important."
Don't Email: <[email protected]>.
#11 lore raw
Quoted 6 lines · Benjamin LaHaise
Quoted 2 lines · Arjan van de Ven

we should just alias our memset to the __builtin one, and then provide a generic one from lib/ for the cases gcc needs to do a fallback.

The last time I checked, gcc generated horrible badly performing code for builtin memset/memcpy() when -Os is specified.

if you care about the last cycle, don't specify -Os but -O2. simple as that... you get what you tell the compiler you want.

Signature · 2 lines
if you want to mail me at work (you don't), use arjan (at) linux.intel.com
Test the interaction between Linux and your BIOS via http://www.linuxfirmwarekit.org
#12 lore raw
Quoted 2 lines · Arjan van de Ven

if you care about the last cycle, don't specify -Os but -O2. simple as that... you get what you tell the compiler you want.

Certain distros are shipping kernels compiled with -Os. And it's more than just a couple of cycles.

-ben

Signature · 2 lines
"Time is of no importance, Mr. President, only life is important."
Don't Email: <[email protected]>.
#13 lore raw
Quoted 6 lines · Benjamin LaHaise
Quoted 2 lines · Arjan van de Ven

if you care about the last cycle, don't specify -Os but -O2. simple as that... you get what you tell the compiler you want.

Certain distros are shipping kernels compiled with -Os. And it's more than just a couple of cycles.

so those distros pick space over some cycles. Who are you to then override that choice ? ;-)

seriously, why are we even talking about overriding a choice the user (or distro vendor as user) made here?

Signature · 2 lines
if you want to mail me at work (you don't), use arjan (at) linux.intel.com
Test the interaction between Linux and your BIOS via http://www.linuxfirmwarekit.org
#14 lore raw

-Os versus -O2

Quoted 5 lines · Arjan van de Ven
Quoted 4 lines · Benjamin LaHaise
Quoted 2 lines · Arjan van de Ven

if you care about the last cycle, don't specify -Os but -O2. simple as that... you get what you tell the compiler you want.

Quoted 8 lines
Quoted 2 lines

Certain distros are shipping kernels compiled with -Os. And it's more than just a couple of cycles.

so those distros pick space over some cycles. Who are you to then override that choice ? ;-)

seriously, why are we even talking about overriding a choice the user (or distro vendor as user) made here?

There is a real issue in the fact that compiling with -Os is available through a kconfig option and AFAIR used by some distributions.

I doubt distros enable CONFIG_CC_OPTIMIZE_FOR_SIZE due to size considerations, but due to speed considerations.

I wouldn't care if CONFIG_CC_OPTIMIZE_FOR_SIZE was hidden behind CONFIG_EMBEDDED, but as long as it's available as a general purpose option we have to consider it's performance.

The interesting questions are:
Does -Os still sometimes generate faster code with gcc 4.2? If yes, why?

cu
Adrian

Signature · 4 lines
       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed
#15 lore raw

Re: -Os versus -O2

Quoted 4 lines

I wouldn't care if CONFIG_CC_OPTIMIZE_FOR_SIZE was hidden behind CONFIG_EMBEDDED, but as long as it's available as a general purpose option we have to consider it's performance.

I think you are missing the point. You tell the kernel to OPTIMIZE_FOR_SIZE. *over performance*. Sure. Performance shouldn't be EXTREMELY pathetic, but it's not; and if it were, it's a problem with the gcc version you have (and if you are a distro, you can surely fix that)

Quoted 4 lines

The interesting questions are:
Does -Os still sometimes generate faster code with gcc 4.2? If yes, why?

on a system level, size can help performance because you have more memory available for other things. It also reduces download size and gives you more space on the live CD....

if you want to make things bigger again, please do this OUTSIDE the "optimize for size" option. Because that TELLS you to go for size.

Signature · 2 lines
if you want to mail me at work (you don't), use arjan (at) linux.intel.com
Test the interaction between Linux and your BIOS via http://www.linuxfirmwarekit.org
#16 lore raw

Re: -Os versus -O2

Quoted 15 lines · Arjan van de Ven
Quoted 3 lines

I wouldn't care if CONFIG_CC_OPTIMIZE_FOR_SIZE was hidden behind CONFIG_EMBEDDED, but as long as it's available as a general purpose option we have to consider it's performance.

I think you are missing the point. You tell the kernel to OPTIMIZE_FOR_SIZE. *over performance*. Sure. Performance shouldn't be EXTREMELY pathetic, but it's not; and if it were, it's a problem with the gcc version you have (and if you are a distro, you can surely fix that)

Quoted 4 lines

The interesting questions are:
Does -Os still sometimes generate faster code with gcc 4.2? If yes, why?

Quoted 6 lines

on a system level, size can help performance because you have more memory available for other things. It also reduces download size and gives you more space on the live CD....

if you want to make things bigger again, please do this OUTSIDE the "optimize for size" option. Because that TELLS you to go for size.

then do we need a new option 'optimize for best overall performance' that goes for size (and the corresponding wins there) most of the time, but is ignored where it makes a huge difference?

I started useing Os several years ago, even when it was hidden in the embedded menu becouse in many cases the smaller binary ended up being faster.

in reality this was a flaw in gcc that on modern CPU's with the larger difference between CPU speed and memory speed it still preferred to unroll loops (eating more memory and blowing out the cpu cache) when it shouldn't have.

if that has been fixed on later versions of gcc this would be a good thing. if it hasn't (possibly in part due to gcc optimizations being designed to be cross platform) then either the current 'go for size' or a hybrid 'performance' option is needed.

David Lang

#17 lore raw

Re: -Os versus -O2

Quoted 11 lines · On Sun, 2007-06-24 at 18:08 -0700, [email protected] wrote:
Quoted 7 lines

on a system level, size can help performance because you have more memory available for other things. It also reduces download size and gives you more space on the live CD....

if you want to make things bigger again, please do this OUTSIDE the "optimize for size" option. Because that TELLS you to go for size.

then do we need a new option 'optimize for best overall performance' that goes for size (and the corresponding wins there) most of the time, but is ignored where it makes a huge difference?

that isn't so easy. Anything which doesn't have a performance tradeoff is in -O2 already. So every single thing in -Os costs you performance on a micro level.

The translation to macro level depends greatly on how things are used (you even have to factor in download times etc)... so that is a fair question to leave up to the user... which is what there is today.

Signature · 2 lines
if you want to mail me at work (you don't), use arjan (at) linux.intel.com
Test the interaction between Linux and your BIOS via http://www.linuxfirmwarekit.org
#18 lore raw

Re: -Os versus -O2

Quoted 9 lines · Arjan van de Ven
Quoted 8 lines · On Sun, 2007-06-24 at 18:08 -0700, [email protected] wrote:
Quoted 7 lines

on a system level, size can help performance because you have more memory available for other things. It also reduces download size and gives you more space on the live CD....

if you want to make things bigger again, please do this OUTSIDE the "optimize for size" option. Because that TELLS you to go for size.

Quoted 7 lines
Quoted 3 lines

then do we need a new option 'optimize for best overall performance' that goes for size (and the corresponding wins there) most of the time, but is ignored where it makes a huge difference?

that isn't so easy. Anything which doesn't have a performance tradeoff is in -O2 already. So every single thing in -Os costs you performance on a micro level.

this has not been true in the past (assuming that it's true today)

ok, if you look at a micro-enough level this may be true, but completely ignoring things like download times, the optimizations almost always boil down to trying to avoid jumps, loops, and decision logic at the expense of space.

however recent cpu's are significantly better as handling jumps and loops, and the cost of cache misses is significantly worse.

is the list of what's included in -O2 vs -Os different for different CPU's? what about within a single family of processors? (even in the x86 family the costs of jumps, loops, and cache misses varies drasticly)

my understanding was that the optimizations for O2 were pretty fixed.

Quoted 3 lines

The translation to macro level depends greatly on how things are used (you even have to factor in download times etc)... so that is a fair question to leave up to the user... which is what there is today.

ignore things like download time for the moment. it's not significant to most people as they don't download things that often, and when they do they are almost always downloading lots of stuff they don't need (drivers for example)

users are trying to get better performance 90+% of the time when they select -Os. That's why it got moved out of CONFIG_EMBEDDED.

David Lang

#19 lore raw

Re: -Os versus -O2

Quoted 1 line · On 06/25/2007 03:33 AM, [email protected] wrote:

is the list of what's included in -O2 vs -Os different for different CPU's? what about within a single family of processors? (even in the x86 family the costs of jumps, loops, and cache misses varies drasticly)

At least not in the example Duron/Athlon case. Both -march=athlon{,-4) but 64K versus 256K L2 which I'd expect to be an important difference in the -Os versus -O2 behaviour.

Rene.

#20 lore raw

Re: -Os versus -O2

In reply to [email protected]
Quoted 39 lines · On Sun, Jun 24, 2007 at 06:33:15PM -0700, [email protected] wrote:
Quoted 9 lines · Arjan van de Ven
Quoted 8 lines · On Sun, 2007-06-24 at 18:08 -0700, [email protected] wrote:
Quoted 7 lines

on a system level, size can help performance because you have more memory available for other things. It also reduces download size and gives you more space on the live CD....

if you want to make things bigger again, please do this OUTSIDE the "optimize for size" option. Because that TELLS you to go for size.

Quoted 7 lines
Quoted 3 lines

then do we need a new option 'optimize for best overall performance' that goes for size (and the corresponding wins there) most of the time, but is ignored where it makes a huge difference?

that isn't so easy. Anything which doesn't have a performance tradeoff is in -O2 already. So every single thing in -Os costs you performance on a micro level.

this has not been true in the past (assuming that it's true today)

ok, if you look at a micro-enough level this may be true, but completely ignoring things like download times, the optimizations almost always boil down to trying to avoid jumps, loops, and decision logic at the expense of space.

however recent cpu's are significantly better as handling jumps and loops, and the cost of cache misses is significantly worse.

is the list of what's included in -O2 vs -Os different for different CPU's? what about within a single family of processors? (even in the x86 family the costs of jumps, loops, and cache misses varies drasticly)

my understanding was that the optimizations for O2 were pretty fixed.

Quoted 3 lines

The translation to macro level depends greatly on how things are used (you even have to factor in download times etc)... so that is a fair question to leave up to the user... which is what there is today.

Quoted 7 lines

ignore things like download time for the moment. it's not significant to most people as they don't download things that often, and when they do they are almost always downloading lots of stuff they don't need (drivers for example)

users are trying to get better performance 90+% of the time when they select -Os. That's why it got moved out of CONFIG_EMBEDDED.

In my experience, -Os produced faster code on gcc-2.95 than -O2 or -O3. It was not only because of cache considerations, but because gcc used different tricks to avoid poor optimizations, and at the end, the CPU ended executing the alternative code faster.

With gcc-3.3, -Os show roughly the same performance as -O2 for me on various programs. However, with gcc-3.4, I noticed a slow down with -Os. And with gcc-4, using -Os optimizes only for size, even if the output code is slow as hell. I've had programs whose speed dropped by 70% using -Os on gcc-4. But their size was smaller than with older versions.

But in some situtations, it's desirable to have the smallest possible kernel whatever its performance. This goes for installation CDs for instance.

Willy

#21 lore raw

Re: -Os versus -O2

Quoted 1 line

In my experience, -Os produced faster code on gcc-2.95 than -O2 or -O3.

On what CPU? The effect of different optimisations varies hugely between different CPUs (and architectures).

Quoted 3 lines

It was not only because of cache considerations, but because gcc used different tricks to avoid poor optimizations, and at the end, the CPU ended executing the alternative code faster.

-Os is "as fast as you can without bloating the code size", so that is the expected result for CPUs that don't need special hand-holding around certain performance pitfalls.

Quoted 5 lines

With gcc-3.3, -Os show roughly the same performance as -O2 for me on various programs. However, with gcc-3.4, I noticed a slow down with -Os. And with gcc-4, using -Os optimizes only for size, even if the output code is slow as hell. I've had programs whose speed dropped by 70% using -Os on gcc-4.

Well you better report those! <http://gcc.gnu.org/bugzilla>

Quoted 3 lines

But in some situtations, it's desirable to have the smallest possible kernel whatever its performance. This goes for installation CDs for instance.

There are much better ways to achieve that.

Segher

#22 lore raw

Re: -Os versus -O2

Quoted 5 lines · Segher Boessenkool
Quoted 1 line

In my experience, -Os produced faster code on gcc-2.95 than -O2 or -O3.

On what CPU? The effect of different optimisations varies hugely between different CPUs (and architectures).

Quoted 7 lines
Quoted 3 lines

It was not only because of cache considerations, but because gcc used different tricks to avoid poor optimizations, and at the end, the CPU ended executing the alternative code faster.

-Os is "as fast as you can without bloating the code size", so that is the expected result for CPUs that don't need special hand-holding around certain performance pitfalls.

this sounds like you are saying that people wanting performance should pick -Os.

what should people pick who care more about code size then anything else? (examples being embedded development where you may be willing to sacrafice speed to avoid having to add additional chips to the design)

David Lang

#23 lore raw

Re: -Os versus -O2

Quoted 5 lines
Quoted 3 lines

-Os is "as fast as you can without bloating the code size", so that is the expected result for CPUs that don't need special hand-holding around certain performance pitfalls.

this sounds like you are saying that people wanting performance should pick -Os.

That is true on most CPUs. Some CPUs really really need some of things that -Os disables (compared to -O2) for decent performance though (branch target alignment...)

Quoted 1 line

what should people pick who care more about code size then anything else? (examples being embedded development where you may be willing to sacrafice speed to avoid having to add additional chips to the design)

-Os and tune some options. There is extensive work being done over the last few years to make GCC more suitable for embedded targets btw. But the -O1/-O2/-O3/-Os gives you four choices only, it's really not so hard to understand I hope that for more specific goals you need to add more specific options?

Segher

#24 lore raw

Re: -Os versus -O2

In reply to Segher Boessenkool
Quoted 4 lines · Segher Boessenkool
Quoted 1 line

In my experience, -Os produced faster code on gcc-2.95 than -O2 or -O3.

On what CPU? The effect of different optimisations varies hugely between different CPUs (and architectures).

x86

Quoted 8 lines
Quoted 3 lines

It was not only because of cache considerations, but because gcc used different tricks to avoid poor optimizations, and at the end, the CPU ended executing the alternative code faster.

-Os is "as fast as you can without bloating the code size", so that is the expected result for CPUs that don't need special hand-holding around certain performance pitfalls.

Quoted 7 lines
Quoted 5 lines

With gcc-3.3, -Os show roughly the same performance as -O2 for me on various programs. However, with gcc-3.4, I noticed a slow down with -Os. And with gcc-4, using -Os optimizes only for size, even if the output code is slow as hell. I've had programs whose speed dropped by 70% using -Os on gcc-4.

Well you better report those! <http://gcc.gnu.org/bugzilla>

No, -Os is for size only :

-Os Optimize for size. -Os enables all -O2 optimizations
that do not typically increase code size. It also performs further optimizations designed to reduce code size.

So it is expected that speed can be reduced using -Os. I won't report a thing which is already documented !

Quoted 5 lines
Quoted 3 lines

But in some situtations, it's desirable to have the smallest possible kernel whatever its performance. This goes for installation CDs for instance.

There are much better ways to achieve that.

Optimizing is not a matter of choosing *one* way, but cumulating everything you have. For instance, on a smart boot loader, I have a kernel which is about 300 kB, or 700 kB with the initramfs. Among the tricks I used :
- -Os
- -march=i386
- align everything to 0
- replace gzip with p7zip

Even if each of them reduces overall size by 5%, the net result is 0.95^4 = 0.81 = 19% gain, for the same set of features. This is something to consider.

Regards,
Willy

#25 lore raw

Re: -Os versus -O2

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

In my experience, -Os produced faster code on gcc-2.95 than -O2 or -O3.

On what CPU? The effect of different optimisations varies hugely between different CPUs (and architectures).

x86

That's not a CPU, that's an architecture. I hope you understand there are very big differences between different members of the x86 family and you don't compare 2.95 on a Pentium class CPU to 3.x on an Opteron or 4.x on a Pentium4 or something like that.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

With gcc-3.3, -Os show roughly the same performance as -O2 for me on various programs. However, with gcc-3.4, I noticed a slow down with -Os. And with gcc-4, using -Os optimizes only for size, even if the output code is slow as hell. I've had programs whose speed dropped by 70% using -Os on gcc-4.

Quoted 8 lines
Quoted 1 line

Well you better report those! <http://gcc.gnu.org/bugzilla>

No, -Os is for size only :

-Os Optimize for size. -Os enables all -O2 optimizations
that do not typically increase code size. It also performs further optimizations designed to reduce code size.

That is not "for size only". Please read again.

A 70% speed decrease is something that should be at least investigated, even if then perhaps it is decided GCC already does the "right thing".

Quoted 2 lines

So it is expected that speed can be reduced using -Os. I won't report a thing which is already documented !

A few percent points slower is expected, 20% would be explainable, but 70%?

-O2 and -Os are supposed to differ in _minor_ ways. Such a huge performance drop is unexpected. If you file the PR, feel free to blame me for reporting it at all.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

But in some situtations, it's desirable to have the smallest possible kernel whatever its performance. This goes for installation CDs for instance.

There are much better ways to achieve that.

Optimizing is not a matter of choosing *one* way, but cumulating everything you have.

Yes of course. I'm just saying -Os is a pretty minor step in the overall making-things-smaller game. Leaving out XFS helps a whole megabyte on my default target, for example.

Quoted 11 lines

For instance, on a smart boot loader, I have
a kernel which is about 300 kB, or 700 kB with the initramfs. Among the tricks I used :
- -Os
- -march=i386
- align everything to 0
- replace gzip with p7zip

Even if each of them reduces overall size by 5%, the net result is 0.95^4 = 0.81 = 19% gain, for the same set of features. This is something to consider.

Sure. I don't think making -Os mean "as small as possible in all cases" (or, rather, introducing a new option for that) would help terribly much over the current -Os meaning -- a few percent at most. That's not to say that no such optimisations are added anymore, but mostly they turn out not to decrease speed at all and so are enabled at any -O level :-)

Segher

#26 lore raw

Re: -Os versus -O2

In reply to [email protected]
Quoted 1 line

then do we need a new option 'optimize for best overall performance' that goes for size (and the corresponding wins there) most of the time, but is ignored where it makes a huge difference?

That's -Os mostly. Some awful CPUs really need higher loop/label/function alignment though to get any performance; you could add -falign-xxx options for those.

Quoted 1 line

in reality this was a flaw in gcc that on modern CPU's with the larger difference between CPU speed and memory speed it still preferred to unroll loops (eating more memory and blowing out the cpu cache) when it shouldn't have.

You told it to unroll loops, so it did. No flaw. If you feel the optimisations enabled by -O2 should depend on the CPU tuning selected, please file a PR.

Also note that whether or not it is profitable to unroll a particular loop depends largely on how "hot" that loop is, and GCC doesn't know much about that if you don't feed it profiling information (it can guess a bit, sure, but it can guess wrong too).

Segher

#27 lore raw

Re: -Os versus -O2

Quoted 17 lines · Segher Boessenkool
Quoted 3 lines

then do we need a new option 'optimize for best overall performance' that goes for size (and the corresponding wins there) most of the time, but is ignored where it makes a huge difference?

That's -Os mostly. Some awful CPUs really need higher loop/label/function alignment though to get any performance; you could add -falign-xxx options for those.

Quoted 4 lines

in reality this was a flaw in gcc that on modern CPU's with the larger difference between CPU speed and memory speed it still preferred to unroll loops (eating more memory and blowing out the cpu cache) when it shouldn't have.

You told it to unroll loops, so it did. No flaw. If you feel the optimisations enabled by -O2 should depend on the CPU tuning selected, please file a PR.

Quoted 5 lines

Also note that whether or not it is profitable to unroll a particular loop depends largely on how "hot" that loop is, and GCC doesn't know much about that if you don't feed it profiling information (it can guess a bit, sure, but it can guess wrong too).

actually, what you are saying is that the compiler can't know enough to figure out how to optimize for speed. it will just do what you tell it to, either unroll loops or not.

this argues that both O2 and Os are incorrect for a project to use and instead the project needs to make it's own decisions on this.

if this is the true feeling of the gcc team I'm very disappointed, it feels like a huge step backwards.

David Lang

#28 lore raw

Re: -Os versus -O2

Quoted 7 lines
Quoted 5 lines

Also note that whether or not it is profitable to unroll a particular loop depends largely on how "hot" that loop is, and GCC doesn't know much about that if you don't feed it profiling information (it can guess a bit, sure, but it can guess wrong too).

actually, what you are saying is that the compiler can't know enough to figure out how to optimize for speed. it will just do what you tell it to, either unroll loops or not.

It bases its optimisation decisions on the options you give it, the profile feedback information you either or not gave it, and a whole bunch of heuristics.

Quoted 1 line

this argues that both O2 and Os are incorrect for a project to use and instead the project needs to make it's own decisions on this.

For optimal performance, you need to fine-tune options yes, per file (or per function even!)

Quoted 1 line

if this is the true feeling of the gcc team I'm very disappointed, it feels like a huge step backwards.

I speak only for myself. However this is the only way it _can_ be, the compiler isn't clairvoyant. Some of the heuristics sure could use some tuning, but they stay heuristics.

Segher

#29 lore raw

Re: -Os versus -O2

In reply to Arjan van de Ven
Quoted 10 lines · Arjan van de Ven
Quoted 4 lines

I wouldn't care if CONFIG_CC_OPTIMIZE_FOR_SIZE was hidden behind CONFIG_EMBEDDED, but as long as it's available as a general purpose option we have to consider it's performance.

I think you are missing the point. You tell the kernel to OPTIMIZE_FOR_SIZE. *over performance*. Sure. Performance shouldn't be EXTREMELY pathetic, but it's not; and if it were, it's a problem with the gcc version you have (and if you are a distro, you can surely fix that)

My point is commit c45b4f1f1e149c023762ac4be166ead1818cefef

CC_OPTIMIZE_FOR_SIZE is currently known as an experimental feature to improve the _performance_.

Quoted 6 lines
Quoted 3 lines

The interesting questions are:
Does -Os still sometimes generate faster code with gcc 4.2? If yes, why?

on a system level, size can help performance because you have more memory available for other things.

For a given gcc version, there's a finite number of differences between -Os and -O2.

The interesting question is for which differences with gcc 4.2 we want the -Os version in the kernel for best performance. This should then be controllable through gcc options.

Quoted 2 lines

It also reduces download size and
gives you more space on the live CD....

That's a different point.

If you don't care about performance but care about size then -Os is the best choice.

Quoted 2 lines

if you want to make things bigger again, please do this OUTSIDE the "optimize for size" option. Because that TELLS you to go for size.

Agreed, but CONFIG_CC_OPTIMIZE_FOR_SIZE should again be under CONFIG_EMBEDDED.

cu
Adrian

Signature · 4 lines
       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed
#30 lore raw

Re: -Os versus -O2

In reply to Adrian Bunk
Quoted 3 lines · Adrian Bunk

The interesting questions are:
Does -Os still sometimes generate faster code with gcc 4.2? If yes, why?

I would wager that the CPU type makes more of a difference than the compiler version. That is, I'd expect my Duron with it's "puny" 64K L1 to have a very different profile than it's Athlon brother with 256K L1. Not to mention CPUs with as little as 8K L1 (P1).

I can't quote numbers -- it's a bit hard to test those things anyway as it's a system-global effect and not su much that's easily isolated in a dedicated benchmark.

Rene.

#31 lore raw

Re: -Os versus -O2

Quoted 7 lines · Rene Herman
Quoted 3 lines · Adrian Bunk

The interesting questions are:
Does -Os still sometimes generate faster code with gcc 4.2? If yes, why?

I would wager that the CPU type makes more of a difference than the compiler version. That is, I'd expect my Duron with it's "puny" 64K L1 to have a very different profile than it's Athlon brother with 256K L1.

Sorry, that should've been L2. And "its" ...

Quoted 3 lines

Not to mention CPUs with as little as 8K L1 (P1).

I can't quote numbers -- it's a bit hard to test those things anyway as it's a system-global effect and not su much that's easily isolated in a dedicated benchmark.

And while I'm at it, "and not so much one that's [ ...]".

Rene.

#32 lore raw

Re: -Os versus -O2

In reply to Adrian Bunk
Quoted 3 lines · Adrian Bunk

The interesting questions are:
Does -Os still sometimes generate faster code with gcc 4.2? If yes, why?

Smaller code can mean fewer page faults, fewer cache invalidations, etc.

It's not just a matter of compiler code generation, gotta look at the whole picture.

Jeff

#33 lore raw

Re: -Os versus -O2

Quoted 9 lines · Jeff Garzik
Quoted 3 lines · Adrian Bunk

The interesting questions are:
Does -Os still sometimes generate faster code with gcc 4.2? If yes, why?

Smaller code can mean fewer page faults, fewer cache invalidations, etc.

It's not just a matter of compiler code generation, gotta look at the whole picture.

Sure, but my point is that if the kernel is considered special and the best optimization for the kernel is therefore between -Os and -O2, we should try to find this point of best optimization.

This should address Arjans point that -Os might not be best choice for best performance (and it's actually our fault if gcc generates stupid but small code when we use -Os).

Quoted 1 line

Jeff

cu
Adrian

Signature · 4 lines
       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed
#34 lore raw

Re: -Os versus -O2

Quoted 10 lines · Adrian Bunk
Quoted 9 lines · Jeff Garzik
Quoted 3 lines · Adrian Bunk

The interesting questions are:
Does -Os still sometimes generate faster code with gcc 4.2? If yes, why?

Smaller code can mean fewer page faults, fewer cache invalidations, etc.

It's not just a matter of compiler code generation, gotta look at the whole picture.

the picture gets even murkier when you consider that even if neither option overflows the cpu cache the one that takes more space in the cache leaves less space in the cache for the userspacde code that the system is actually there to run.

Quoted 7 lines

Sure, but my point is that if the kernel is considered special and the best optimization for the kernel is therefore between -Os and -O2, we should try to find this point of best optimization.

This should address Arjans point that -Os might not be best choice for best performance (and it's actually our fault if gcc generates stupid but small code when we use -Os).

what can be done to find the horribly bad but small code among the "it's smaller and would be less efficiant if you didn't consider the cache" majority?

David Lang