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: optimize append([]byte(string), ...) to avoid a duplication #47454

Open
go101 opened this issue Jul 29, 2021 · 2 comments
Open

cmd/compile: optimize append([]byte(string), ...) to avoid a duplication #47454

go101 opened this issue Jul 29, 2021 · 2 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Milestone

Comments

@go101
Copy link

go101 commented Jul 29, 2021

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

$ go version
go version go1.17rc1 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

package main

import "testing"

var str = "1234567890123456789012345678901234567890"

func f() {
	_ = append([]byte(str), str...)
}

func main() {
	stat := func( f func() ) int {
		allocs := testing.AllocsPerRun(1, f)
		return int(allocs)
	}
	println(stat(f)) // 2
}

What did you expect to see?

One allocation.

What did you see instead?

Two instead.

The allocation for []byte(str) could be saved.

Partially related to #43429.

@randall77 randall77 added this to the Unplanned milestone Jul 29, 2021
@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 30, 2021
quasilyte added a commit to quasilyte/go-perfguard that referenced this issue Mar 20, 2022
For hot paths (o2 level) suggest this rewrite:

	append([]byte(str1), str2...)
	=>
	append(append(make([]byte, 0, len(str1)+len(str2)), str1...), str2...)

This will make the operation work with only 1 allocation instead of 2.

See golang/go#47454
quasilyte added a commit to quasilyte/go-perfguard that referenced this issue Mar 20, 2022
For hot paths (o2 level) suggest this rewrite:

	append([]byte(str1), str2...)
	=>
	append(append(make([]byte, 0, len(str1)+len(str2)), str1...), str2...)

This will make the operation work with only 1 allocation instead of 2.

See golang/go#47454
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
@earthboundkid
Copy link
Contributor

Interestingly, append([]byte(nil), str...) and append([]byte(str), 'a') both only do one alloc, so it's only the combination which the compiler fails to optimize.

@go101
Copy link
Author

go101 commented Jun 8, 2023

If len(str) > 32, then there will be two allocs,

Also see #56353 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Projects
None yet
Development

No branches or pull requests

5 participants