Re: Global versus Stack variables

"oliverhunt@gmail.com" <oliverhunt@gmail.com>
21 Nov 2005 22:40:47 -0500

          From comp.compilers

Related articles
Global versus Stack variables shreyas76@gmail.com (shrey) (2005-11-19)
Re: Global versus Stack variables oliverhunt@gmail.com (oliverhunt@gmail.com) (2005-11-21)
Re: Global versus Stack variables gah@ugcs.caltech.edu (glen herrmannsfeldt) (2005-11-21)
Re: Global versus Stack variables henry@spsystems.net (2005-11-21)
Re: Global versus Stack variables torbenm@app-5.diku.dk (2005-11-21)
Re: Global versus Stack variables jatin.bhateja@amdocs.com (Jatin Bhateja) (2005-11-26)
Re: Global versus Stack variables napi@cs.indiana.edu (2005-11-26)
Re: Global versus Stack variables david.thompson1@worldnet.att.net (Dave Thompson) (2005-12-05)
| List of all articles for this month |

From: "oliverhunt@gmail.com" <oliverhunt@gmail.com>
Newsgroups: comp.compilers
Date: 21 Nov 2005 22:40:47 -0500
Organization: http://groups.google.com
References: 05-11-094
Keywords: storage, optimize, comment
Posted-Date: 21 Nov 2005 22:40:47 EST

shrey wrote:
> Hi all
> I was wondering if ppl can give their opinion on the following
> ...


There are a number of factors that effect the performance of locals and
globals:


    * if you are targetting an architecture with enough registers (eg.
not an x86) it becomes possible to keep many locals inside registers,
and reduce the number of memory accesses, etc.


    * Dependency and data flow analysis is more practical with local
variables as the compiler doesn't need to worry about the contained
values changing between function calls (same holds for concurrent
modification of variables). C bypasses these problems by assuming
everything is is effectively local (during dependency, etc analysis)
unless a var is declared volatile.


    * It may be possible to read a fixed address global faster than a
local depending on architecture, but i would imagine the cache in most
architectures would destroy that advantage nowadays.


There are other things that effect performance, but the first two alone
should give locals a reasonable performance advantage (in my opinion
anyway :) )


> [It depends on your language, but in languages with pointers, the
> biggest advantage of stack locals is that the compiler can assume
> no aliasing of stack variables that haven't had their address taken.
> -John]
but they shouldn't in the happy world of C anything is possible :)
void foo()
{
    int b[10], c;
    c=5;
    for(int i=0; i<=10; i++) b[i]=4;
    if(c==4) //a good optimizer should probably
        printf("Foo!");
}


Although yes, things like that are *probably* an error (and exceedingly
dependant on the order of vars on the stack) :)


--Oliver
[Things like that are definitely an error if your program purports to be
in ANSI C. -John]


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.