2 ms·
Here's an article about optimizing reflection on Unity AOT platforms like iOS: Mike Talbots Blog: Faster Invoke for reflected property access and method invoca
by DonHopkins 10y ago
Here's an article about optimizing reflection on Unity AOT platforms like iOS:
Mike Talbots Blog: Faster Invoke for reflected property access and method invocation with AOT compilation
Article: https://web-beta.archive.org/web/20120626190355/http://whydoidoit.com/2012/04/18/faster-invoke-for-reflected-property-access-and-method-invocation-with-aot-compilation https://web-beta.archive.org/web/20120626190355/http://whydo...
Source: https://web-beta.archive.org/web/20161005070240/http://www.whydoidoit.net/Downloads/DelegateSupport.cs https://web-beta.archive.org/web/20161005070240/http://www.w...
Newer version: 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...
"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."
http://stackoverflow.com/questions/1116073/can-delegate-dynamicinvoke-be-avoided-in-this-generic-code http://stackoverflow.com/questions/1116073/can-delegate-dyna...
If you need to extend this to wrapping member invocations from classes without using Reflection.Emit you can do so by creating a series of compiler hints that can map a class and a function parameter list or return type.
Basically you need to create lambdas that take objects as parameters and return an object. Then use a generic function that the compiler sees AOT to create a cache of suitable methods to call the member and cast the parameters. The trick is to create open delegates and pass them through a second lamda to get to the underlying hint at runtime.
You do have to provide the hints for every class and signature (but not every method or property).
I've worked up a class here that does this, it's a bit too long to list out in this post.
In performance testing it's no where near as good as the example above, but it is generic which means it works in the circumstances I needed. Performance around 4.5x on reading a property compared to Invoke.