r/lisp • • 10d ago

Code vs data

Hi, I have been reading up on Lisp for the last couple of days. I come from Java, but have also written a "toy" Forth that compiles to byte code on Arduino.

It seems to me that the memory model of Lisp, if based on CONS cells alone, means a lot of serial traversing. I tried to search for malloc-style memory management in Lisp, and came up mostly empty, with various commentators condemning this approach.

I realize the "code as data" is great for macros, which remind me of immediate words in Forth, but with the added advantage of potentially having the code following it available as a CONS data structure, upon which it can work, as part of the compile process.

Now, compiling Forth is very simple, because Forth is mostly "flat", whereas Lisp is nested, but in the combined interest of speed and readability (where Forth falls short), I wonder if there has been made Lisp implementations with full macro support (on CONS source tree data) but with malloc style data handling, for lookup by offset and memory efficiency?

My Forth targets the atmega328p with 2Kb of SRAM. To a certain extent the stack nature of Forth becomes "bearable" when writing code, but Lisp syntax is much more explicit. I am interested in mapping a Lisp parser/compiler onto the bytecode machine I have defined for my Forth-like language.

15 Upvotes

16 comments sorted by

View all comments

Show parent comment

6

u/arthurno1 9d ago

In a sense. That is what it ends up in, but you can break that down to simpler ideas that are easier to digest.

Point of cons data structure is just to have a linked list. Linked lists in their turn are used to store parsed source code in. Each parsed token becomes a node in the linked list representing an expression, and very few tokens have special meaning. Notably parens stand for start of the list and end of the list.

Once you have the source in the list form, you can than use ordinary list operations to process it like you would process any other data in a list. So there comes "code is data, data is code" from. Admittedl, my first comment, and this one, are a bit of a misnomer, because what I really had in mind was the common form of "cdr-ing" and recursive processing over lists, as we know it in classical Lisp(s).

However, nothing says you have to do so, and there are Lisps, that don't store the source code in cons cells. Clojure is one example. We could do recursion even if source was stored in a different data structure, say a vector:

(defun iterate-array (arr &optional (index 0))
  "Recursively traverse ARR and process each element."
  (when (< index (length arr))
    ;; do something with the element here
    (print (aref arr index))
    (iterate-array arr (1+ index))))

Hopefully, your Lisp dialect have TCO optimization and the above is turned into iterative loop version without the function call cost and stack allocations.

For your considerations about performance, I don't think you should really worry. In modern Lisps, such as Common Lisp in SBCL, performant code typically don't use lists for user data. Lists are typically used mostly for processing the source code itself in macros, and for user data where the performance does not matter. It means you typically use arrays, structures, hashtables and trees, and whatever you might need for your application to be efficient, just like in any other programming language.

SBCL also have an efficient cons allocator, bump allocator, which allocates cons cells on thread local "allocation regions" which short said, store cons cells close to each other, in a contigous regions, so access patern is cache friendly, more like a vector. So for practical purpose it means you don't typically point wildly in the heap, but exploit the cache locality. That is of course specific from case to the case.

For your application needs, where performance matters, you would typically use vectors and defstruct which gives you malloc-like, contigous, O(1) time access pattern.

Processing source code is typically a very small part of the compilation and lists representing top-level forms in a lisp file are typically not very huge that linked-list representation would be a bottleneck. By switching to more compact representation like an array for example, you would get probably just a marginal speed-up if even that, but will have to switch to completely different mental model of source code processing. You will have to give up the classical cdr-recursion model which we know from classical, McCarthian, Lisps, which some people will say is not Lisp any more (RMS was the last one well-known who proclaimed Clojure is not a Lisp, I guess for that particular reason).

The above is of course for a big, modern, "desktop" lisp, like SBCL on x86-64 or Arm. A side note from CMUCL manual:

5.10.1 Think Before You Use a List

Although Lisp’s creator seemed to think that it was for LISt Processing, the astute observer may have noticed that the chapter on list manipulation makes up less that three percent of Common Lisp: The Language II. The language has grown since Lisp 1.5—new data types supersede lists for many purposes.

For a constraint system like yours, Micro Lisp (uLisp), already does something similar as you write about. As I read about, they allocate static array and conses are just indexes into that array. However, I am not familiar with uLisp, I looked once at it, more for the curiosity, so I can't tell you much about. Look at the sources, or use it for a while, see what they do and adapt their techniques for your lisp.