Proposal: Removing C# stack copies syntactically via (Out-Returns)


If anyone here uses a heavy amount of vectors or math in their games or frameworks and would like to see a significant performance increase across all .NET runtimes you might chime in here:

Trying to get attention for a C# feature that would benefit all .NET runtimes NOT just .NET Core.

This means a significant performance increase and energy savings for Mono (in turn WASM), IL2CPP, .NET Framework and .NET Core runtimes for your games, physics engines or APIs in general that make heavy use of non-primitive structs.

So if you’re like me and would love to see more scripting performance in Unity3D, C# based games in general or frameworks, it might help voicing this on GitHub. The C# team is much more likely to consider language features if a larger body of people have use for them. I know I’m not the only one who would have a strong use case for this. Proving that point is the hardest part sometimes…


Sounds interesting, but even after reading the original C# issue, I still don’t understand the problem. You can, after all, pass structs by reference instead. The parameter modifiers in, ref and out should do the trick normally. So this is just a problem when you use a struct with operator overloading. So, why not use a class instead?
Anyways, it seems similar to so it has been proposed before.


Classes require heap allocation. Using classes here would be tremendously slow and take up way more ram.
Look at: “HLSL Comparison”

.NET will inline operators giving the same optimized result I’m looking for BUT only if you can return out a new struct, which means you must have a suitable constructor for the return type.

Also if you call a method that can’t be inlined (a non-operator method) using an ‘out’ will be faster on larger structs like Vec4 but you can’t elegantly change that.

Even in the example you posted on GitHub, he is doing the common mistake of not directly returning a new instance. He first declares the type on the stack, which the .NET optimizer doesn’t play well with. It inlines it but still does the extra stack copy.

.NET Foundation Website | Blog | Projects | Code of Conduct