2007-06-18 10:12:47

by cheng renquan

[permalink] [raw]
Subject: [PATCH] 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.

Signed-off-by: Denis Cheng <[email protected]>

---
Is there some comments on this?

--- arch/x86_64/mm/init.c.orig 2007-06-07 10:08:04.000000000 +0800
+++ arch/x86_64/mm/init.c 2007-06-18 14:43:15.000000000 +0800
@@ -406,8 +406,7 @@ 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] = { 0, };
max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
max_zone_pfns[ZONE_NORMAL] = end_pfn;


2007-06-18 10:27:47

by Robert P. J. Day

[permalink] [raw]
Subject: Re: [PATCH] trivial: the memset operation on a automatic array variable should be optimized out by data initialization

On Mon, 18 Jun 2007, Denis Cheng wrote:

> 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.
>
> Signed-off-by: Denis Cheng <[email protected]>
>
> ---
> Is there some comments on this?
>
> --- arch/x86_64/mm/init.c.orig 2007-06-07 10:08:04.000000000 +0800
> +++ arch/x86_64/mm/init.c 2007-06-18 14:43:15.000000000 +0800
> @@ -406,8 +406,7 @@ 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] = { 0, };

the drawback i see to this is that it's *visually* misleading. it
*appears* that what the programmer is trying to do is just initialize
the first element, not all of them -- regardless of the actual effect.

rday
--
========================================================================
Robert P. J. Day
Linux Consulting, Training and Annoying Kernel Pedantry
Waterloo, Ontario, CANADA

http://fsdev.net/wiki/index.php?title=Main_Page
========================================================================

2007-06-18 14:23:20

by Cong Wang

[permalink] [raw]
Subject: Re: [PATCH] trivial: the memset operation on a automatic array variable should be optimized out by data initialization

On Mon, Jun 18, 2007 at 06:25:17AM -0400, Robert P. J. Day wrote:
>On Mon, 18 Jun 2007, Denis Cheng wrote:
>
>> 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.
>>
>> Signed-off-by: Denis Cheng <[email protected]>
>>
>> ---
>> Is there some comments on this?
>>
>> --- arch/x86_64/mm/init.c.orig 2007-06-07 10:08:04.000000000 +0800
>> +++ arch/x86_64/mm/init.c 2007-06-18 14:43:15.000000000 +0800
>> @@ -406,8 +406,7 @@ 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] = { 0, };
>
>the drawback i see to this is that it's *visually* misleading. it
>*appears* that what the programmer is trying to do is just initialize
>the first element, not all of them -- regardless of the actual effect.
>

C standard can guarantee all of them are initialized, and this is _not_
hard to understand for a good C programmer.

Ack for Denis's change.

Regards!

2007-06-18 17:08:35

by Jeremy Fitzhardinge

[permalink] [raw]
Subject: Re: [PATCH] trivial: the memset operation on a automatic array variable should be optimized out by data initialization

Denis Cheng wrote:
> 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.
>

How does the generated code change? Does gcc do something stupid like
statically allocate a prototype structure full of zeros, and then memcpy
it in? Or does it generate a series of explicit assignments for each
member? Or does it generate a memset anyway?

Seems to me that this gives gcc the opportunity to be more stupid, and
the only right answer is what we're doing anyway.

J

2007-06-19 13:27:16

by Adrian Bunk

[permalink] [raw]
Subject: Re: [PATCH] trivial: the memset operation on a automatic array variable should be optimized out by data initialization

On Mon, Jun 18, 2007 at 10:07:21AM -0700, Jeremy Fitzhardinge wrote:
> Denis Cheng wrote:
> > 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.
>
> How does the generated code change? Does gcc do something stupid like
> statically allocate a prototype structure full of zeros, and then memcpy
> it in? Or does it generate a series of explicit assignments for each
> member? Or does it generate a memset anyway?
>
> Seems to me that this gives gcc the opportunity to be more stupid, and
> the only right answer is what we're doing anyway.

I checked with gcc 4.2, and gcc is quite clever:

If an array is big, gcc uses memset.
If an array is small, gcc does it directly in assembler.

> J

cu
Adrian

--

"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

2007-06-21 13:21:39

by cheng renquan

[permalink] [raw]
Subject: Re: [PATCH] trivial: the memset operation on a automatic array variable should be optimized out by data initialization

On 6/19/07, Jeremy Fitzhardinge <[email protected]> wrote:
> Denis Cheng wrote:
> > 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.
> >
>
> How does the generated code change? Does gcc do something stupid like
> statically allocate a prototype structure full of zeros, and then memcpy
> it in? Or does it generate a series of explicit assignments for each
> member? Or does it generate a memset anyway?
>
> Seems to me that this gives gcc the opportunity to be more stupid, and
> the only right answer is what we're doing anyway.
>
> J
>
Technically speaking, C standard guarantees the data be initialized correctly;
just from the point view of code style, let the compiler selects how
to initialize will be better, this could let the compiler has more
optimization points.

--
Denis Cheng
Linux Application Developer