quasarbright / PongChamp

Other
1 stars 1 forks source link

Tail call optimization / Garbage Collection #16

Open RyanRio opened 3 years ago

RyanRio commented 3 years ago

There are various levels of garbage collection and/or optimization that can be achieved when calling a function. Currently if you do any recursive call, even one in tail position, like this:

function fac_help(x, acc) {
    __printState__();
    if(x <= 1) {
        return acc;
    } else {
        return fac_help(x - 1, x * acc);
    }
}

function fac(x) {
    return fac_help(x, 1);
}

print(fac(4));

The stack will bloat with all the values of x and acc, even worse, this actually bloats the stack more than if you were to not use an accumulator, as it makes a new stack slot for x and acc every time fac_help is called.

We would like to support internal tail call optimization, if a call is in tail position than we update the stack with it's new values and just call the function again with it's variables (for instance x and acc) still pointing to the same place.

Additionally, temporary values should be garbage collected upon returning from a function.

RyanRio commented 3 years ago

Exposing continuations (#15 ) to the programmer should also allow them to CPS anything (provide a cpser as available tooling?)