mirror of
https://github.com/apple/swift-nio
synced 2026-11-25 04:47:49 +00:00
Motivation: Many uses of atomics restrict themselves to using atomic variables within objects that already have reference semantics (i.e. classes) and have appropriate lifetime management. In this case, the current NIO Atomic performs an unnecessary heap allocation in order to get itself a reference count that it can use to free the underlying heap memory. This is an unnecessary allocation that can often be elided, especially in situations where many such atomics may be needed. Thus, we should provide the ability to manually manage the lifetime of the heap-allocated memory for the atomic, and change some uses of it in our codebase to reduce overhead. Modifications: 1. Moved most of the code of Atomic to a new struct, UnsafeEmbeddedAtomic. 2. Reimplemented Atomic in terms of UnsafeEmbeddedAtomic. 3. Updated EventLoopFuture to use an UnsafeEmbeddedAtomic for isFulfilled. Result: 1/3 as many allocations per EventLoopFuture.