Chicken Scheme internals: the garbage collector (2014)
Let’s consider a silly program which sums up all the numbers in a list, and shows the result multiplied by two:
A naive compilation to C would look something like this (brutally simplified):
This particular implementation probably can’t use a copying garbage collector like CHICKEN uses, because the pointers which store the Scheme objects’ locations would all become invalid. On top of that, generating a lot of intermediate values means generating a lot of garbage, which will trigger many GCs during which a lot of these temporary objects will be cleaned up. However, this program is a lot longer due to one important thing: At the start of each continuation function we first check to see if there’s enough space left on the stack to accommodate the objects this function will allocate.
Source: www.more-magic.net