Skip to content

slices: grow should document zeroness of extended elements #56086

@dsnet

Description

@dsnet

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions