Our great sponsors
-
WorkOS
The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.
Unfortunately, while I would love JIT to do such kind of optimizations, this is completely not how it works today.
First and foremost, '&mut' in Rust is a mutable reference. The C# alternative is just 'ref'. Both point to specific addresses. 'Box' OTOH is a handle for a struct allocated on a heap, just like C#'s one is implementation-wise (except in C# they are further managed by runtime but that's besides the point).
Now regarding the arrays - 'T[]' is strictly object withouts ifs and buts. There is no "short array" optimisation that would substitute array type with stack-allocated inline buffer. If you want this, you have to write it manually. See: https://github.com/dotnet/runtime/blob/57bfe474518ab5b7cfe6b...
The last but not least, if the method signature accepts an argument by its interface type e.g. 'HandleEnumerable(IEnumerable values)', then the IEnumerable-implementing struct will be boxed before method can be called. C#/.NET doesn't do full generic monomorphization like Rust. Instead, for class-type generics a common '_Canon' method body will be JITted which will also implicitly accept generic-type arguments to correctly dispatch on an actual of the method. OTOH, for structs, all method calls will be fully monomorphized per each struct type. This means that you can avoid struct boxing when dispatching by interface by rewriting method signature to 'HandleEnumerable(TEnumerable values) where TEnumerable : IEnumerable'. In this case, unless you cast your struct to an interface type explicitly beforehand, the struct will not be boxed.
Speaking of serverless, this might be of interest to you: https://github.com/awslabs/dotnet-nativeaot-labs
IIRC there's a little more work needed for unity, but Cysharp has you covered (https://github.com/Cysharp/UniTask)