I noticed that the capacity of slices behaves in a different way, when the capacity is an odd number. More specifically: When an element is added to a slice, the capacity of the slice is doubled when the original capacity was an even number. But when the original capacity was an odd number, the capacity is incremented by one and then doubled. Example:
s := make([]int, 28, 28)
s = append(s, 1)
fmt.Println("len=", len(s), " cap=", cap(s)) // len = len + 1, cap = 2 * cap
pri := make([]int, 27, 27)
pri = append(pri, 1)
fmt.Println("len=", len(pri), " cap=", cap(pri)) // len = len + 1, cap = 2 * (cap + 1)
Assuming this is not a bug, what's the reason for this behavior?
Link to playground: http://play.golang.org/p/wfmdobgCUF
Short answer
It is rounding up the slice capacity to fill the allocated memory blocks.
Long answer
Let's have a look into the Go1.5.1 source code :
https://github.com/golang/go/blob/f2e4c8b5fb3660d793b2c545ef207153db0a34b1/src/cmd/compile/internal/gc/walk.go#L2895 tells us that
append(l1, l2...)
is expanded toThe part we are interested in,
growslice_n
, is defined there : https://github.com/golang/go/blob/f2e4c8b5fb3660d793b2c545ef207153db0a34b1/src/runtime/slice.go#L36Going a bit deeper, we find this :
roundupsize
is defined there : https://github.com/golang/go/blob/f2e4c8b5fb3660d793b2c545ef207153db0a34b1/src/runtime/msize.go#L178And it was introduced there : https://groups.google.com/forum/#!topic/golang-codereviews/bFGtI4Cpb_M