-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Open
Labels
DocumentationIssues describing a change to documentation.Issues describing a change to documentation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
Description
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
Labels
DocumentationIssues describing a change to documentation.Issues describing a change to documentation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.