-
Notifications
You must be signed in to change notification settings - Fork 18k
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
reflect: permit Value.Bytes (but not SetBytes) on addressable byte arrays #47066
Comments
If you have a pointer to an array of bytes in a p := v.Interface().(*[8]byte)[:] (assuming you know In your code, that's |
I don't know the length, though. I want to do this in a package that can run over anybody's types at runtime. |
I see. I think it would be fine to allow |
Is it possible to make |
Edit: I'm definitely more tired than I think. Somehow completely missed the
underneath. I read through both It should also probably be noted that this is pretty easy to workaround, especially in Go 1.17: package main
import (
"fmt"
"reflect"
"testing"
"unsafe"
)
func main() {
v := reflect.ValueOf(&[...]byte{3, 5, 7}).Elem()
var s []byte
n := testing.AllocsPerRun(1024, func() {
s = unsafe.Slice((*byte)(unsafe.Pointer(v.Addr().Pointer())), v.Len())
})
fmt.Println(n)
} |
I like @randall77's suggestion of allow In some code, I have: if t.Kind() == reflect.Array {
b = va.Slice(0, t.Len()).Bytes()
} else {
b = va.Bytes()
} I would like to reduce it to simply: b = va.Bytes() and avoid the allocation that occurs in |
Analyzing the module proxy, I found:
Of the 372 cases of |
Making v.Bytes() work when v is an addressable byte array seems OK. |
This proposal has been added to the active column of the proposals project |
Optimizing the allocation out of All to avoid just letting |
This would unfortunately still not help the common patterns where |
In addition, we should probably update |
SetBytes seems more difficult. |
Change https://golang.org/cl/357331 mentions this issue: |
I mailed out https://golang.org/cl/357331 as a prototype.
I would expect it to panic. The |
That Unlike Would it make more sense for That means you couldn't |
That is in fact subtle, @randall77. |
Agreed. If you really want the alias behavior, you can use
Unlike with |
@randall77 Good point about subtly different behavior for It feels weird that |
Indeed - reading and writing are asymmetrical. |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. 🎉 |
Change https://go.dev/cl/408874 mentions this issue: |
Update #47066 Update #52411 Change-Id: I85139d774c16c9e6d1a2592a5abba58a49338674 Reviewed-on: https://go-review.googlesource.com/c/go/+/408874 Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
I'm trying to get a []byte of an array via reflect, without allocations.
reflect.Value.Bytes says:
I have an array, not a slice. I can slice it, but reflect.Value.Slice call allocates:
(fails with 1 alloc, from Slice)
Perhaps reflect.Value.Bytes could also permit getting a
[]byte
of an array of bytes?Or maybe I'm holding reflect wrong and there's an alloc-free way to do this already.
/cc @josharian
The text was updated successfully, but these errors were encountered: