Skip to content

spec: append doesn't specify zero values when growing underlying array #56684

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

Closed
griesemer opened this issue Nov 10, 2022 · 6 comments
Closed
Assignees
Labels
Documentation Issues describing a change to documentation. FrozenDueToAge
Milestone

Comments

@griesemer
Copy link
Contributor

When append is called on a slice and the underlying array is not large enough, a new underlying array is allocated, possibly increasing the slice's capacity significantly (typically doubling it). The spec doesn't specify the value of any slice elements past the slice length and up to the capacity.

@griesemer griesemer added the Documentation Issues describing a change to documentation. label Nov 10, 2022
@griesemer griesemer added this to the Go1.20 milestone Nov 10, 2022
@griesemer griesemer self-assigned this Nov 10, 2022
@zephyrtronium
Copy link
Contributor

I noticed also that the spec doesn't actually say append will allocate an underlying array larger than strictly necessary. All it says is "a new, sufficiently large underlying array that fits both the existing slice elements and the additional values." It doesn't specify the values of extra elements because it doesn't specify that there may be extra elements.

@griesemer
Copy link
Contributor Author

griesemer commented Nov 10, 2022

Yes, that part is on purpose - we don't want to be too specific and constrain an implementation. But we should probably say that the array may possibly be larger than absolutely necessary.

@dsnet
Copy link
Member

dsnet commented Nov 10, 2022

Related to #56086? Or rather, #56086 is dependent on this since slices.Grow is implemented in terms of append.

@dsnet
Copy link
Member

dsnet commented Nov 10, 2022

When I was thinking about #56086, I came across this issue, and I think the Go spec does guarantee it.

Appending to and copying slices says:

If the capacity of s is not large enough to fit the additional values, append allocates a new, sufficiently large underlying array

The zero value says:

When storage is allocated for a variable, either through a declaration or a call of new, or when a new value is created, either through a composite literal or a call of make, and no explicit initialization is provided, the variable or value is given a default value. Each element of such a variable or value is set to the zero value for its type.

I interpreted the list in "the zero value" section as non-exhaustive and that allocations from append fell under this category.

@griesemer
Copy link
Contributor Author

@dsnet I agree. The thought occurred to me as well, afterwards. Perhaps all that's needed to make this more explicit is making "allocates" link to the appropriate section.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/452619 mentions this issue: spec: add a link to Allocation section in section on append built-in

@golang golang locked and limited conversation to collaborators Nov 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation Issues describing a change to documentation. FrozenDueToAge
Projects
None yet
Development

No branches or pull requests

4 participants