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

proposal: slices: add Clear function #60136

Closed
Jorropo opened this issue May 12, 2023 · 5 comments
Closed

proposal: slices: add Clear function #60136

Jorropo opened this issue May 12, 2023 · 5 comments
Labels
Milestone

Comments

@Jorropo
Copy link
Member

Jorropo commented May 12, 2023

// Clear will zero out all elements from s[len(s):cap(s)], this is usefull if they contain
// pointers as they keep alive objects pointed to even if they are past the len.
// By zeroing this range s wont keep alive the objects pointed to anymore.
func Clear[S ~[]E, E any](s S) {
 // Implementation detail, clearer than words to explain imo.
 s = s[len(s):cap(s)]
 var zero E
 for i := range s {
  s[i] = zero
 }
}

Rational Compact* and Delete* have this comment:

// When Compact discards m elements in total, it might not modify the elements
// s[len(s)-m:len(s)]. If those elements contain pointers you might consider
// zeroing those elements so that objects they reference can be garbage collected.

// Delete might not modify the elements s[len(s)-(j-i):len(s)]. If those
// elements contain pointers you might consider zeroing those elements so that
// objects they reference can be garbage collected.

Clear would make it easy to perform this operation.

@gopherbot gopherbot added this to the Proposal milestone May 12, 2023
@Jorropo
Copy link
Member Author

Jorropo commented May 12, 2023

It might be usefull to add a max int parameter which stop clearing once this index is reached if this index is smaller than cap(s) so that this pattern could be used:
Can easily be done by reslicing before calling Clear.

oldLen := len(s)
s = DoDelete(s)
s = DoCompact(s)
s = DoFoo(s)
slices.Clear(s, oldLen)

This is because it can be a reasonable assumption to assume what was already free capacity was already zeroed, what would need to be rezeroed would just be the range between the old len and the new len (since we expect remanents from Delete and Compact to be remaining).

@ianlancetaylor
Copy link
Contributor

I don't yet see a need for a max parameter. Can't you simply write Clear(s[:max]) ?

@Jorropo
Copy link
Member Author

Jorropo commented May 12, 2023

I somehow overlooked that, this would require s[:len(s):max] but yes that solves the problem of over zeroing traling capacity.

@magical
Copy link
Contributor

magical commented May 12, 2023

There is already a builtin clear function coming in Go 1.21 (#56351, 4fe46ce). It zeroes the range 0:len(s) instead of len(s):cap(s) but you can always write clear(s[len(s):cap(s)]) to do that. Do we really need slices.Clear too?

@Jorropo
Copy link
Member Author

Jorropo commented May 12, 2023

sgtm ty

@Jorropo Jorropo closed this as not planned Won't fix, can't repro, duplicate, stale May 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants