[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Trampolines

At 5:37 PM -0400 5/12/03, Mike Newhall wrote:
>	Now that I think of it thread local storage could easily be 
>simulated by passing the pointer to the shared resource down the 
>call chain as an extra parameter.  The first guy that gets a null 
>pointer knows it's his responsibility to allocate it, then he passes 
>it in any call to a client who needs it.  Sounds like a job for an 

This does have some performance impact associated with it, which you 
do need to be careful of. (Well, if you care :) Passing an extra 
parameter has a non-zero cost associated with it, and the smaller 
your functions/methods/subs are the higher that cost climbs relative 
to the execution time of the actual code. Checking for a null value 
on every use is also potentially non-trivial, as it means a test and 
branch set, generally flushing your CPU's pipeline. Also not 
necessarily a huge cost, but once again it can add up pretty quickly.

This is one of the reasons for thread-local storage in the first 
place--that's generally stuck someplace where the OS/CPU/MMU 
(depending on your CPU architecture and OS) can cheat wildly for 
performance reasons, generally in ways that user code can't. Carrying 
around thread-local storage generally has no cost (though using it 
can have a significantly non-zero cost in some cases) so you're best 
using it for not-too-frequently-used data.

--------------------------------------"it's like this"-------------------
Dan Sugalski                          even samurai
dan@sidhe.org                         have teddy bears and even
                                       teddy bears get drunk