-
Notifications
You must be signed in to change notification settings - Fork 18k
unsafe: unsafe.Slice escape to heap #72732
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
Note that both |
This violates I would use the new-ish But that will likely allocate. I'm not sure how you think you can get away without allocation. |
Hi @awalterschulze, quick drive-by comment (and I might not have understood what you are trying to do here), but have you tried using encoding/binary? If the goal is to avoid an allocation, I think you should be able to convert from uint64 to []byte without allocating and without using unsafe or reflect. There's probably a better description somewhere, but see for example #42958, or you could just try it and benchmark (and/or look at the assembly, etc.). |
What does this mean in practice? What are you really trying to do? |
Thank you for all the help, I'll try to answer these comment by comment. So first thank you @gabyhelp for finding Unsafely converting []byte to string, safely? It seems the answer is:
|
tl;dr I am building a validator, which doesn't need a copy of any of the data after parsing is complete. I am building a json parser for a serialization format agnostic validation language.
Here is the full pull request that updates the parser to use unsafe casting to int64 and float64. |
@thepudds if binary
But PutUint64 does require allocating a buffer, that needs to be passed in:
Maybe I misunderstood, what you were trying to say? |
@cuonglm thanks, I didn't know about these rules. I fixed the violation of castToString:
, but unsure how to fix the violation in castFromInt64? |
@randall77 Thank you I didn't know about the rules and thank you for also explaining it a bit more.
I do keep holding onto I do not expect you to check my work, but if you want then this is what |
Actually thanks again @gabyhelp for showing me a safer way to convert bytes to string
|
Go version
go version go1.24.0 darwin/arm64
Output of
go env
in your module/workspace:What did you do?
I see reflect.SliceHeader is deprecated and I am trying to upgrade to unsafe.Slice.
I want to use it to cast from an int64 to a slice of bytes, without allocating any memory on the heap.
I tried upgrading to unsafe.Slice, but it seems that the pointer is leaked to the heap, which is usually not the case with reflect.SliceHeader.
Here is a small reproduction:
I can't seem to record an allocation in isolation, so maybe I did something else wrong here:
But I can reproduce it, if I use it in a larger program and make a single change:
katydid/parser-go-json@16defb4
The assembler analysis is also interesting and shows that it is significantly more instructions to use unsafe.Slice that to use reflect.SliceHeader for doing this type of cast: https://godbolt.org/z/azPebxG79
So I am just wondering if there is better way to cast from int64 to a slice of bytes without allocating any memoory?
Or is there a concern about the performance implications of moving from reflect.SliceHeader to unsafe.Slice?
I am also wondering if there is a new way to cast from a byte slice to string without doing a copy?
This is the old way, that I used to do it:
What did you see happen?
This is also answered in previous question.
What did you expect to see?
This is also answered in previous question.
The text was updated successfully, but these errors were encountered: