[API Proposal]: stackalloc inside ref struct #6753
Replies: 10 comments 3 replies
This comment has been hidden.
This comment has been hidden.
-
Your example of a constant-sized stackalloc can already be achieved like this: public ref struct CustomIterator<T> where T: unmanaged
{
private Span<T> _data;
public CustomIterator(Span<T> data)
{
_data = data;
}
} public static void Something()
{
var iterator = new CustomIterator<byte>(stackalloc byte[128]);
} |
Beta Was this translation helpful? Give feedback.
-
This belongs to dotnet/csharplang, where this is effectively a part of #1502. |
Beta Was this translation helpful? Give feedback.
-
This is partially a solution but you dont always want to pass that stackalloc from the outside, cause you may need it for specific calculations and you know exactly the length at compile time inside the ref-struct. RIght now i have such an instance where i dont want outsiders to define what and how large the storage shall be and even then allow that thing outsiders to even utilize. Its nice that you even have the chance to do that, but its really not an all case scenario solution. I would think that, since we can have |
Beta Was this translation helpful? Give feedback.
-
Another flaw with your design is that you would allow potentially evrything to be passed, which can be dangerous. So this is prevented with my api-choice aswell. PS: I shall remove the where T: unmanaged constraint because i need a 3-tuple insidethat enumerator; So i need a fixed-size pool of that, which is sadly not possible with the usual "fixed T[10]" cause it only can contain primitive datatypes. |
Beta Was this translation helpful? Give feedback.
-
It would seem to me that a better solution would be to make this possible then: public ref struct CustomIterator<T> where T: unmanaged
{
private fixed T data[<const size >];
} Plus to be able to treat |
Beta Was this translation helpful? Give feedback.
-
yea fixed does all that, except like u cant use ValueTuples with it |
Beta Was this translation helpful? Give feedback.
-
+1 for the proposal. The problem of the injected-span workaround is ownership. If the ref struct is supposed to own the span, allowing the struct itself to stackalloc the span would guarantee ownership. With an injected span, ownership is not guaranteed, and the injector could mutate the span unexpectedly to the struct. With this workaround you have to rely on documentations to make the ownership intention clear and hoping that your consumers would follow the rules, and the lack of |
Beta Was this translation helpful? Give feedback.
-
The nature of the IL requires that stack allocations only be valid for the length of the method. Hence the workaround of passing it into the constructor. |
Beta Was this translation helpful? Give feedback.
-
As an alternative, you can today do roughly what you’re asking for with public ref struct MyStruct
{
private Store _store;
[InlineArray(42)]
private struct Store
{
private T _element0;
}
} In this case, the stack allocated memory is part of the ref struct, so returning it further up the stack will copy it. |
Beta Was this translation helpful? Give feedback.
-
Background and motivation
You can do a lot of things with it when you can store arrays inside the restricts which are stack allocated and have no need to exist longer than they have to.
API Proposal
API Usage
Alternative Designs
No response
Risks
Not AFAIK
Beta Was this translation helpful? Give feedback.
All reactions