puniverse / quasar

Fibers, Channels and Actors for the JVM
http://docs.paralleluniverse.co/quasar/
Other
4.56k stars 575 forks source link

Control over ThreadLocal swapping #304

Open exFalso opened 6 years ago

exFalso commented 6 years ago

Currently all ThreadLocals become "FiberLocal"s, and quasar swaps them out during a park. Although in theory this is correct, it also means some optimisations (like thread local reuse of an expensively built object) cease to work. It would be very useful if we had some control over what to and what not to swap out.

I reckon the best way to do this would be adding a black/whitelist of ThreadLocals to the FiberScheduler?

doctorpangloss commented 6 years ago

Use a ConcurrentHashMap keyed on thread IDs. Don't overthink it :)

exFalso commented 6 years ago

The main issue is third party libs doing thread-local caching. An example would be netty using threadlocals to store buffers

doctorpangloss commented 6 years ago

Based on what I just read in the netty source, you'd have to implement your own ByteBufAllocator which uses a "TrueThreadLocal..." reimplementation of PooledByteBufAllocator.

You're right that this is a general problem though, I wasn't even aware that this sort of thrashing was occurring.