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
marigonzes opened this issue Aug 26, 2019 · 1 comment
Closed

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

marigonzes opened this issue Aug 26, 2019 · 1 comment
Labels
Milestone

Comments

@marigonzes
Copy link

@marigonzes marigonzes commented Aug 26, 2019

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
}
@marigonzes marigonzes 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

This comment has been minimized.

Copy link
Member

@dsnet dsnet commented Aug 27, 2019

Duplicate of #30509

@dsnet dsnet marked this as a duplicate of #30509 Aug 27, 2019
@dsnet dsnet closed this Aug 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.