Wednesday, October 29, 2008

The cost of method calls

Here I'll talk about the cost (or performance) of various ways of method invocation in .NET.

First of all, let's assume:
- Virtual method call time is 1x. This is about ~ 600M calls / sec. on Core 2 Duo @ 2.66 GHz. To be precise, we're talking about instance method getting no arguments and returning a single Int32 value (i.e. it is an average property getter).

So:
- Delegate method call time is 1.5x (the same method invoked by delegate pointing to it)
- Interface method call time is 2x (the same method invoked on reference of interface type)

A bit surprising, yes? The explanation is here. Briefly, interface method dispatch is more complex than delegate dispatch, since we must locate appropriate interface method table for the instance we have first. If count of implemented interfaces on a particular type is quite large (that's actually almost impossible), the only good way to do this is to use hash table. But if it is small (that's the most frequent case), there must be just a search in small [possibly - ordered] list. But this is anyway more complex than in case with delegate, since delegate already contains the exact method address (this isn't correct for open delegates).

Now one more fact to think about: all the tests I'm talking about are loop-based, performed in Release configuration, and their code is nearly the following:

for (int i = 0; i<...; i+=10) {
o = c.GetValue();
o = c.GetValue();
o = c.GetValue();
o = c.GetValue();
o = c.GetValue();
o = c.GetValue();
o = c.GetValue();
o = c.GetValue();
o = c.GetValue();
o = c.GetValue();
}


.NET is able to cache resolved interface method tables (and possibly - even method addresses), so calling interface method in a loop must be a bit faster, than calling it once. So in general the cost of interface method call in comparison to delegate call is even bigger.

This explains why we have such types as Hasher(of T) (Comparer, Arithmetic, etc.). They cache the delegates performing a set of operations on T type faster than with use of similar interface in their fields. See e.g. Hasher(of T).GetHash field. Certainly, such an approach is used only when performance is essential - i.e. when it's well known these operations will be invoked many times e.g. on any index seek.

Let's look of few more metrics:
- Creating a delegate pointing to non-virtual method time is 7.5x
- Creating a delegate pointing to virtual method time is 50x
- Creating a delegate pointing to interface method time is 150x

So delegate construction isn't cheap at all.

Now we're turning to virtual generic methods:
- Virtual generic method call time is 10x - independently on of it is called on interface or not. Dependency on count of generic arguments almost absents as well - adding one more argument makes the call longer by ~ 0.5%.
- Creating a delegate pointing to generic virtual method time is 1000x. Not sure, why - it looks like because of some bug in .NET. Since delegates in .NET may point to fully parameterized methods only (they store method address), so the time of calling such a delegate is 1.5x, as before.

Why it's so costly to call virtual generic method? Because there can be generally any number of its implementations, dependently on the argument substitution, so .NET framework resolves its address using internal hash table, that must be bound to corresponding virtual (or interface) method table.

So we may also take that:
- Internal hash table seek time is ~ 10x - I'll use this time in my future posts to show how to estimate the cost of generally any basic operation in .NET.

And few conclusions related to generic virtual methods:
- The smallest heap allocation time is 3.5x; Int32 boxing time is 4x (with its heap allocation). So it's almost always cheaper to have non-generic virtual method with object type argument, rather than generic virtual method.
- If generic virtual method seems anyway preferable, you might think about implementing its "call caching" with use of delegates. E.g. we use DelegateHelper.CreateDelegates and DelegateHelper.ExecuteDelegates methods to perform the operations on Tuples of the same structure faster.

149 comments:

  1. Forgot to add: the code for all these tests can be found in DataObjects.Net 4.0 test suite, see "Xtensive.Core\Xtensive.Core.Tests\DotNetFramework\".

    Any comments are welcome.

    ReplyDelete
  2. Good stuff, Alex.

    What do you think of functional-style programming in C# 3.0 and anonymous delegates? It seems that it will generate slower programs without the developer's knowledge... It is maybe the natural path from a low-level and efficient programming language to a high-level and low one :(.

    -gael

    ReplyDelete
    Replies
    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
      or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

      Delete
  3. I agree with this. Anonymous delegates are frequently not cached, and in such cases construction of their instances is rather costly. Let's estimate this:

    - .NET creates an anonymous type to handle such a delegate. Its construction cost is ~ 3.5 in comparison to VM call.
    - Then it creates a delegate pointing to its method. The cost is ~ 7.5.
    - Finally, it invokes it. The cost is 1.5, assuming invocation happens just once (i.e. the worst, but most likely the most common case).

    So if anonymous delegate is called just once, you're getting ~ 8.5 times slower call in case it isn't cached.

    In general this isn't "to bad" - i.e. you should have about 50M invocations \ second in such a case. This is anyway much more then such languages as Ruby or Python may provide. But less then 400M with a regular delegate, of course ;)

    Btw, this is enough for the most of RDBMS \ BLL related tasks. It was shown that peak speed of data extraction with ORM is about 400K instances per second, i.e. 100 times slower. So using such anonymous delegates won't seriously affect on performance in such cases.

    But if I develop a game on C#, or something else requiring real-time computing, I would seriously care about this.

    ReplyDelete
  4. Copying recent discussion of this post by e-mail:

    Q: Hello Alex,

    I have read your blog post "The cost of method calls". It's very interesting. I was very surprised on the cost of virtual generic method call. Isn't there any difference between using reference vs value type parameters on the type or method?


    A:

    I suspect there can be noticeable difference: since actually there is just one JITted version of method emitted for any reference type, theoretically it can be found much faster (i.e. without a hash table lookup).

    On the other hand, generic methods are practically much more interesting for value types... For reference types they bring only better safety & readability. That's why we measured this for value types only.

    So just measured:

    Virtual generic call test (1 generic argument: Int32):
    Type: Int32
    Measurement: Generic method 1 (class) 65,500 M/s.
    Measurement: Generic method 1 (class, by delegate) 403,042 M/s.
    Measurement: Generic method 1 cast (class) 42,202 M/s.
    Measurement: Generic method 1 (interface) 65,523 M/s.
    Measurement: Generic method 1 (interface, by delegate) 405,369 M/s.
    Measurement: Generic method 1 cast (interface) 609,802 K/s.
    Virtual generic call test (1 generic argument: String):
    Type: String
    Measurement: Generic method 1 (class) 51,888 M/s.
    Measurement: Generic method 1 (class, by delegate) 308,143 M/s.
    Measurement: Generic method 1 cast (class) 444,508 K/s.
    Measurement: Generic method 1 (interface) 51,326 M/s.
    Measurement: Generic method 1 (interface, by delegate) 309,349 M/s.
    Measurement: Generic method 1 cast (interface) 443,909 K/s.

    Conclusions:
    - The difference is generally neglectable. May be we should try putting "where T: class" constraint on such methods to make JITter to think about this a bit more? ;)
    - Again, it seems there is a serious bug in delegate creation (see bold italicized results in "... cast" lines).

    ReplyDelete
  5. An overview of implementation of virtual generic method call in Mono: http://schani.wordpress.com/2008/10/12/fast-virtual-generic-calls/

    ReplyDelete
  6. A report about mentioned issue with performance of VGM calls on interface: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=91710

    ReplyDelete
  7. Here we present to introduce to all visitors in this great blog. and thank you for allowing me .

    cara menggugurkan kandungan
    obat aborsi
    tanda tanda kehamilan

    ReplyDelete
  8. By reading this article many benefits that we can learn. thank you for sharing your insights to us all . alat bantu sex

    ReplyDelete
  9. By taking the time to read a lot of information like this to add my insight . cara menggugurkan hamil , By taking the time to read a lot of information like this to add my insight . cara menggugurkan kandungan dengan sprite

    ReplyDelete
  10. Very Interesting and wonderful information keep sharing this post kindly check
    standard international delivery

    ReplyDelete
  11. See more at the link: bobby movie
    See more at the link: vivavideo pro
    See more at the link: Fildo

    ReplyDelete
  12. See more at the link: bobby movie
    See more at the link: vivavideo pro
    See more at the link: Fildo

    ReplyDelete
  13. Thank you so much sharing such an amazing blog. It’s really helpful for me. Visit for
    SEO Service in Delhi

    ReplyDelete

  14. The article is very interesting. And I also want to share articles about health, I'm sure this will be useful. Read and share it. Thank you very much :)

    Cara Menyembuhkan Sakit Maag Kronis
    Cara Menghilangkan Stretch mark
    Cara Mengobati Epilepsi

    ReplyDelete
  15. THANKS FOR INFORMATION

    you can search low-cost website with high-quality website functions.
    Today Join Us
    Call: +91 - 8076909847

    website development company in Delhi

    levantro
    interior designers in delhi


    livewebindia
    website designing company in delhi

    SEO services in Delhi

    Best It Service Provider:

    1. Website Designing And Development.
    2. SEO Services.
    3. Software Development.
    4. Mobile App Development.

    ReplyDelete
  16. Nice blog! Thanks for sharing with us. It was really helpful for me. Visit for
    5 Best Things to Do in Switzerland during Winter

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

    ReplyDelete
  18. .Net is quite effective and .NET creates a unified environment that allows developers to create programs in C++, Java or Virtual Basic.

    For More Details about .net and best website designing Company jsut on Jeewan Garg - Website Designing Company

    ReplyDelete
  19. Well !!! This is very awesome Happy Easter 2019 Images and good written article Good Friday 2019 Images you have provided here all the Information and facts Kentucky Derby 2019 are always truthful and great. Thanks for this awesome article

    ReplyDelete

  20. Nice information.. Thanks for sharing this blog. see my website also
    .. VIEW MORE:- Website Designing Company in Delhi

    ReplyDelete

  21. بسم الله الرحمن الرحيم عميلنا العزيز نحن نقدم افضل خدمات تنظيف المجالس والشقق بافضل انواع

    التنظيفات المشهود لها عالميا
    شركة تنظيف مجالس بالطائف
    شركة تنظيف مجالس بجازان
    شركة تنظيف مجالس بحائل
    ونحن فى خماتكم 24 ساعة على مدار الشهر

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

    ReplyDelete
  23. Hey, thanks for posting amazing articles. These blogs would definitely help us keep posted about new trends in the market. Thanks for sharing
    Dentist in Kanpur

    ReplyDelete
  24. Hey, thanks for posting amazing articles. These blogs would definitely help us keep posted about new trends in the market. Thanks for sharing
    Best Unisex Hair Salon in Lucknow

    ReplyDelete
  25. Excellent blog here! Also your site loads up very fast! What web host are you using? Can I get your affiliate link to your host? I wish my web site loaded up as fast as yours lol

    Happy Easter Images 2019
    Happy Easter Captions 2019
    Happy Easter Sayings 2019
    Happy Easter Images 2019 For Facebook
    Happy Easter Pictures 2019 For Facebook

    ReplyDelete
  26. Ah,so beautiful and wonderful post!An opportunity to read a fantastic and imaginary blogs.It gives me lots of pleasure and interest.Thanks for sharing.
    Find the Interior Designers in Madhurawada

    ReplyDelete
  27. I do not know what to say except that I have enjoyed reading your article. I will keep visiting this blog very often. I’ll use this information for my work.

    https://www.starepisodes.net/aap-ke-aa-jane-se/

    ReplyDelete
  28. Hi All,
    I have found one of the best SEO services provider company in Delhi(www.nexcuit.com) they are providing the best SEO service in Delhi, here you can get the best professional services within your budget and as per your given timeline this company is among one of the top most companies which provide SEO services in India as of now Our Office is situated in East Delhi which is close to Nirman Vihar metro station.For more details please dial+919910326510 and get more insights concerning their charges, 100% satisfaction guaranteed.

    seo services in India

    seo company in delhi

    ReplyDelete
  29. Nice post, thanks for sharing with us this kind of important information with us.
    SEO services in navi mumbai
    website maker in mumbai

    ReplyDelete
  30. Read more now about Eid Mubarak Greetings and share to your Facebook account...

    ReplyDelete
  31. Nice Post I saw something similar here

    Contact other Sugar Mummies Here

    New York Sugar Mommy Want To Be Your Sugar Mummy – Click Allow Now

    You Have A Private Message From Sugar Mummy Loveth


    Rich Sugar Mommy Looking For Love – Get Phone Number

    Sugar Mummy On Whatsapp Want To Pay You Weekly


    Sugar Mummy On Whatsapp Want To Pay You Weekly


    Get Sugar Mummy Direct Phone Number Without Agent Now


    I saw something similar here




    Nice Post I saw something similar here

    Contact other Sugar Mummies Here
    https://2kinfomedia.com/
    https://2kinfomedia.com/category/celeb-gists/
    https://2kinfomedia.com/category/news/
    https://2kinfomedia.com/category/weird-news/
    https://2kinfomedia.com/category/unizik-news/
    https://2kinfomedia.com/category/music/
    https://2kinfomedia.com/category/interesting/
    https://2kinfomedia.com/category/interesting/
    https://2kinfomedia.com/category/scholarships/
    https://2kinfomedia.com/category/events/
    https://2kinfomedia.com/tag/sa-hiphop/


    New York Sugar Mommy Want To Be Your Sugar Mummy – Click Allow Now

    You Have A Private Message From Sugar Mummy Loveth

    ReplyDelete
  32. Wow!! Really a nice Article. Thank you so much for your efforts. Definitely, it will be helpful for others,
    Just want to share a valuable information, nexcuit.com is the best SEO company in Delhi which provides the complete SEO solutions. These SEO company based in Laxmi Nagar,East Delhi, owned and managed by OM Prakash who have a good amount of experience in digital marketing, SEO and other related fields. We provide the best SEO service in Delhi and across India.,Call @ +919910326510 and get more insights related to charges and other work related terms & conditions.

    seo services in delhi
    best seo company in delhi
    seo company in delhi
    seo company in laxmi nagar

    ReplyDelete
  33. A mobile app or mobile application is a computer program or software application designed to run on a mobile device such as a phone/tablet or watch. Some Good apps for our iOS and Android Devices are
    Snaptube For PC
    Tutuapp Lite

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

    ReplyDelete
  35. Looking for the best site for Vidmate Download ? Vidmate Official site https://www.vidmateapp.com is the right place for you! Vidmate is the best app that lets you download videos and songs from YouTube, Facebook, Twitter, etc and various other sites.

    ReplyDelete
  36. I have been a keen follower of your website.
    recently I came across this topic and after reading the whole article I am amazed that how well you have written it.
    Amazing writing skills shown.
    You have done a good research on this topic.
    Great Work.

    - Inquire Hub - Best website for luxury items and Luxurious lifestyle magazine

    ReplyDelete
  37. Don't you want your business to be explored in front of thousands of eyeballs? Check this out only if you're serious to get it exposed online.
    scorsh

    ReplyDelete
  38. Thanks for the nice blog. It was very useful for me. I'm happy I found this blog. Thank you for sharing with us,I too always learn something new from your post.
    flipkart customer care
    flipkart big billion days
    flipkart helpline
    flipkart app download
    flipkart no cost emi | bajaj finserv emi card
    flipkart buyback guarantee
    flipkart seller support number
    flipkart internship
    flipkart smartbuy
    Download flipkart app For PC/Laptop Windows 10/7/8.1/8/XP

    ReplyDelete
  39. https://aixonne.com/l/iPhoneCase.html
    If you have an iPhone you probably need a great protection cover for it to prevent any kind of falling damages to it. Aixonne has a complete collection of cute iPhone case 8, 7, 6, 5 and X series as well as high quality best iPhone screen protector products to keep your iPhone screen safe.
    https://aixonne.com/l/iPhoneTemperedGlass.html
    Do not think that it is an iPhone accessories store! Not at all! Aixonne is complete store with a high range of products like clothing, electronic gadgets, home and garden tools and devices, accessories like watches or choker necklace for women baby and mother items, etc.
    Just visit their awesome store and you’ll become a loyal costumer of Aixonne!
    https://aixonne.com/l/necklace.html

    ReplyDelete
  40. Kinemaster pro is the only full-featured professional video editor for Android, Chrome OS, iPhone and iPad, supporting multiple video layers, image layers, and text.
    Also read this kinemaster pc

    ReplyDelete
  41. This is very nice article for more information about the unique and high quality content denture repair

    ReplyDelete
  42. That was a really nice post! Thanks for sharing!
    Jewellery Industry in India

    ReplyDelete
  43. it is official page for download shareit app shareitforpcs

    ReplyDelete
  44. https://gadgetsright.com/moviebox-apk-latest-version-free-download-android-ios-2019/
    https://gadgetsright.com/airdroid-connect-android-phone-linux-via-airdroid/
    https://vpnxon.com/vpn/how-to-set-up-nordvpn-on-linux-the-best-guide/
    https://vpnxon.com/vpn/android-tv-box-how-to-install-a-vpn-on-android-tv-box/
    https://gadgetsright.com/safe-mode-on-tecno-phones/
    https://vpnxon.com/vpn/android-tv-box-how-to-install-a-vpn-on-android-tv-box/
    https://gadgetsright.com/download-install-appvalley-ios-android/
    https://vpnxon.com/vpn/chromecast-how-to-setup-a-vpn-on-chromecast/

    ReplyDelete
  45. Cartoon HD has its own features like it gives you trending videos,
    featured videos and Download Cartoon HD Apk section.
    You can tap on it to open and choose the video right away.
    No need for television or big screen it works in your devices in very good quality. It's easy to attempt all the videos any time when you want to do.

    ReplyDelete
  46. Excellent and nice post. It will beneficial for everyone. Thanks for sharing such a wonderful post, If anyone is looking out for a best smo company in delhi then I would like to share the name of one such company which ranked among the Top social media marketing company in delhi (www.nexcuit.com) they are the social media marketing agency in delhi,here you can get the best professional services within your budget and as per your given timeline this company is among one of the top most companies which provide social media agency services in India as of now there Office is situated in East Delhi which is close to Nirman Vihar metro station. For more details please dial +919910326510 and get more insights concerning their charges, 100% satisfaction guaranteed.
    social media agency in delhi
    social media marketing company in delhi
    social media marketing companies in delhi /
    social media marketing agency in delhi
    social media marketing companies
    social media agency in laxmi nagar
    social media marketing
    social media agency
    smo company in delhi

    ReplyDelete
  47. This is very good piece of work. Please keep it Up.
    Periodontist The woodlands

    ReplyDelete