umka-lang icon indicating copy to clipboard operation
umka-lang copied to clipboard

Stack escape logic is too aggressive

Open vtereshkov opened this issue 3 years ago • 0 comments

Currently, a stack frame of any function, whose body has at least one ref count increment instruction, is moved from the stack to the heap. The resulting heap frame is ref-counted as a whole, which helps detect memory usage errors like this:

var p: ^int

fn f() {
        a := 5
        p = &a
}                      // Runtime error: Pointer to a local variable escapes from the function

fn main() {
        f()
}

However, this approach has a great performance impact, since it requires additional malloc()/free() calls on each function call, as well as copying function parameters to the heap frame. In practice, about 80-90% of functions are subject to this "stack escape" procedure, even if no pointer assignments are made.

Another, but similar problem is that all array, dynamic array or structure literals are always moved to the heap just after construction.

vtereshkov avatar May 09 '22 10:05 vtereshkov