next up previous contents index
Next: Local Tail Recursion Up: Local Call Previous: Let Calls   Contents   Index


Closures

closures

Local call allows for much more efficient use of closures, since the closure environment doesn't need to be allocated on the heap, or even stored in memory at all. In this example, there is no penalty forlocalfun referencing a and b:

(defun foo (a b) (flet ((localfun (x) (1+ (* a b x)))) (if (= a b) (localfun (- x)) (localfun x))))
In local call, the compiler effectively passes closed-over values as extra arguments, so there is no need for you to ``optimize'' local function use by explicitly passing in lexically visible values. Closures may also be subject to let optimization (see section let-optimization.)

Note: indirect value cells are currently always allocated on the heap when a variable is both assigned to (with setq or setf) and closed over, regardless of whether the closure is a local function or not. This is another reason to avoid setting variables when you don't have to.



Peter Van Eynde 2001-03-08