-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[API Proposal]: Decimal.GetBits overload without required array or span creation #110713
Comments
Tagging subscribers to this area: @dotnet/area-system-numerics |
This is not aligned with the normal API patterns exposed by the BCL, it's unlikely to pass API review. The standard pattern for allocation free is the following: Span<int> bits = stackalloc int[4];
decimal.GetBits(value, bits); If you really prefer the out pattern style, that can be trivially achieved with your own helper API.
The existing patterns around span are already JIT friendly and the JIT understands where bounds elision and other things can happen. There's potentially "more" the JIT can do beyond what its doing today, but the existing support is generally doing all the right and expected things.
For If you do need to check for something like |
F# does have stackalloc. That statement about not having it is simply untrue |
Right, It would be appropriate to request a helper on the F# side for better safety and ease of use if it is missing |
they closed the proposal in favor of #52065 let bits = Span<int>(NativePtr.toVoidPtr (NativePtr.stackalloc<int> 4), 4)
Decimal.GetBits(value, bits)
printfn "%d" bits.[0]
printfn "%d" bits.[1]
printfn "%d" bits.[2]
printfn "%d" bits.[3] |
What are the patterns to which you're referring? (asking out of genuine ignorance; I haven't interacted much with the runtime repos :) )
Sure; understood. I guess the real thing that irks me is the lack of static type-safety, that's all. I.e. you have to read documentation to know you're supposed to get exactly 4 things out of it (and this will never change), when that knowledge could be encoded in the type system instead. It's more the principal of the thing! :) But it's true that it's so minor that you'd only make this blunder once
I could have sworn I read something in the docs yesterday specifying that
Honestly forgot about that. This closed suggestion was more recent in my mind: fsharp/fslang-suggestions#720 (comment) In summary, I withdraw all of my points except for the one about type safety -- in other words, the same justification for why we prefer individual method arguments rather than just using arrays to represent them (like they sometimes do in dynamic languages where the distinction doesn't matter). (Of course it would be amazing to just provide a tuple return value but I know that's not really the BCL way and therefore I digress.) What are your thoughts on that in particular? |
Assigned to @PranavSenthilnathan to consult with @tannergooding and finish triaging this. |
There are examples for BitConverter.TryWriteBytes(Span, Double) Just like those APIs, I see The correct way to get the logical parts would be to split up the sign and the scale factor from the fourth Line 19 in c75166b
As a public API I would expect this to have a different name. However, given that this can be implemented as a small helper and there are already other APIs usable for most common tasks (like |
Background and motivation
It would be nice to have a Decimal API that just gives me the 4 individual bit parameters, without having to pack them into an array or span just for the caller to pull them right out. Such an overload would also have no possibility of throwing exceptions or causing bounds checks or anything like that, and I would imagine be quite JIT friendly.
Would also be particularly helpful for allocation-free F# code, since F# doesn't have stackalloc. Currently you have to resort either to allocating/pooling an array or span (which feels silly because you know exactly how many elements there are going to be), or relying on decimal's internal representation and doing evil bit casting.
This becomes particularly unfortunate when you need to care about the distinction between -0.0m and +0.0m, as the only way to do that (as of this writing) is to inspect the binary representation.
API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: