How much is read from ThreadLocal variable slower than from regular field?
More concretely is simple object creation faster or slower than access to ThreadLocal variable?
I assume that it is fast enough so that having ThreadLocal<MessageDigest> instance is much faster then creating instance of MessageDigest every time. But does that also apply for byte[10] or byte[1000] for example?
Edit: Question is what is really going on when calling ThreadLocal‘s get? If that is just a field, like any other, then answer would be ‘it’s always fastest’, right?
Running unpublished benchmarks,
ThreadLocal.gettakes around 35 cycle per iteration on my machine. Not a great deal. In Sun’s implementation a custom linear probing hash map inThreadmapsThreadLocals to values. Because it is only ever accessed by a single thread, it can be very fast.Allocation of small objects take a similar number of cycles, although because of cache exhaustion you may get somewhat lower figures in a tight loop.
Construction of
MessageDigestis likely to be relatively expensive. It has a fair amount of state and construction goes through theProviderSPI mechanism. You may be able to optimise by, for instance, cloning or providing theProvider.Just because it may be faster to cache in a
ThreadLocalrather than create does not necessarily mean that the system performance will increase. You will have additional overheads related to GC which slows everything down.Unless your application very heavily uses
MessageDigestyou might want to consider using a conventional thread-safe cache instead.