Skip to content

cmd/compile: don't generate call to growslice if the slice has capacity #33853

Closed
@mariecurried

Description

@mariecurried

What version of Go are you using (go version)?

$ go version
go version devel +739123c Sun Aug 25 00:27:25 2019 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What did you do?

I compiled these three versions of the same function: https://godbolt.org/z/8UTTBt.
Here is one of them:

func copySlice1(x []byte) []byte {
	res := make([]byte, 0, len(x))
	for i := 0; i < len(x); i++ {
		res = append(res, x[i])
	}
	return res
}

What did you expect to see?

I didn't expect that these functions would't have code to call growslice.

What did you see instead?

Instead, said code was generated, as if the slices don't have capacity for the new elements.

I found this while compiling the following function (https://godbolt.org/z/FFCJGe), but figured it also happened for the simpler cases above:

func joinSlices(x, y []byte) []byte {
	res := make([]byte, 0, len(x)+len(y))
	for i := 0; i < len(x); i++ {
		res = append(res, x[i])
	}
	for j := 0; j < len(y); j++ {
		res = append(res, y[j])
	}
	return res
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions