-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: Go 2: spec: allow conversion from *T to *[1]T #45545
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
Comments
I don't think this idea would help the reflect package. This could reduce allocations in the case where we already have the address of a value, and we need a slice. But the reflect package doesn't have the address of a value, it just has a value (an |
For language change proposals, please fill out the template at https://go.googlesource.com/proposal/+/refs/heads/master/go2-language-changes.md . When you are done, please reply to the issue with Thanks! |
@gopherbot please remove label WaitingForInfo |
In Go 1.17 we will introduce a new function (With generics we could introduce a generic function that does this, to push the import of unsafe into that package.) Therefore, this is a likely decline. Leaving open for four weeks for final comments. |
For the record, for a notionally zero-cost operation, this could be made more efficient.
|
No change in consensus. |
To go along with the already accepted #395, I would love to see support for converting/aliasing a pointer of some type, to a single-element array pointer of the same type.
The main use-case would be passing an already-allocated single item to a function that accepts a slice of those items, avoiding an extra allocation and copy.
Another place this might be useful is in the reflect package, which mentions issue #2320.Edit: Below are my answers to the questions from the Go 2 language change template.
Experienced.
None quite as much as Go at this point, but (to varying degrees): Rust, Zig, Ruby, C, Erlang, and Haskell.
It wouldn't make it easier, but I don't think it would make it harder either, unless you count learning obscure features of a language.
I'm not aware of any proposal exactly like this, but there are some related (and already accepted) proposals. Primarily #395, and to some extent #19367. The former was the inspiration for this proposal, and the latter would allow this to be done with less-nasty
unsafe
code than before.The difference here is that Go would allow converting between these pointer types without
unsafe
, since there shouldn't really be anything unsafe about it.This proposal helps in performance-sensitive code where allocations are undesirable. When you already have a pointer to a heap-allocated value, and you need a slice (because a function signature demands it), you can avoid allocating space for a single-element slice and copying the value to it.
The only way to do this now is by using unsafe. Alternatively, you could work with a
*[1]T
instead of a*T
from the beginning, but that isn't very ergonomic, and wouldn't be a viable option in the public interface of a library.The proposal is to allow unsafe-free conversion (or aliasing) of a pointer of some type, to an array pointer with a single element of the same type. You could then slice that array pointer.
Alternatively, we could allow converting to a slice directly (since
*[1]T
is rarely useful by itself), but that seems less general.I'm not sure how to answer this any differently from my answer to the previous question.
Probably just adding a new item in the Conversions section where says:
Yikes, I'll have to think about this one a bit.
Yes. All code that was valid before will remain valid. Some code that would have required
unsafe
before will no longer require it.Before:
After:
Yes, it's longer and uglier, but also doesn't allocate.
I don't really know. I haven't implemented a language change and I'm not very familiar with the code that would need to be changed in the compiler. The documentation cost should be relatively small.
I think the compiler would have given an error before this change, and thus
vet
would not likely need any logical changes. Beyond that, I'm not really sure.It should be negligible.
Where it is used, it can speed up execution slightly by avoiding unnecessary allocations. Otherwise, it has no impact.
Nope. I'm just not familiar enough with the compiler.
No, but you can achieve this with some rather nasty unsafe right now.
I think I already answered this question above.
The change would overlap somewhat with #19367 (accepted proposal, not exactly existing), except that it wouldn't require
unsafe
.Yes. Strictly performance.
Fewer allocations and copying. It would be more noticeable if
T
is large.A
go test
micro-benchmark.No.
No.
The text was updated successfully, but these errors were encountered: