I have spent the good part of a few days trying to work through a performance problem on a similar custom scripting language for use in unity. I went through the source code for miniscript in csharp and noticed intrinsics are defined as just an anonymous function assigned to a delegate with a standard signature T signature(context, something). Does invoking the delegate have any performance impacts?
In my use case, I have my intrinsics as classes instead of just functions. These classes are written with attributes marking certain properties or methods as "exposed" to the language. The methodinfo from these functions is transformed into a delegate for better performance and cached. However the one thing I can't seem to avoid is garbage collection when invoking the delegate. Sometimes it seems to not generate any garbage, but a majority enough of the calls generate garbage that the overhead of running the language alongside unity freezes up the application. The language itself works fine, and if I replace the intrinsic classes' reflection gathered methods/properties with a massive if else chain inside object GetField(string name), void SetField(string name, object value), object Call(string name, object[] parameters) there's almost no dip in performance. I want to use reflection so that we can treat our intrinsic classes like builtin classes as well as for autodoc purposes.
Is there a way to avoid gc alloc calls when using reflection? Is there a different approach I should take to this?
I apologize if this topic is out of the scope of this forum, I've just reached my wits end googling for information on optimizing reflection.