mirror of
https://github.com/apple/swift-nio
synced 2026-11-22 04:30:41 +00:00
Motivation: Right now we store our selectable event loop reference in a NIO ThreadSpecificVariable. That's nice enough, but this has become a hot code path in cases where users are using the complete concurrency override. In those cases, the ThreadSpecificVariable forces us to both call `pthread_getspecific` and to end up needing to perform a dynamic cast to our expected type. These dynamic casts are costly: in one sample project where we spent fully 7.84% of the runtime in dispatching tasks onto the EL, the cost of that dynamic cast _alone_ was 0.5% of the runtime, with pthread_getspecific being another 0.2%. This is 10% of the cost of dispatching a job, entirely unnecessarily. Modifications: - Use some fancy C thread-locals, which have much better performance: see #3302 for more on that. - Use Unamanged to pass the self reference into and out of the thread-local, avoiding dynamic casts. Result: Further improved performance for concurrency takeover. Before this change: ``` any() from event loop thread (fast path): 30.654833 ns/iteration any() from outside (fallback path): 17.881084 ns/iteration ``` After this change: ``` any() from event loop thread (fast path): 11.571958 ns/iteration any() from outside (fallback path): 17.52175 ns/iteration ``` Nearly a 3x speedup on that particular operation. Not so bad. Note the irony that before this change it was actually more expensive to call `any` from _on_ an EL thread than off it. Both calls had to call pthread_getspecific, which means all of this cost was the runtime type conversion.