4 ms·
What they are saying is the stackalloc approach causes no GC pressure. You can run that in a tight / infinite loop with essentially no downside. Using a regular
by osigurdson 1y ago
What they are saying is the stackalloc approach causes no GC pressure. You can run that in a tight / infinite loop with essentially no downside. Using a regular heap allocated array in the same situation will hammer the GC.
C# doesn't do escape analysis and automatically put things on the stack like (for example) Go does. However the stack size is limited in C# so I wouldn't suggest going too crazy with stackalloc and deep stacks / recursion. You don't want a stack overflow!
- ygra 1y ago> C# doesn't do escape analysis and automatically put things on the stack There has been work ongoing in this direction since .NET 9 at least, but the effect is very limited currently. The following code however, has no allocations at runtime, despite having an object creation in the code: https://sharplab.io/#v2:C4LghgzgtgPgAgJgIwFgBQcDMACR2DC2A3utmbjgJYB2w2AYgPaMAUAlMaedwG5gBO2MNgC82agFMA7thrAA2gHYAugG4u3MnEVCAdABkJ1AObAAFurTcAvumtA= https://sharplab.io/#v2:C4LghgzgtgPgAgJgIwFgBQcDMACR2DC2A3ut...
- Izikiel43 1y ago> C# doesn't do escape analysis In net9 they started on that, and on net10 they have improved it, you can check the performance blogpost by Stephen toub for net10 for more info