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

If anyone here uses a heavy amount of vectors or math in their games and would like to see a significant performance increase I invite you to chime in here: https://github.com/dotnet/csharplang/issues/2217

I’m trying to get a C# language feature more attention that would benefit all .NET runtimes NOT just .NET Core.

This means a significant performance increase and energy saver for Mono runtimes for your games.

So if you’re like me and would love to see more scripting performance in games, it might help voicing this on GitHub. The C# team is much more likely to consider language features if a large body of people have use for them.

I am currently experimenting with a different approach,

The idea is to take an assembly with MSIL and replace the op_addition,op_multiply,etc calls generated by code like this r = a + b; into this Vector2.Add(ref a, ref b, out r);
This is to keep your code readable and a at same time get the best performance.

SharpDX is using a similar technique to patch marhal code with faster direct calls.

In principle it works., The next step is to build an AST from IL which would allow me to handle all possible cases. I have looked at CCI, Rolsyn, ILSpy. The best candidate so far is CCI, or to build an IL->AST from scratch on Cecil.

I’ve started a project called IL2X which will be designed to convert .NET IL into C89 or other native assembly targets.

IL2X could come with special compiler attributes to tell operator methods that get generated with returns to be generated with outs.

Example:

[return:IL2X_OutReturn]
public static Vec3 operator+(in Vec3 a, in Vec3 b)
{
	return new Vec3(a.x + b.x, a.y + b.y, a.z + b.z);
}

When IL2X sees that attribute it could convert it to C like so:

void Vec3_add_Operator(Vec3* IL2X_result, Vec3* a, Vec3* b)
{
	*IL2X_result = Vec3_INIT(a->x + b->x, a->y + b->y, a->z + b->z);
}

Not only will C get you far more performance this syntax conversion will make it massively more performant.