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 · 0 comments
Open

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

go101 opened this issue Jul 29, 2021 · 0 comments
Labels
NeedsInvestigation Performance
Milestone

Comments

@go101
Copy link

@go101 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 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Performance
Projects
None yet
Development

No branches or pull requests

3 participants