2

Slice length vs. capacity in Go

 1 year ago
source link: https://teivah.medium.com/slice-length-vs-capacity-in-go-af71a754b7d8
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Slice length vs. capacity in Go

1*vg8Zx5ZtONVUpM-9RaJ6Tg.png

TL;DR: The slice length is the number of available elements in the slice, whereas the slice capacity is the number of elements in the backing array, counting from the first element in the slice.

It’s pretty common for Go developers to mix slice length and capacity or not understand them thoroughly. Assimilating these two concepts is essential for efficiently handling core operations such as slice initialization and adding elements with append, copying, or slicing. This misunderstanding can lead to using slices suboptimally or even to memory leaks.

In Go, a slice is backed by an array. That means the slice’s data is stored contiguously in an array data structure. A slice also handles the logic of adding an element if the backing array is full or shrinking the backing array if it’s almost empty.

Internally, a slice holds a pointer to the backing array plus a length and a capacity. The length is the number of elements the slice contains, whereas the capacity is the number of elements in the backing array, counting from the first element in the slice. Let’s go through a few examples to make things clearer. First, let’s initialize a slice with a given length and capacity:

The first argument, representing the length, is mandatory. However, the second argument representing the capacity is optional. Figure 1 shows the result of this code in memory.

1*p3wNR1_4I6dmqdI7han4vA.png

Figure 1 — A three-length, six-capacity slice

In this case, make creates an array of six elements (the capacity). But because the length was set to 3, Go initializes only the first three elements. Also, because the slice is an []int type, the first three elements are initialized to the zeroed value of an int: 0. The grayed elements are allocated but not yet used.

If we print this slice, we get the elements within the range of the length, [0 0 0]. If we set s[1] to 1, the second element of the slice updates without impacting its length or capacity. Figure 2 illustrates this.

1*dCwJuzbb1hr9rFwdlgv1Zg.png

Figure 2 — Updating the slice’s second element: s[1] = 1

However, accessing an element outside the length range is forbidden, even though it’s already allocated in memory. For example, s[4] = 0 would lead to the following panic:

panic: runtime error: index out of range [4] with length 3

How can we use the remaining space of the slice? By using the append built-in function:

This code appends to the existing s slice a new element. It uses the first grayed element (which was allocated but not yet used) to store element 2, as figure 3 shows.

1*VgOSiEteGbEbR5PMbN6a6A.png

Figure 3 — Appending an element to s

The length of the slice is updated from 3 to 4 because the slice now contains four elements. Now, what happens if we add three more elements so that the backing array isn’t large enough?

If we run this code, we see that the slice was able to cope with our request:

[0 1 0 2 3 4 5]

Because an array is a fixed-size structure, it can store the new elements until element 4. When we want to insert element 5, the array is already full: Go internally creates another array by doubling the capacity, copying all the elements, and then inserting element 5. Figure 4 shows this process.

1*O397s5KUJgPsy5rkoOFKyA.png

Figure 4 — Because the initial backing array is full, Go creates another array and copies all the elements.

The slice now references the new backing array. What will happen to the previous backing array? If it’s no longer referenced, it’s eventually freed by the garbage collector (GC) if allocated on the heap. (We discuss heap memory in mistake #95, “Not understanding stack vs. heap,” and we look at how the GC works in mistake #99, “Not understanding how the GC works.”)

What happens with slicing? Slicing is an operation done on an array or a slice, providing a half-open range; the first index is included, whereas the second is excluded. The following example shows the impact, and figure 5 displays the result in memory:

1*y1ppI-M4l09EUZkhG1ZnoQ.png

Figure 5 — The slices s1 and s2 reference the same backing array with different lengths and capacities

First, s1 is created as a three-length, six-capacity slice. When s2 is created by slicing s1, both slices reference the same backing array. However, s2 starts from a different index, 1. Therefore, its length and capacity (a two-length, five-capacity slice) differ from s1. If we update s1[1] or s2[0], the change is made to the same array, hence, visible in both slices, as figure 6 shows.

1*0IavKX5R7gi66HJlkDw0CQ.png

Figure 6 — Because s1 and s2 are backed by the same array, updating a common element makes the change visible in both slices

Now, what happens if we append an element to s2? Does the following code change s1 as well?

The shared backing array is modified, but only the length of s2 changes. Figure 7 shows the result of appending an element to s2.

1*HRsbVQ_AhiKEROTOBHv8Mg.png

Figure 7 — Appending an element to s2

s1 remains a three-length, six-capacity slice. Therefore, if we print s1 and s2, the added element is only visible for s2:

It’s important to understand this behavior so that we don’t make wrong assumptions while using append.

NOTE: In these examples, the backing array is internal and not available directly to the Go developer. The only exception is when a slice is created from slicing an existing array.

One last thing to note: what if we keep appending elements to s2 until the backing array is full? What will the state be, memory-wise? Let’s add three more elements so that the backing array will not have enough capacity:

This code leads to creating another backing array. Figure 8 displays the results in memory.

1*cRm46z-CXwxXRn_h9kq5_Q.png

Figure 8 — Appending elements to s2 until the backing array is full

s1 and s2 now reference two different arrays. As s1 is still a three-length, six-capacity slice, it still has some available buffer, so it keeps referencing the initial array. Also, the new backing array was made by copying the initial one from the first index of s2. That’s why the new array starts with element 1, not 0.

Conclusion

To summarize, the slice length is the number of available elements in the slice, whereas the slice capacity is the number of elements in the backing array. Adding an element to a full slice (length == capacity) leads to creating a new backing array with a new capacity, copying all the elements from the previous array, and updating the slice pointer to the new array.

About the author

https://twitter.com/teivah

1*umIokvsrxpTkZNu6jN6ZGQ.png

This post is taken from my book, 100 Go Mistakes and How to Avoid Them, which was released in August 2022 (mistake #22):

100 Go Mistakes and How to Avoid Them shows you how to replace common programming problems in Go with idiomatic, expressive code. In it, you’ll explore dozens of interesting examples and case studies as you learn to spot mistakes that might appear in your own applications.

Save 35% with the code au35har.

Meanwhile, here’s the GitHub repository summarizing all the mistakes in the book: https://github.com/teivah/100-go-mistakes.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK