Saturday, November 26, 2016

10x faster than Delegate.DynamicInvoke

This is a follow up to my previous blog posts, Optimizing Dynamic Method Invokes in .NET, and Dynamically Invoke Methods Quickly, with InvokeHelpers.EfficientInvoke. Basically, I have re-implemented this for Tact.NET in a way that makes it smaller, faster, and compatible with the .NET Standard.

So, how much faster is this new way of doing things? EfficientInvoker.Invoke is over 10x faster than Delegate.DynamicInvoke, and 10x faster than MethodInfo.Invoke.

Check out the source on GitHub:

Simple Explanation

Here is an example of a method and a class that we might want to invoke dynamically...

public class Tester
{
    public bool AreEqual(int a, int b)
    {
        return a == b;
    }
}

...and then here is the code that the EfficientInvoker will generate at runtime to call that method:

public static object GeneratedFunction(object target, object[] args)
{
    return (object)((Tester)target).AreEqual((int)args[0], (int)args[1]);
}

See, it's simple!

Sample Tests

public class EfficientInvokerTest
{
    public void DelegateInvoke()
    {
        var del = (Delegate) new Func<int, int, bool>((a, b) => a == b);
            
        // Standard slow way
        var result1 = (bool)del.DynamicInvoke(1, 2);
        Assert.False(result1);
 
        // New 10x faster way
        var invoker = del.GetInvoker();
        var result2 = (bool)invoker.Invoke(del, 1, 2);
        Assert.False(result2);
    }
    
    public void MethodInvoke()
    {
        var tester = new Tester();
        var type = tester.GetType();
 
        // Standard slow way
        var methodInfo = type.GetTypeInfo().GetMethod("AreEqual");
        var result1 = (bool) methodInfo.Invoke(tester, new object[] {1, 2});
        Assert.False(result1);
 
        // New 10x faster way
        var invoker = type.GetMethodInvoker("AreEqual");
        var result2 = (bool) invoker.Invoke(tester, 1, 2);
        Assert.False(result2);
    }
    
    private class Tester
    {
        public bool AreEqual(int a, int b)
        {
            return a == b;
        }
    }
}

Enjoy,
Tom

25 comments:

  1. Hi, i cannot check the source on GitHub !
    any help?

    ReplyDelete
  2. https://github.com/tdupont750/tact.net/blob/master/framework/src/Tact/Reflection/EfficientInvoker.cs

    ReplyDelete
  3. Thankyou for this! I needed something super fast with minimal garbage. This looks like it does the job.

    ReplyDelete
  4. First of all, thank you for your post. Your posts are neatly organized with the information I want, so there are plenty of resources to reference. I bookmark this site and will find your posts frequently in the future. Thanks again ^^ 토토커뮤니티

    ReplyDelete
  5. If you are facing any challenges in arranging any place 토토사이트

    ReplyDelete
  6. Hello! Nice to meet you, I say . The name of the community I run is 먹튀검증사이트, and the community I run contains articles similar to your blog. If you have time, I would be very grateful if you visit my site .

    ReplyDelete
  7. What is this? That's a hell of a piece of news.토토사이트Can you tell me a secret next time? Thank you so much. Have a nice day.

    ReplyDelete
  8. We all need to see this kind of information, and we really hope that this article will spread widely.메이저안전놀이터 May the good things always be full.

    ReplyDelete
  9. I have gone through this article named ' Walk My World Learning Event'. It was a very informative article 룰렛

    ReplyDelete
  10. Incredible! This blog lo?ks exactly ?ike my o?d one!
    It’s on a totally d?fferent subject b?t it ?as pretty muc?
    t?e ?ame layout ?nd design. Outstanding choice ?f colors!
    먹튀검증

    ReplyDelete
  11. Thank you for sharing this information. I read your blog and I can't stop my self to read your full blog. Again Thanks and Best of luck to your next Blog in future.
    바카라사이트

    ReplyDelete
  12. Thanks for sharing with us this important Content. I feel strongly about it and really enjoyed learning more about this topic.
    카지노사이트

    ReplyDelete
  13. This was an extremely nice post. Taking a few minutes and actual effort to generate a top notch article.
    토토

    ReplyDelete
  14. While looking for articles on these topics, I came across this article on the site here. As I read your article, I felt like an expert in this field. I have several articles on these topics posted on my site. Could you please visit my homepage? 토토사이트모음

    ReplyDelete
  15. This is a very interesting post. Thank you for posting a lot of interesting posts. And please visit my site!~! The name of the site is 슬롯사이트.

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. Hi! This is my first visit to your blog! We are a team of volunteers and new initiatives in the same niche. Blog gave us useful information to work. You have done an amazing job! 메이저토토사이트 Thank you very much. Can I refer to your post on my website? Your post touched me a lot and helped me a lot. If you have any questions, please visit my site and read what kind of posts I am posting. I am sure it will be interesting.

    ReplyDelete
  18. Hi, after reading this remarkable piece of writing i am as well happy to share my experience here with friends. This is a very interesting article. Please, share more like this! Feel free to visit my website; 한국야동

    ReplyDelete
  19. Greetings! Very helpful advice within this post! It's the little changes that will make the most important changes. Thanks for sharing! Feel free to visit my website; 국산야동

    ReplyDelete
  20. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. Feel free to visit my website; 일본야동

    ReplyDelete
  21. GitHub links are broken, please update :(

    ReplyDelete
  22. Secret mansion 먹튀커뮤니티 — like something out of James Bond

    ReplyDelete
  23. I think your website has a lot of useful knowledge. I'm so thankful for this website.
    I hope that you continue to share a lot of knowledge.
    This is my website.
    넷마블머니상

    ReplyDelete

Real Time Web Analytics