Skip to content

Minimize heap allocations, put as much as possible on the stack. #22

Open
@mattjquinn

Description

For performance reasons, as much as possible should be allocated on the stack. Currently malloc calls are favored to keep the code understandable in its early stages, but in the future stack-only allocations should become a focus.

For instance, rather than malloc arrays we can decrement the stack pointer continually until we reach the end. The number of decrements is the length of the array. We can then apply a monadic function directly to this array, or for dyads, we remember the verb, store the next array, and intelligently operate on the two arrays. As a rough example:

1 + 2 3 4 becomes: sub $sp, store 1 $len1, (remember "+"), store $sp,
sub $sp (3 times), store 3 $len2, then call add 3 times

Arrays of random length, or whose length are unknown at compile time, should have the same thing done; the number of decrements to the stack pointer simply becomes variable and verb code will need to handle it accordingly.

Arthur Whitney seems to use only the stack in his B language interpreter, so maybe get some inspiration there:
http://kparc.com/b/
https://docs.google.com/document/d/1W83ME5JecI2hd5hAUqQ1BVF32wtCel8zxb7WPq-D4f8/edit
https://github.com/tlack/b-decoded

And while this early interpreter for J uses malloc, there should be something to learn here as well:
https://code.jsoftware.com/wiki/Essays/Incunabulum

http://www.jsoftware.com/help/jforc/contents.htm#_Toc191734291

APL: A Glimpse of Heaven: https://news.ycombinator.com/item?id=19325361
K7 Tutorial: https://news.ycombinator.com/item?id=19418570

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions