Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by absameen · May 18, 2012 at 05:22 PM · reflectionsendmessageinvoke

SendMessage vs MethodInfo.Invoke

Which of these has better performance:

SendMessage or Invoke on a cached MethodInfo?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image absameen · May 20, 2012 at 09:40 PM 0
Share

Through my own testing, it seems that Invoke is slightly faster but the difference is negligible.

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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());
 }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

iOS Reflection Code stripping problem 2 Answers

How do I get the system type of Mathf in JS ? 1 Answer

How can I delay dealing damage? 1 Answer

In-Game Console and Permanent Variable Changes 1 Answer

Reflection - passing parameters to an invoked function 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges