- Home /
SendMessage vs MethodInfo.Invoke
Which of these has better performance:
SendMessage or Invoke on a cached MethodInfo?
Through my own testing, it seems that Invoke is slightly faster but the difference is negligible.
Answer by whydoidoit · May 21, 2012 at 02:05 AM
If you can cache the calls rather than dynamically look the methodinfo up every time you can create a delegate off the MethodInfo and that will be much faster.
You should be able to cache said delegates in a dictionary and call through that - the downside is that you need to be able to figure out the type of the delegate in advance (Action, Action etc). You can inspect the method and figure out what Action/Func flavour you need and get Expression to build it for you.
That can lead to problems if you are running on a mobile device. See my article here on workarounds for that.
The code below is from that article - it shows creating delegate and deciding on the correct type - then caching them and allowing a FastInvoke method. Note that you wouldn't have to worry about all of the RegisterXXX functions and the lookup in the FastInvoke if you aren't running on a phone or in any other AOT compilation scenario.
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;
using System.Reflection;
using System.Linq.Expressions;
public static class DelegateSupport
{
private static Dictionary<MethodInfo, Type> delegateTypes = new Dictionary<MethodInfo, Type>();
private static Dictionary<MethodInfo, Delegate> openDelegates = new Dictionary<MethodInfo, Delegate>();
public static Delegate ToOpenDelegate(MethodInfo mi)
{
Delegate result;
if(!openDelegates.TryGetValue(mi, out result))
{
Type delegateType;
if(!delegateTypes.TryGetValue(mi, out delegateType))
{
var typeArgs = mi.GetParameters()
.Select(p => p.ParameterType)
.ToList();
typeArgs.Insert(0, mi.DeclaringType);
// builds a delegate type
if (mi.ReturnType == typeof(void)) {
delegateType = Expression.GetActionType(typeArgs.ToArray());
} else {
typeArgs.Add(mi.ReturnType);
delegateType = Expression.GetFuncType(typeArgs.ToArray());
}
delegateTypes[mi] = delegateType;
}
openDelegates[mi] = result = Delegate.CreateDelegate(delegateType, mi);
}
return result;
}
public static Delegate ToDelegate(MethodInfo mi, object target)
{
Delegate result;
Type delegateType;
if(!delegateTypes.TryGetValue(mi, out delegateType))
{
var typeArgs = mi.GetParameters()
.Select(p => p.ParameterType)
.ToList();
// builds a delegate type
if (mi.ReturnType == typeof(void)) {
delegateType = Expression.GetActionType(typeArgs.ToArray());
} else {
typeArgs.Add(mi.ReturnType);
delegateType = Expression.GetFuncType(typeArgs.ToArray());
}
delegateTypes[mi] = delegateType;
}
// creates a binded delegate if target is supplied
result = Delegate.CreateDelegate(delegateType, target, mi);
return result;
}
public class Index<TK, TR> : Dictionary<TK,TR> where TR : class, new()
{
public new TR this[TK index]
{
get
{
TR val;
if(!(TryGetValue(index, out val)))
{
base[index] = val = new TR();
}
return val;
}
set
{
base[index] = value;
}
}
}
private static Index<Type, Dictionary<string, Func<object, object[], Delegate, object>>> _functions = new Index<Type, Dictionary<string, Func<object, object[], Delegate,object>>>();
private static Dictionary<MethodInfo, Func<object, object[], object>> _methods = new Dictionary<MethodInfo, Func<object, object[], object>>();
public static void RegisterActionType<TType>()
{
_functions[typeof(TType)][GetTypes(typeof(void))] = (object target, object[] parms, Delegate @delegate)=>{
((Action<TType>)@delegate)((TType)target); return null;
};
}
public static void RegisterActionType<TType, T1>()
{
_functions[typeof(TType)][GetTypes(typeof(T1), typeof(void))] = (object target, object[] parms, Delegate @delegate)=>{
((Action<TType, T1>)@delegate)((TType)target, (T1)parms[0]); return null;
};
}
public static void RegisterActionType<TType, T1, T2, T3>()
{
_functions[typeof(TType)][GetTypes(typeof(T1), typeof(T2), typeof(T3), typeof(void))] = (object target, object[] parms, Delegate @delegate)=>{
((Action<TType,T1,T2,T3>)@delegate)((TType)target, (T1)parms[0], (T2)parms[1], (T3)parms[2]); return null;
};
}
public static void RegisterActionType<TType, T1, T2>()
{
_functions[typeof(TType)][GetTypes(typeof(T1), typeof(T2), typeof(void))] = (object target, object[] parms, Delegate @delegate)=>{
((Action<TType, T1, T2>)@delegate)((TType)target, (T1)parms[0], (T2)parms[1]); return null;
};
}
public static void RegisterFunctionType<TType, TReturns>()
{
_functions[typeof(TType)][GetTypes(typeof(TReturns))] = (object target, object[] parms, Delegate @delegate)=>{
return (object)((Func<TType, TReturns>)@delegate)((TType)target);
};
}
public static void RegisterFunctionType<TType, T1, TReturns>()
{
_functions[typeof(TType)][GetTypes(typeof(T1), typeof(TReturns))] = (object target, object[] parms, Delegate @delegate)=>{
return (object)((Func<TType, T1, TReturns>)@delegate)((TType)target, (T1)parms[0]);
};
}
public static void RegisterFunctionType<TType, T1, T2, TReturns>()
{
_functions[typeof(TType)][GetTypes(typeof(T1), typeof(T2), typeof(TReturns))] = (object target, object[] parms, Delegate @delegate)=>{
return (object)((Func<TType, T1, T2, TReturns>)@delegate)((TType)target, (T1)parms[0], (T2)parms[1]);
};
}
public static void RegisterFunctionType<TType, T1, T2, T3, TReturns>()
{
_functions[typeof(TType)][GetTypes(typeof(T1), typeof(T2), typeof(T3), typeof(TReturns))] = (object target, object[] parms, Delegate @delegate)=>{
return (object)((Func<TType, T1, T2, T3, TReturns>)@delegate)((TType)target, (T1)parms[0], (T2)parms[1], (T3)parms[2]);
};
}
public static object FastInvoke(this MethodInfo mi, object target, params object[] parameters)
{
Func<object, object[], object> getter;
string types;
if(!_methods.TryGetValue(mi, out getter))
{
if(_functions.ContainsKey(mi.DeclaringType) && _functions[mi.DeclaringType].ContainsKey(types = GetTypes(mi)))
{
var @delegate = ToOpenDelegate(mi);
var inner = _functions[mi.DeclaringType][types];
var complete = _methods[mi] = (t,p) => inner(t, p, @delegate);
return complete(target, parameters);
}
return mi.Invoke(target, parameters);
}
else
{
return getter(target, parameters);
}
}
static string GetTypes(params Type[] types)
{
return types.Select(t=>t.FullName).Aggregate("", (v,n)=>v += n);
}
static string GetTypes(MethodInfo mi)
{
return GetTypes(mi.GetParameters().Select(p=>p.ParameterType).Concat(new Type[] {mi.ReturnType}).ToArray());
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613071643im_/https://answers.unity.com/themes/thub/images/avi.jpg)