Why does `append(x, x...)` copy the slice into a new backing array in Go?

PHPz
Release: 2024-02-10 11:12:23
forward
978 people have browsed it

为什么 `append(x, x...)` 将切片复制到 Go 中的新支持数组中?

php editor Youzi will answer a common question for everyone: Why does `append(x, x...)` copy the slice to the new support array in Go? In the Go programming language, the `append` function is used to append elements to a slice. When we use the `append` function, if the slice capacity is insufficient, Go will create a new underlying array and copy the elements in the original slice to the new underlying array. This is because in Go, a slice is a reference to a dynamic array. When the slice capacity is not enough, a new array must be created to accommodate more elements. This mechanism ensures the continuity and scalability of slices, but also brings some performance losses.

Question content

In go's slicing tips wiki and go libraries (such as this example), you will sometimes see code similar to the following for copying a slice to a new backing array middle.

// In a library at the end of a function perhaps...
return append(whateverSlice[:0:0], whateverSlice...)

// In an assignment, as in the wiki example...
b = append(a[:0:0], a...)
Copy after login

Here's what I think I understand:

  • All items in the slice as the second argument to append will be copied to the new backing array.
  • In the first parameter of append, the code uses a full slice expression. (We could rewrite the first parameter as a[0:0:0], but if omitted, the first 0 will be provided. I think this is more consistent with here The big meaning has nothing to do with it.)
  • According to the specification, the generated slice should be of the same type as the original slice, and the length and capacity should be zero.
  • (Again, not directly related, but I know you can use copy instead of append and it reads clearer.)

However, I still don't quite understand why the syntax append(someslice[:0:0], someslice...) creates a new backing array. I was also initially confused as to why the append operation didn't mess up (or truncate) the original slice.

Now my guess:

  • I assume all of these are necessary and useful because if you just assign newslice := oldslice then changes to one will be reflected in the other. Normally, you don't want this.
  • Because we are not assigning the result of append to the original slice (which is normal in go), nothing happens to the original slice. It is not truncated or altered in any way.
  • Since the length and capacity of anyslice[:0:0] are zero, if go wants to assign the elements of anyslice to the result, a new support must be created array. Is this the reason to create a new backing array?
  • What happens if anyslice... has no elements? A snippet on the go playground shows that if you use this additional trick on an empty slice, the copy and the original initially have the same backing array. (Edit: As the commenter explained, I misunderstood the snippet. The snippet shows that both items are initially the same, but neither supports arrays. They both point to the original is a universal zero value.) Since both slices have zero length and capacity, when you add anything to one of the slices, that slice gets a new backing array. So I guess, the effect is still the same. That is, append After copying, the two slices cannot affect each other.
  • This other playground snippet shows that if the slice has more than zero elements, the append copy method immediately generates a new backing array. In this case, the two resulting slices are immediately separated, so to speak.

I'm probably overly worried about this, but I'd like a fuller explanation of why append(a[:0:0], a...) The trick works is so.

Solution

Since the length and capacity of anySlice[:0:0] are zero, if Go wants to assign the elements of anySlice to the result, it must create a new backing array . Is this why the new backing array was created?

Because the capacity is 0, yes.

https://pkg.go.dev/[email protected]#append

If there is sufficient capacity, the target will be resliced ​​to accommodate the new elements. If not, a new underlying array will be allocated.

  • cap=0 is not enough for non-empty slices, a new array needs to be allocated.

The above is the detailed content of Why does `append(x, x...)` copy the slice into a new backing array in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!