Skip to content
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

Add Span.StackAlloc<T> method #29605

Closed
VBAndCs opened this issue May 20, 2019 · 8 comments
Closed

Add Span.StackAlloc<T> method #29605

VBAndCs opened this issue May 20, 2019 · 8 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Memory
Milestone

Comments

@VBAndCs
Copy link

VBAndCs commented May 20, 2019

C# allows to allocate objects in the stack to be used with Span and Memory:
Span<byte> bytes = stackalloc byte[10];
I suggest to add a StackAlloc method to these classes to allow other languages like VB.NET an F# to do the same:
dim bytes = Span.StackAlloc(of byte)(10)

@FiniteReality
Copy link

This is likely impossible because stackalloc allocates to the current stack frame, which means that you'll be referencing freed/invalid memory as soon as your proposed API returns. To even have a chance at being implemented correctly, this would likely have to be an intrinsic and have compiler detection.

You'd be much better suggesting language features for VB.Net and F# to support stack-allocation a la C#. It would be a lot easier for them to implement it as a language feature than it would be to change the runtime and likely the compiler to detect this pattern.

@tannergooding
Copy link
Member

This would not be impossible, it would just require the runtime to specially recognize the method and to treat it as part of the calling method (this is how the alloca() method works in C/C++ after all).

F# already has support for stackalloc today via their NativePtr.stackalloc<'T> method so you can already get a Span<T> around that stack-allocated memory by calling the appropriate constructor. F# also has inline methods which would allow you to create a helper which calls NativePtr.stackalloc and returns a Span<'T> wrapper over it.

I would think, if something like this was exposed, it would be better suited first as T* Unsafe.StackAlloc<T>(length). You could then just call the appropriate Span<T> constructor if required or consider a Span<T>.StackAlloc method separately.

@stephentoub
Copy link
Member

stephentoub commented May 20, 2019

@VBAndCs
Copy link
Author

VBAndCs commented May 20, 2019

Thanks.
I tried to avoid requesting this feature in VB.NET lang because it will take too long if it ever happened. VB.NET team, or what left of it, of course is aware of new features of C#, so, why should we ask?
MS seems to left VB behind two years ago!

@tannergooding
Copy link
Member

CC. @KathleenDollard

@FiniteReality
Copy link

@tannergooding TIL - However, it still would require runtime changes as you mention, which I'm guessing are harder to justify than language changes.

@Nukepayload2
Copy link

You can use MemoryMarshal.CreateSpan if you want to stackalloc in VB.NET .
https://github.com/Nukepayload2/ryu/blob/db558e00798ed48292f919264f7b99cd7361649e/visualbasic/D2s.vb#L610

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 5.0 milestone Feb 1, 2020
@stephentoub
Copy link
Member

Closing as a dup of #25423.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Memory
Projects
None yet
Development

No branches or pull requests

6 participants