Memory footprint of UnsafeAtomic

It does. Swift (very deliberately!) does not provide a reliable way to get this address, as accessing ivars through unsafe pointers defeats Swift's elaborate exclusivity checking infrastructure. Swift's model for variable accesses is at the heart of its concurrency strategy; allowing code to randomly escape it in an ad hoc way is not a good idea. (We have learned this in the old Exposing the Memory Locations of Class Instance Variables thread.)

The current implementation of struct Atomic relies on the non-public, experimental @_rawLayout attribute that implements an evolved variant of @Joe_Groff's @RawStorage suggestion from the same thread. It would be possible to use it to make this a first-class, public construct by e.g. providing a Swift equivalent of something like Rust's Cell and UnsafeCell types -- however, properly designing this is future work.

9 Likes