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

Comments

Projects
None yet
4 participants
@petermattis
Copy link

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

This comment has been minimized.

Copy link
Contributor

commented Jan 31, 2018

Want to send a change for 1.11?

@josharian josharian added this to the Go1.11 milestone Jan 31, 2018

@petermattis

This comment has been minimized.

Copy link
Author

commented Jan 31, 2018

@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.

@gopherbot

This comment has been minimized.

Copy link

commented Jan 31, 2018

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

@gopherbot

This comment has been minimized.

Copy link

commented Feb 18, 2018

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

@gopherbot gopherbot closed this in dfb0e4f Feb 19, 2018

@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.
You can’t perform that action at this time.