Performance Quiz #10 -- Thread local storage
It's time for another quiz!
A very short one this time. If I need some thread local storage and I might need several entries should I use:
Thread.GetData(slot) and Thread.SetData(slot, object)
or should I make my own static member like this
[ThreadStatic]
static Dictionary<String,Object> myItems = new Dictionary<String,Object>;
and get things through the dictionary?
Warning: spoilers below in the comments ... already :) Maybe my quizzes are too easy :)
Comments
- Anonymous
June 16, 2006
Hi Rico,
I think you should use the second attributed variant. Thread.GetData takes a process wide lock if I remember right which is a know scalability issue of .NET 2.0.
Full Post is here: http://72.14.221.104/search?q=cache:czogGnWiI20J:blogs.gotdotnet.com/junfeng/comments/508423.aspx+%22junfeng%22+Thread.GetData&hl=de&gl=de&ct=clnk&cd=1
By the way I find the .NET Trace class has some design flaws in terms of performance and reliablity: http://geekswithblogs.net/akraus1/articles/81629.aspx
Yours,
Alois Kraus - Anonymous
June 16, 2006
Regardless of the bug I did mention above I think it is better to get the reference to the TLS data only once since it does cost more than a simple array/Hashtable access. - Anonymous
June 16, 2006
I'm going to have to add a spoilers warning now :) - Anonymous
June 16, 2006
Since your quiz was anounced as short I thought my answer could also be short and concise ;-).
By the way Greg Young has some really great answers to very tricky questions regarding for loops and JIT optimizations at assembly (not MSIL) level:
http://codebetter.com/blogs/gregyoung/archive/2006/06/11/146343.aspx
Your blog is a great resource to advance my still incomplete knowledge about .NET. But it seems I have already made some progress ;-).
Yours,
Alois Kraus - Anonymous
June 18, 2006
And yet the Win32 TLS APIs do not take a global lock. Or at least, they shouldn't need to: certainly Wines implementation of it doesn't and neither does ELF TLS. So what went wrong here? - Anonymous
June 19, 2006
According to the MSDN documentation, you shouldn't use inline initializers with static variables that bear the [ThreadStatic] attribute.
From the MSDN entry:
"Do not specify initial values for fields marked with ThreadStaticAttribute, because such initialization occurs only once, when the class constructor executes, and therefore affects only one thread." - Anonymous
July 18, 2006
I actually posted quiz #10 quite a while ago but a comment with the correct solution came in so quickly... - Anonymous
December 21, 2006
I actually posted quiz #10 quite a while ago but a comment with the correct solution came in so quickly