Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gnovm): zero-in Ops and Values correctly in (*Machine).Release #894

Merged
merged 1 commit into from
Jun 15, 2023

Conversation

thehowl
Copy link
Member

@thehowl thehowl commented Jun 14, 2023

$ go doc builtin.copy
package builtin // import "builtin"

func copy(dst, src []Type) int
    The copy built-in function copies elements from a source slice into a
    destination slice. (As a special case, it also will copy bytes from a string
    to a slice of bytes.) The source and destination may overlap. Copy returns
    the number of elements copied, which will be the minimum of len(src) and
    len(dst).

doing copy(m.Ops, opZeroed[:0]) is effectively a no-op, as len(src) == 0 and 0 items will be copied.

@thehowl thehowl marked this pull request as ready for review June 14, 2023 14:13
@github-actions github-actions bot added the 📦 🤖 gnovm Issues or PRs gnovm related label Jun 14, 2023
@thehowl thehowl merged commit 232754d into gnolang:master Jun 15, 2023
@piux2
Copy link
Contributor

piux2 commented Aug 8, 2023

This will not zero the m.Ops and m.Values slice.

The opZeroed and valueZeroed arrays are fixed in size with a length of 1024. However, m.Ops and m.Values are slices, and their length could far exceed 1024. The copy() function, in this context, only zeroes out the first 1024 elements of the slice, leaving the remainder unchanged.

Here's a sample code that illustrates this behavior:
https://go.dev/play/p/JoShYG2Q8lf

Given the above, I don't see a significant advantage to zeroing out the entire slice.
A more effective approach might be to set the slice to nil, allowing the Go runtime to garbage collect the underlying arrays.

Here's why:

The VM machine operates within a transactional context, with its lifecycle managed by this context. The efficiency of interactions with gno.land is governed by the time it takes to process requests and responses within this context. The Go runtime handles the allocation and management of underlying slice memory far better than the manual methods we're attempting here.

Furthermore, there's an oversight: only m.Ops and m.Values are being considered for reset. The machine also retains m.Exprs, m.Stmts, m.Blocks, and m.Frames, all of which are slices. It's crucial to release these as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 🤖 gnovm Issues or PRs gnovm related
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants