netty/netty — pull request #2504 by belliottsmith (2014-05-19 08:13:22+00:00)
Title: FastThreadLocal and faster Recycler.recycle()

I can rebase this against 5.0 if the patch is considered worth including. It encompasses a couple of changes:

1) a "FastThreadLocal" which uses an EnumMap and a predefined fixed set of possible thread locals (all of the static instances created by netty) that is around 10-20% faster than standard ThreadLocal in my benchmarks (and can be seen having an effect in the direct PooledByteBufAllocator benchmark that uses the DEFAULT ByteBufAllocator which uses this FastThreadLocal, as opposed to normal instantiations that do not, and in the new RecyclableArrayList benchmark);

2) A larger change is the Recycler now uses a BitMap for managing presence in the stack (so that with assertions enabled we are 30-50% faster), assigning per-recyclable-object ids, and we now relatively cheaply recycles objects from all threads by placing them on a queue with minimal synchronization (well, we could reduce it slightly further, but it's pretty close to minimal). The micro-benchmark here could do with some more work, but it's quite difficult to test the performance of the result as a benchmark is inherently much more contended than such a feature would be ordinarily, and the weak-visibility guarantees do not save us much in a benchmark since we have to perform thread synchronisation and signalling to avoid wasting many cycles and distorting the results.

Let me know what you think and, if you're interested, we can tidy it up for merge with 4.1 and 5.0.