Facebook Twitter Gplus LinkedIn RSS
formats

Faster Invoke for reflected property access and method invocation with AOT compilation


Download

The bane of the iOS programmers life, when working with reflection in Mono, is that you can’t 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 can’t 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 I’m 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 haven’t 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.

3 Responses

  1. Thank you for the post!

    But will this solve the issue about ‘delegate to unmanage function pointer’ for full AOT mode?
    I got this problem everytime I uses delegate callbacks.

    ExecutionEngineException: Attempting to JIT compile method ‘(wrapper native-to-managed) LuaInterface.MetaFunctions:collectObject (intptr)’

    Jhoemar

Home Project With Code Faster Invoke for reflected property access and method invocation with AOT compilation