On Mon, May 12, 2014 at 08:06:57AM +0200, Alexander Burger wrote:
> The problem with this is that is horribly inefficient. The dynamic version
> myStruct bnd[length(x)];
> simply decrements the stack pointer by "length(x) * sizeof(myStruct)"
> (which is a single machine instruction!), while the malloc() call
> involves the whole memory management machinery.
BTW, this inability of C to properly support stack manipulations was the
main reason to write the 64-bit version of PicoLisp in assembly.
As I wrote this in http://software-lab.de/doc64/README
The reasons to choose assembly language (instead of C) were, in decreasing
1. Stack manipulations
Alignment to cell boundaries: To be able to directly express the
stack data structures (see "doc64/structures", e.g. "Apply frame"), a
better control over the stack (as compared to C) was required.
Indefinite pushs and pops: A Lisp interpreter operates on list
of unknown length all the time. The C version always required two
the first to determine the length of the list to allocate the necessary
stack structures, and then the second to do the actual work. An
version can simply push as many items as are encountered, and clean up
stack with pop's and stack pointer arithmetics.
Pushing and popping data of unknown length is at the heart of the PicoLisp
interpreter. It is done all the time.
Note that even with arrays of variable length, as in the discussed case:
it is still not optimal, because the interpreter has to call length() on
the list first, before actually processing it. The list needs to be
In a language with proper stack control, you can simply call 'push' in
the loop doing the processing.