Path: utzoo!mnetor!uunet!actnyc!djs From: djs@actnyc.UUCP (Dave Seward) Newsgroups: comp.lang.misc Subject: Re: Var scoping in Wirth-type languages (was: Poor Algorithms) Message-ID: <729@actnyc.UUCP> Date: 12 Mar 88 20:45:22 GMT References: <3821@ihlpf.ATT.COM> <2791@enea.se> <3949@ihlpf.ATT.COM> Reply-To: djs@actnyc.UUCP (Dave Seward) Organization: InterACT Corporation Lines: 38 In article <3949@ihlpf.ATT.COM> nevin1@ihlpf.UUCP (00704a-Liber,N.J.) writes: >Globals, unless implemented with the optimization I'm going to state later, >are much less efficient than using locals! If I didn't make that point >clear in my last posting, please excuse me. > >For every level deep that a variable is used after it is declared, one >climb up the static chain of activation records (I think this is the right >term; it's been a long time since I took an implementation of PLs class) is >required to access the variable >... >level 3 declaration). In order for p3() to access var a1, 2 climbs on the >static chain are required. This is in contrast to p3() accessing a3, which >require no climbing of the static chain. > >If some vars are declared 'globally' (at the topmost level), it is very >inefficient to access those variables in a procedure declared more than 1 >or 2 levels deep. There is a way to implement scoping that alleviates this >inefficiency, however. > This is true in principle, but it doesn't have to be implemented this way. First of all, a static link is a frame pointer for an enclosing activation record and so is a constant for a given invocation of a nested routine. The routine may therefore obtain the value once (through a static link chain walkback, or however) and then reuse it any number of times, even across calls. Some implementations create an array of static links, subscripted by the lexical level, and pass it into a routine, so that only a single dereference is required to access an outer level variable, regardless of the number of intervening levels. Finally, I believe that most compilers treat variables at the outermost level (usu. 1) as static anyway, so their addresses are constants during the program's execution. An optimizer can determine that a routine is not directly recursively called, and can either place its variables in the enclosing scope, or can at least access the enclosing scope variables directly via its own frame pointer, rather than using the static link. Dave Seward uucp.actnyc.djs