Skip to content

Tail call optimization / Garbage Collection #16

@RyanRio

Description

@RyanRio

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions