-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Complier should optimize "alloc temporary small object" to "alloc on stack" #2104
Comments
This is a more appropriate request for dotnet/coreclr. The C# and VB compilers have no control over this. Considering we have the ability to create user-defined value types which are directly supported by the runtime, I'm wondering if this optimization would provide less benefits than similar optimizations applied to the JVM (and if so, to what extent). |
I think that means that this optimization could be done by Roslyn, it could generate a Though doing it in the runtime still sounds like a much better option, because the Roslyn-based optimization would probably be quite limited. |
I think his comment was more about the fact that the CLR has value types at all whereas the JVM only has its set of primitives. If you needed a complex type on the stack you at least have that option. Having Roslyn automatically generate both seems like overkill with limited benefit. I suspect that the original request involves any class that already exists. As mentioned, that is simply not possible in the CLR as the only IL opcode to initialize a class handles the allocation for you. Even assuming that the CLR could be modified to support the new variable types and op codes to initialize classes on the stack there are a number of real challenges. For example, you couldn't define the variable of a base type and then instantiate a derived type since they could have different footprints. There's also no practical way of knowing that some method wouldn't want to take that stack reference and slap it into a field somewhere. |
Note that
There are many reasons why it is much more appropriate to do this kind of optimizations at JIT time. |
@HaloFour The JVM doesn't have opcodes for stack allocation either. Instead, a technique known as escape analysis is used to identify classes which can be allocated on the stack instead of on the heap. |
I think this idea need effort of both roslyn and coreclr teams. |
Also most of Linq queries can be allocated on stack.
all this can be converted by Roslyn to stack logic like this:
For all this Roslyn need to store data about parameter usage in functions and function purity. And additionally colorcode all allocations in IDE so we can know we allocate on stack or on GC. |
@Jes28 There's an existing tool called LINQ Optimiser that does optimisations similar to this (although I don't know if it'll do the exact optimisation that you outlined) Either way it would be nicer if it was done automaticially, rather than via a library. |
Yes. This kind of optimization can only happen at JIT level. |
@ygc369 You are welcome to suggest this in dotnet/coreclr |
If a small object's reference is never stored in heap or static variables, (only stored in local variables), it is temporary, the complier should alloc it on stack. Thus, GC pressure would be lower.
The text was updated successfully, but these errors were encountered: