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

runtime: gobytes() initializes allocated memory unnecessarily #23634

Closed
petermattis opened this issue Jan 31, 2018 · 4 comments
Closed

runtime: gobytes() initializes allocated memory unnecessarily #23634

petermattis opened this issue Jan 31, 2018 · 4 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Performance release-blocker
Milestone

Comments

@petermattis
Copy link

petermattis commented Jan 31, 2018

I noticed this in the go1.9 sources, though this also applies to go1.10. runtime.gobytes currently uses make([]byte, ...) and then immediately overwrites the data in the slice:

func gobytes(p *byte, n int) []byte {
	if n == 0 {
		return make([]byte, 0)
	}
	x := make([]byte, n)
	memmove(unsafe.Pointer(&x[0]), unsafe.Pointer(p), uintptr(n))
	return x
}

The zero initialization of the slice by make is a small but noticeable performance hit when copying a large chunk of memory via C.GoBytes. The allocation can be replaced with rawbyteslice(n) which already exists for the purpose of allocating an uninitialized []byte.

@josharian
Copy link
Contributor

Want to send a change for 1.11?

@josharian josharian added this to the Go1.11 milestone Jan 31, 2018
@petermattis
Copy link
Author

@josharian Sent https://go-review.googlesource.com/c/go/+/91075. Wasn't sure if you wanted it now or when go1.11 opens, but I'm likely to forget later.

@bradfitz bradfitz added NeedsFix The path to resolution is known, but the work has not been done. release-blocker labels Jan 31, 2018
@gopherbot
Copy link

Change https://golang.org/cl/91075 mentions this issue: runtime: allocate uninitialized in gobytes

@gopherbot
Copy link

Change https://golang.org/cl/94760 mentions this issue: runtime: avoid clearing memory during byte slice allocation in gobytes

@golang golang locked and limited conversation to collaborators Feb 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Performance release-blocker
Projects
None yet
Development

No branches or pull requests

4 participants