14

I have a question about the below code

package main

import "fmt"

func main() {
    var a []int
    printSlice("a", a)

    // append works on nil slices.
    a = append(a, 0)
    printSlice("a", a)

    // the slice grows as needed.
    a = append(a, 1)
    printSlice("a", a)

    // we can add more than one element at a time.
    a = append(a, 2, 3, 4)
    printSlice("a", a)
}

func printSlice(s string, x []int) {
    fmt.Printf("%s len=%d cap=%d %v\n",
    s, len(x), cap(x), x)
}       

I always guess what the result of running a piece of code will look like then run the code and check if my guess is correct. But this code resulted a little bit different from my guess:

Result:
On my local go tour server:

a len=0 cap=0 []
a len=1 cap=1 [0]
a len=2 cap=2 [0 1]
a len=5 cap=6 [0 1 2 3 4]      

Everything is ok until the last line but I don't get

cap=6     

why not

cap=5    

My opinion is I did not create slice with explicit capacity therefore my system gave it this value of 6.

2) But when I tried this same code on the golang tour server I get a little more diffferent result like this :

a len=0 cap=0 []
a len=1 cap=2 [0]
a len=2 cap=2 [0 1]
a len=5 cap=8 [0 1 2 3 4]   

What about cap=2 on the second line and cap=8 on the last line?

3
  • 7
    Are you running the same version as the tour server? How append extends capacity isn't defined by the language spec, and the implementation can change.
    – JimB
    Jul 25, 2016 at 17:28
  • Hmm I figured out this when I append items to the slice in the way that dont get over old slice capacity capacity does not change but when it gets over capacity it rearranges the slice. Jul 25, 2016 at 17:34
  • 1
    Well I would say, go tries to increase your capacity a little more than your length value if length, and when you cross the capacity max limit, by appending a new value, it adds multiples of 2 to the capacity value, and so it always ends with an even number to make sure to reduce operation of copies. This is with go version 1.6
    – giri-sh
    Jul 25, 2016 at 18:57

2 Answers 2

17

This question isn't an exact duplicate, but my answer here also effectively answers this.

TL;DR — how much the capacity of the slice is expanded by isn't mentioned in the specification and different versions of Go (or different implementations, or the same version on different architectures, etc.) can expand the slice by different amounts.

The reason you might want to make the capacity larger than you need is because underneath the slice there is an array which is immutable (it can't be expanded). When you "grow" a slice what actually happens is that you make a new (longer) array, copy all the values over, and then set that as the backing array for the slice. If you were appending lots of values, you'd have to do lots and lots of copies (one for every single value), which would be very slow, so instead the runtime allocates more space than it thinks you need so that it has to make copies less often.

3
  • good explanation but i think the question is more focused on why one would get a different result on their local machine then the same code running in the tour playground. golang version issue???
    – GregL83
    Apr 9, 2017 at 16:36
  • 1
    See the first paragraph of my answer; this could change for many reasons. I'm not sure that the specific reason matters, but you could always test it against the playground, and the same version locally to narrow it down. (FWIW I see this same behavior right now when I swap back to 1.8 which is what the playground is running, so it's not the version).
    – user1087001
    Apr 9, 2017 at 21:57
  • As for "why" - I would imagine that an implementation might grow the capacity liberally when it's convenient to do so (like the case of a small initial slice that fits in the cache), while it could be much more conservative in other cases (little memory left)
    – hmijail
    Oct 12, 2020 at 2:40
2

In fact, after the cap capacity is calculated, a roundupsize() function will be run, which will calculate the real cap due to memory alignment considerations.

newcap := old.cap
doublecap := newcap + newcap
if cap > doublecap {
    newcap = cap
} else {
    if old.cap < 1024 {
        newcap = doublecap
    } else {
        for 0 < newcap && newcap < cap {
            newcap += newcap / 4
        }
        if newcap <= 0 {
            newcap = cap
        }
    }
}
......
capmem = roundupsize(uintptr(newcap) * sys.PtrSize)
newcap = int(capmem / sys.PtrSize)

In this example, the size after the first expansion is expected to be sizeof(int64)*5=40, and then the first value greater than or equal to this number will be found in the array below, here is 48, so the true cap is 48 /sizeof(int64)=6

class_to_size = uint16{0, 8, 16, 24, 32, 48, 64, 80, 96,112, 128, 144, 160, 176, 192, 208, 224,...

As for why the results of the two runs are different, I guess it should be related to the operating system. For more details, see https://github.com/golang/go/blob/master/src/runtime/slice.go#L164 https://github.com/golang/go/blob/master/src/runtime/msize.go

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.