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



From: Denis Cheng <crquan@xxxxxxxxx>

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.

Signed-off-by: Denis Cheng <crquan@xxxxxxxxx>

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

On 6/18/07, Jan Engelhardt <jengelh@xxxxxxxxxxxxxxx> wrote:
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:

--- arch/x86_64/mm/init.c.orig 2007-06-07 10:08:04.000000000 +0800
+++ arch/x86_64/mm/init.c 2007-06-23 13:12:26.000000000 +0800
@@ -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;
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



Relevant Pages

  • Re: [PATCH] trivial: the memset operation on a automatic array variable should be optimized out by d
    ... the explicit memset call could be optimized out by data initialization, ... hard to understand for a good C programmer. ...
    (Linux-Kernel)
  • Re: Storage of Variables
    ... code at entry to a function zero out the stack. ... Local variables are stack based. ... think), the C Standard, Paragraph A8.7 Initialization has this to say: ... The stack and the data segment are generally identical. ...
    (comp.os.linux.misc)
  • Re: Interview question: variable initialisation
    ... So what is the rule in an embedded system, ... expect all un-initialised *global* variables to be zero ... The rule in _embedded_ systems is that the user can expect the startup code to do whatever the startup code happens to do, and if the user needs the startup code to act in some predictable way he'd darn well make sure of it before he stakes his reputation on it. ... Note that I wouldn't even trust uninitialized variables to be zero -- I've worked with systems that took short cuts around initialization and left 'bss' random. ...
    (comp.arch.embedded)
  • Re: Static-duration array objects beefing up the executable file size
    ... while .bss is initialized to zero by the operating system. ... Naturally the compiler will arrange that any object that's initialized ... duration which get the default zero initialization. ...
    (comp.lang.c)
  • Re: Force menubar visualization
    ... >My question in the thread called " Fast dialog visualization" was how to ... >display quickly the dialog and then, ... doing the data initialization. ...
    (microsoft.public.windowsce.embedded.vc)

Quantcast