-
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
Allow stackalloc span of reference types #38677
Comments
Related / duplicate: #25423 |
When I remove the compiler error it generates the following: CSharp: class C
{
static void Main()
{
Span<string> p = stackalloc string[] { ""Hello World"" };
Console.WriteLine(p[0]);
}
} IL:
This fails when constructing the runtime/src/libraries/System.Private.CoreLib/src/System/Span.cs Lines 108 to 109 in 719c58c
Is this IL safe? If so this will require removing that error from Span, and no runtime changes. |
This IL does not work. Pointers stored in memory allocated using localloc are not tracked by the GC. |
There are really two issues here:
As for As for the call site optimizations these are more complex. The feature is designed such that we could safely stack alloc the The problem though is even if we can do this, should we do this? Yes this saves heap allocations but it also increases stack space. It's a virtual guarantee that if we blindly applied this optimization that we would break some customers because their programs, which previously were running very close to the edge of the stack size, now blew right past it. In chatting with @davidwrighton about this a bit we decided to take a bit of a different approach. We'd move to have the runtime provide stack alloc APIs that the compiler treats as if they were stack alloced but the runtime makes a runtime decision on whether or not to actually stack alloc the memory. The runtime is best setup to determine if a stack alloc is safe or not, or at least it's much better setup than the compiler is. The compiler could then emit calls to this API at the call site of |
This is exactly the API proposed by #25423 . Closing as duplicate. |
I was looking at these old notes from the LDM: https://github.com/dotnet/csharplang/blob/master/meetings/2018/LDM-2018-09-19.md#params-spant
The idea being that allowing for stackallocating an array of reference types, combined with params span, would allow for using params in an allocation free manner. I've contributed language features to Roslyn in the past, and would definitely do the compiler work to make this happen if the runtime work was done. I also think it reasonably likely the LDM would take this, @jaredpar?
The text was updated successfully, but these errors were encountered: