Is there a big performance hit in frequently retrieving an object from thread local storage? (Assume that it's stored once per thread and never changed but is retrieved many times)
Is there a big performance hit in frequently retrieving an object from thread local storage? (Assume that it's stored once per thread and never changed but is retrieved many times)
Define "big", "frequently", and "many". If you have an existing program about which you are wondering, profile it and see if there is indeed a penalty.
What architecture is this on?
Most x86 OS's use FS or GS segment register as a pointer to the TLS base, so the access to get the TLS pointer will require an extra attribute, but it shouldn't make much difference compared to using a pointer to get the data in the first place. I doubt you could measure the difference here.
If the TLS base is not in a segment register, then it may require a bit more "effort" from the processor to dig out the correct data.
For example, in some systems, it involves a "GetThreadId()" system call, and then an index into a table - that could easily take a few hundred or more clock-cycles.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.