Search code examples
lispcommon-lispheap-memoryclispstack-memory

DYNAMIC-EXTENT ignored in CLISP?


I read here that "it is permissible for an implementation to ignore" the dynamic-extent declaration in Common Lisp, and I was wondering if it is in fact ignored in the CLISP implementation.

I have tried testing with the following code:

(let ((b (cons 1 2))) 
 (declare (dynamic-extent b)) 
 (list b))

Which returns:

((1 . 2))

My guess is that it is ignored, but I wanted to be sure.

Also, if it is ignored, is there a way for me to explicitly allocate memory to the stack rather than the heap?


Solution

  • > is there a way for me to explicitly allocate memory to the stack rather than the heap?

    No, and you can be thankful of it because it eliminates an entire category of bugs from the programs: there can be no "dangling pointers" to already dead objects that make your program crash.

    Furthermore, with CLISP or similar implementations, you don't need stack-allocated memory because:

    • The garbage collector eliminates short-lived objects quickly, and CLISP's garbage collector consumes usually less than 10% of the CPU time.
    • CLISP's garbage collector is generational, which means that collecting short-lived object is particularly fast. And once a short-lived object has been collected, the next short-lived object will be allocated in the same small memory area - so you have a similar speed improvement (by use of locality) as with stacks.

    Finally, insisting on stack allocation of objects prevents you from freely choosing the programming style that is adapted to your problem. Lisp supports many programming styles: functional, procedural, object-oriented, pattern-based, logic, relational, rule, goal-oriented, and more. By requesting stack allocation, you restrain yourself to functional and procedural programming style; this really does not bring you forward.