Thursday, October 30, 2008

The cost of [ThreadStatic] attribute

First of all, raw results:

Instance field: Operations: 2,650 B/s.
Static field: Operations: 2,630 B/s.
Volatile instance field: Operations: 2,649 B/s.
[ThreadStatic] field: Operations: 43,611 M/s.
Thread data slot: Operations: 4,068 M/s.

The test is actually quite simple: we read specified field in a loop. As before, on Core 2 Duo @2.66GHz. The code can be found in DataObjects.Net 4.0 test suite, see Xtensive.Core\Xtensive.Core.Tests\DotNetFramework\FieldTypeTest.cs.

Conclusions:
- Reading regular, static or volatile field is quite cheap: ~ 0.2x in previously introduced metrics, or 20% of virtual method call time
- [ThreadStatic] fields are actually quite costly in comparison to others: ~ 14x.

Now the main question: why? It isn't so obvious [ThreadStatic] is ~ 60 times slower than static.

JITted [ThreadStatic] access code actually always consists of two parts:
- Call to a system routine returning address of [ThreadStatic] field by its token
- Regular field access instruction.

Obviously, the first part (call) "eats" almost the whole execution time: there is no more efficient way to get the address of such a field by its token rather than using hash table. As I've mentioned before, reading from a system hash table takes ~ 10x. So that's nearly what we have in this case.

Why they're implemented this way in .NET? I can't imagine why they didn't use some faster approach. E.g. I suspect calculating the lowest stack boundary (as well as the upper one) from the current stack pointer value is quite simple operation - something like bitwise and. Why we can't store the address of the first [ThreadStatic] location as fixed address nearby it, and use constant offset for each [ThreadStatic] field relatively to the address of the first one? In this case it would take ~ 1x to access it...

Ok, this is what could be, but in reality we have 14x.

Finally, there are thread data slots as well. But they're 10 times slower than [ThreadStatic] fields, so it's always better to simulate their behavior with e.g. a Dictionary stored in [ThreadStatic] field.

58 comments:

  1. Replies
    1. I have read your blog its very attractive and impressive. I like it your blog.

      Dot Net Training in Chennai Dot Net Training in Chennai .Net Online Training .Net Online Training Dot Net Training in Chennai

      Dot Net Online Training .Net Online Training ASP.NET Online Training ASP.NET Online Training

      Delete
  2. I’m not sure if this is a formatting issue or something to do with internet browser compatibility but I thought I’d post to let you know. The layout look great though! Hope you get the problem resolved soon. Cheers.www.modula2.net

    ReplyDelete
  3. This article is interesting and useful. Thank you for sharing. And let me share an article about health that God willing will be very useful. Thank you :)

    Khasiat Daun Sirsak untuk Benjolan
    Obat Telinga Bernanah paling Ampuh
    Cara Menghilangkan Benjolan di Miss V

    ReplyDelete
  4. Beberapa tips cara menggugurkan hamil untuk anda dengan menggunakan obat penggugur kandungan cytotec , terbukti dengan cepat untuk cara mencegah kehamilan . Maka dari itu pilihlah obat aborsi sangat ampuh . Jika anda telah haid obat telat datang bulan juga manjur , kami jual obat aborsi ini secara online http://readthedocs.org/projects/cara-menggugurkan-hamil/

    ReplyDelete

  5. Some us know all relating to the compelling medium you present powerful steps on this blog and therefore strongly encourage
    contribution from other ones on this subject while our own child is truly discovering a great deal.
    Have fun with the remaining portion of the year.
    Selenium training in bangalore
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  6. Only here are the best winnings Online casinos Gaming casino.

    ReplyDelete
  7. Great post. I was once checking constantly this weblog and I'm impressed! Extremely useful information specially the closing part. I maintain such information much. I was once seeking this specific information for a very long time. Many thanks and best of luck.
    apple service center chennai
    apple service center in chennai
    apple mobile service centre in chennai
    apple service center near me

    ReplyDelete
  8. Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.
    Thanks & Regards,
    VRIT Professionals,
    No.1 Leading Web Designing Training Institute In Chennai.

    And also those who are looking for
    Web Designing Training Institute in Chennai
    SEO Training Institute in Chennai
    Photoshop Training Institute in Chennai
    PHP & Mysql Training Institute in Chennai
    Android Training Institute in Chennai

    ReplyDelete
  9. The threadStatic attribute is just useful when anyone control the threadPool as well the lifecycle of the threads. It reminds one to think about variable scope/lifetime especially in the context of ASP.NET. online password generator

    ReplyDelete
  10. I drink 2 litres of flavoured milk every day and 2 protein shakes each with 40g protein. war machines hack

    ReplyDelete
  11. zombie gunship survival hack Ur editing is next level man. I hv also seen brawl stars by ea. But this is fantastic

    ReplyDelete
  12. mini golf king hack I am a BIG FAN of getting up at 5AM. It's perfect for quiet time, CREATIVE time and connection time. I hope to make videos as GREAT as yours in the near future. (I just created my first 7 day EPIC LIFE challenge

    ReplyDelete
  13. Thank you for this, definitely will try out some of these just to try getting a summer body for our August beach trip!
    war machines hack

    ReplyDelete
  14. After a year, analysing all kinds of diets, finally understood that we should eat normal foods that are grown locally from our native home town... no packed or imported foods. This is the trick but nobody ill like this comment!! factory inc hack

    ReplyDelete

  15. Hey, would you mind if I share your blog with my twitter group? There’s a lot of folks that I think would enjoy your content. Please let me know. Thank you.

    blockchain training in chennai | best blockchain training in chennai | blockchain coaching in chennai

    ReplyDelete
  16. Our writing company is the most college research papers company and we are always ready to answer any form of questions which may come up especially those seeking for best writing services online.

    ReplyDelete
  17. Excellent Blog. I really want to admire the quality of this post. I like the way of your presentation of ideas, views and valuable content. No doubt you are doing great work. I’ll be waiting for your next post. Thanks .Keep it up!
    Kindly visit us @
    Luxury Boxes
    Premium Packaging
    Luxury Candles Box
    Earphone Packaging Box
    Wireless Headphone Box
    Innovative Packaging Boxes
    Wedding gift box
    Leather Bag Packaging Box
    Cosmetics Packaging Box
    Luxury Chocolate Boxes

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

    ReplyDelete

  19. I recently visited your blog and it is a very impressive blog and you have got some interesting details in this post. Provide enough knowledge for me. Thank you for sharing the useful post and Well do...
    Corporate Training in Chennai
    Corporate Training
    Power BI Training in Chennai
    Unix Training in Chennai
    Linux Training in Chennai
    Pega Training in Chennai
    Oracle DBA Training in Chennai
    job Openings in chennai
    Corporate Training in Porur
    Corporate Training in T Nagar

    ReplyDelete