Notes
2026/11/29

Heap-backed growable lists on the stack

What if never implicitly growing lists on the stack is not good enough because we want to grow lists without having to move the entire stack above it? Could we just keep growable lists on the heap?

Every growable list would then point from the stack to a heap fragment that stores the element indexes as well as the element data of non-atomic elements that are stored inside the list. This is simple, but has one flaw: How do we refer to elements inside the list? Pointing to a stack position is not enough, because the stack merely contains a pointer to the heap fragment, not the full elements. Worse, what if heap-allocated lists store elements that are themselves heap-allocated lists and we want to store a reference to such an inner list on the stack?

We can solve this through a bit of indirection: Let's call the stack slot that contains a pointer to the heap fragment our “heap slot”, and a reference to something inside the heap-allocated list a “heap reference”. A heap reference never points directly into the heap. Instead, it's a reference to the heap slot (which always remains on the stack as long as the list is alive) + an offset into the heap fragment (which we can think of as a stack reference into the heap fragment viewed as its own little stack).

A reference to an element of a heap-allocated list inside of another heap-allocated list can never be a direct reference, because it can only refer to an offset into a heap fragment that is directly reachable from the stack. But if another such reference sits at that offset, it effectively forms a reference to the element of the list inside the list, at the cost of requiring a pointer chase for every nesting level of heap-allocated lists.

       stack                    outer fragment        inner fragment
+----> 00: HEAP @-------------> 0: HEAP @-----------> [10, 20]
|                               1: HEAP @-----------> 0: 30
|      01: ...                                        1: 40
|                                                     2: 50
+----@ 02: HREF, offset 1  <--+
       03: HREF, offset 2  @--+

Growing a heap-allocated list is then easy, because only the heap slot points to the actual heap fragment, whereas all heap references point to the heap slot. We thus only need to switch out the heap slot and point to a larger heap fragment instead.

The idea that any active references to a heap fragment must remain materialized on the stack where they are used is the fundamental principle of this heap-backed approach. Each heap fragment can then be viewed as a stack of its own, where the same principle applies and every reference to a nested heap-allocated list is again materialized as a reference on the parent heap fragment.

stack                    outer fragment        inner fragment
00: HEAP @-------------> 0: HEAP @-----------> [10, 20]
                    +--> 1: HEAP @-----------> 0: 30
                    |                          1: 40
                    +--@ 2: HREF, offset 2     2: 50

Heap fragments can point to older values in their parent stack/heap, by simply pointing past the bottom of the heap fragment (and then logically continuing the offset at the stack location where the heap slot sits). This means that mark-and-compact GC has to walk the stack and (nested) heap fragments recursively. Since heap fragments still follow the stack-based model of nested scopes and each heap fragment only needs to be checked once, we can store a return address to the parent stack/heap per heap fragment, which tells us where to continue the mark-and-compact.

There's one other extremely appealing benefit for regions: Heap-allocated regions can grow without having to either move the entire stack or devolve into no-ops because we can simply reallocate and thus grow their underlying heap fragment. Stack-based regions could still be provided for cases where we know the maximum size in advance, but heap-based regions are probably more useful in practice.