3.2 Making declarations

3.2.10 DYNAMIC-EXTENT declarations

You can request stack allocation of&rest arguments by usingdynamic-extent declarations. Allocating&rest list structures on a stack rather than in dynamic storage reduces the amount of dynamic memory used by a program and thus reduces the number of garbage collections.

The list of argument values bound to an&rest argument is normally constructed in dynamic storage. When the function that created the list exits, this dynamic storage is not deallocated until the next garbage collection because the list might be stored in a data structure or be bound to a dynamic variable that survives the function call.

Often, however, the&rest argument has dynamic extent, which means that the list is not accessible after the function call that created it exits. The storage allocated to it is therefore not needed after the function call exits. For example, in the following code the &rest argument is only needed to access the components of the list during the execution of the function:

(defun fixnum-+ (&rest numbers)
  (let ((sum 0))
    (dolist (number numbers)
      (incf sum number))
    sum))

Adynamic-extent declaration informs the Compiler that an &rest argument has dynamic extent and that the storage allocated to it can be released when the function call returns. The Compiler can then generate code that allocates and deallocates the storage for the list on a stack. Lists that are allocated and deallocated on a stack are called stack lists.

You can allocate lists on a stack in the following manner:

You can declare a stack list&rest argument by using the following declaration:

(declare (dynamic-extent variable))
When stack list allocation is enabled, the following code produces a function that sums its fixnum arguments without using dynamic allocation:

(defun fixnum-+ (&rest numbers)
  (declare (dynamic-extent numbers))
  (let ((sum 0))
    (dolist (number numbers)
      (incf sum number))
    sum))


The Advanced User's Guide - 9 SEP 1996

Generated with Harlequin WebMaker