3.2 Making declarations
&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))A
dynamic-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:
dynamic-extent
declarations for the&rest
arguments to be allocated on a stack.&rest
argument by using the following declaration:
(declare (dynamic-extent variable))
disable-stack-lists
; you can explicitly enable it by calling the functionenable-stack-lists
.(defun fixnum-+ (&rest numbers) (declare (dynamic-extent numbers)) (let ((sum 0)) (dolist (number numbers) (incf sum number)) sum))
Generated with Harlequin WebMaker