gnolang / gno

Gno: An interpreted, stack-based Go virtual machine to build succinct and composable apps + Gno.land: a blockchain for timeless code and fair open-source
https://gno.land/
Other
877 stars 359 forks source link

[gno] synchronous garbage collection #266

Open jaekwon opened 2 years ago

jaekwon commented 2 years ago

Currently the memory allocator doesn't account for any GC freed objects. That is, objects like large strings or arrays each count against the allocation limit, even for temporary objects that get thrown away after the scope of the function call.

func SomeFunction() {
    x := ""
    for i:=0; i<100; i++ {
        x += "."
    }
}

For example, the above variable x returned is 100 bytes long, but all the prior subsequent strings were also accounted for, from x length 1, 2, 3, .. to length 100. Even though all prior x's become garbage collected, gno's memory management is not yet that smart.

This is a feature request issue to add some kind of synchronous garbage collection mechanism to free up GC'd memory. This could happen between realm function call boundaries, or it could happen with some special system function call. Or maybe this is best solved with function comment directives.


update

Garbage collection can be implemented synchronously upon memory alloc failure (before it returns failure) to scan all stack and block local variables to see what is still alive (and then decrement the total allocated number accordingly).

Two points:

jaekwon commented 1 year ago

clearing this just so anyone ambitious and familiar with the gno VM can try their hand at implementing. please definitely get my feedback with some high level spec before actual implementation. add me as reviewer in the PR.