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

slices: grow should document zeroness of extended elements #56086

Open
dsnet opened this issue Oct 6, 2022 · 3 comments
Open

slices: grow should document zeroness of extended elements #56086

dsnet opened this issue Oct 6, 2022 · 3 comments
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@dsnet
Copy link
Member

dsnet commented Oct 6, 2022

In the event that Grow grows a slice, what assumptions are we allowed to make about the zeroness of the added elements? The current implementation guarantees that they are zero. Can we document that fact?

I have a loop that looks something like:

var s []T = ...   // possibly a pooled slice
mustZero  := true // we do not know the cleanliness of unused capacity
for hasMore() {
    if len(s) == cap(s) {
        s = slices.Grow(s, 1)
        mustZero = false // TODO: Is it safe to assume extended elements are zero?
    }
    s = s[:len(s)+1]
    e := &s[len(s)-1]
    if mustZero {
        *e = T{} // T may be large, so we want to avoid doing this if possible
    }
    mergeInto(e)
}

As an optimization it relies on slices.Grow extended the slice with zero elements, but this is not a documented guarantee.

\cc @ianlancetaylor

@gopherbot gopherbot added this to the Unreleased milestone Oct 6, 2022
@ianlancetaylor
Copy link
Contributor

My first thought is that we shouldn't assume that Grow will zero out elements between the length and the capacity. In particular, if the capacity is already large enough, I can easily imagine a version of Grow that would leave the slice untouched.

@dsnet
Copy link
Member Author

dsnet commented Oct 7, 2022

I think it's fine to leave the behavior of new[len(old):cap(old)] to be undefined.

The issue is about guaranteeing that new[cap(old):cap(new)] will always be zero initialized. I can't imagine any situation where this wouldn't be zeroed since any newly allocated slice from the runtime (either through make or append) should return zero initialized elements.

@ianlancetaylor
Copy link
Contributor

OK, sure.

@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 7, 2022
@cagedmantis cagedmantis changed the title x/exp/slices: Grow should document zeroness of extended elements x/exp/slices: grow should document zeroness of extended elements Oct 7, 2022
@ianlancetaylor ianlancetaylor changed the title x/exp/slices: grow should document zeroness of extended elements slices: grow should document zeroness of extended elements Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants