Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mariecurried opened this issue Aug 26, 2019 · 1 comment
Closed

Comments

@mariecurried
Copy link

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
}
@mariecurried mariecurried changed the title cmd/compile: don't call growslice if the slice has capacity cmd/compile: don't generate call to growslice if the slice has capacity Aug 26, 2019
@randall77 randall77 added this to the Unplanned milestone Aug 26, 2019
@dsnet
Copy link
Member

dsnet commented Aug 27, 2019

Duplicate of #30509

@dsnet dsnet marked this as a duplicate of #30509 Aug 27, 2019
@dsnet dsnet closed this as completed Aug 27, 2019
@golang golang locked and limited conversation to collaborators Aug 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants