5 ms·
C# reflection is also slow, and even slower in Unity3D with the IL2CPP ahead-of-time compiler, where there is no just-in-time compiler or System.Reflection.Emit
by DonHopkins 9y ago
C# reflection is also slow, and even slower in Unity3D with the IL2CPP ahead-of-time compiler, where there is no just-in-time compiler or System.Reflection.Emit to dynamically generate code at runtime (which is impossible and prohibited on iOS, for example).
Here are the limitations of the Mono runtime:
https://developer.xamarin.com/guides/ios/advanced_topics/limitations/ https://developer.xamarin.com/guides/ios/advanced_topics/lim...
However, there are ways of compiling the little snippets of the code you need ahead of time, then plugging them together with delegates at runtime, which avoids much of the overhead.
Much of the cost is the overhead of converting from unknown types to specific types. So you can pre-compile loosely typed adaptor thunks that take generic "object" arguments, convert them to the required type, and directly call strongly typed delegates (which you can plug into the adaptor, so adaptors can be used for any number of compatible delegates).
Check out how the following code uses FastInvoke, ToOpenDelegate, Delegate.CreateDelegate, Expression.GetActionType, and
Expression.GetFuncType.
It may be that some of these general techniques could be applied to Java.
Faster Invoke for reflected property access and method invocation with AOT compilation:
https://web-beta.archive.org/web/20120826100615/http://whydoidoit.com/2012/04/18/faster-invoke-for-reflected-property-access-and-method-invocation-with-aot-compilation https://web-beta.archive.org/web/20120826100615/http://whydo...
The bane of the iOS programmers life, when working with reflection in Mono, is that you cant go around making up new generic types to ensure that your reflected properties and methods get called at decent speed. This is because Mono on iOS is fully Ahead Of Time compiled and simply cant make up new stuff as you go along. That coupled with the dire performance of Invoke when using reflected properties lead me to construct a helper class.
This works by registering a series of method signatures with the compiler, so that they are available to code running on the device. In my tests property access was 4.5x faster and method access with one parameters was 2.4x faster. Not earth shattering but every little helps. If you knew what you wanted ahead of time, then you could probably do a lot better. See here for info.
You have to register signatures inside each class Im afraid. Nothing I can do about that.
So to register a signature you use:
static MyClass()
{
//All methods returning string can be accelerated
DelegateSupport.RegisterFunctionType<MyClass, string>();
//All methods returning string and taking an int can be accelerated
DelegateSupport.RegisterFunctionType<MyClass, int, string>();
//All methods returning void and taking a bool can be accelerated
DelegateSupport.RegisterActionType<MyClass, bool>();
}
Then when you have a MethodInfo you use the extension method
FastInvoke(object target, params object[] parameters) to call
it. FastInvoke will default to using normal Invoke if you havent
accelerated a particular type.
myObject.GetType().GetProperty("SomeProperty").GetGetMethod().FastInvoke(myObject);
myObject.GetType().GetMethod("SomeMethod").FastInvoke(myObject, 1, 2);
You can download the source code for FastInvoke from here.
https://web-beta.archive.org/web/20120826100615/http://www.whydoidoit.net/Downloads/DelegateSupport.cs https://web-beta.archive.org/web/20120826100615/http://www.w...
Newer version from unityserializer-ng:
https://gitgud.io/TheSniperFan/unityserializer-ng/blob/master/Assets/Plugins/OpenUnityTools/unityserializer-ng/Radical/System/Delegates/DelegateSupport.cs https://gitgud.io/TheSniperFan/unityserializer-ng/blob/maste...