<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 15, 2016, at 10:58 AM, Joe Groff <<a href="mailto:jgroff@apple.com" class="">jgroff@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Swift heap object headers are fairly large—16 bytes on 64-bit, and 12 bytes on 32-bit. Into this space we pack:<div class=""><br class=""></div><div class="">- the 'isa' pointer for the object, pointing to its heap metadata/class object,</div><div class="">- the strong and unowned reference counts,</div><div class="">- 'pinned' and 'deallocating' flags.</div><div class=""><br class=""></div><div class="">We've also discussed taking a flag bit for 'not refcounted' objects, such as statically-allocated globals and/or stack promotions that need to be ABI compatible with heap objects, and potentially one for thread-local objects, to avoid barriers when refcounting objects we dynamically know are not referenced from multiple threads. We should consider whether we can reduce the header size. Two ideas come to mind:</div><div class=""><br class=""></div><div class=""><b class="">Dropping the unowned reference count</b></div><div class=""><b class=""><br class=""></b></div><div class="">If we adopt a sufficiently fast implementation for normal weak references, such as the activity count implementation suggested by Kevin and Mike, the unowned reference count might not be worth the expense. If we dropped it, that would be enough to bring the 32-bit object header down to 8 bytes. The tradeoff would be that unowned references become fatter, like weak references would, which might complicate our plans to eventually allow unowned to transparently become unowned(unsafe) in unchecked builds.</div><div class=""><br class=""></div><div class=""><b class="">Non-pointer isa for 64-bit platforms</b></div><div class=""><b class=""><br class=""></b></div><div class="">Neither x86-64 nor ARM64 populates the full 64 bits of address space—contemporary x86-64 uses only 48 bits (sign-extended, so effectively 47 bits for userspace), and Apple ARM64 platforms use fewer bits, the exact number dependent on OS version. If we were willing to drop the unowned refcount, and say that "64Ki-retains ought to be enough for anyone", overflowing the retain count into the "not refcounted" bit to leak overly-retained objects, </div></div></div></blockquote><div><br class=""></div><div>64K retains is not enough for everybody. NSParagraphStyle had a 19-bit inline retain count with no overflow protection (i.e. it incorrectly deallocated if you retained it too much and then called some releases). This occasionally crashed in Xcode (<a href="rdar://16008112" class="">rdar://16008112</a>).</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">we could use a layout similar to this to pack the remaining information into 8 bytes:</div><div class=""><br class=""></div><div class=""><div class=""><font face="Courier New" class="">bits meaning</font></div><div class=""><font face="Courier New" class="">----- -------</font></div><div class=""><font face="Courier New" class="">63 not refcounted</font></div><div class=""><font face="Courier New" class="">47…62 strong refcount</font></div><div class=""><font face="Courier New" class="">03…46 metadata pointer</font></div><div class=""><font face="Courier New" class="">02 (reserved)</font></div><div class=""><font face="Courier New" class="">01 deallocating</font></div><div class=""><font face="Courier New" class="">00 pinned</font></div></div></div></div></blockquote><div><br class=""></div><div>What is the difference between "pinned" and "not refcounted" ? I would expect that you only need one bit to mark objects that are constant or whose refcount has overflowed.</div><div><br class=""></div><div>Note that the memory analysis folks really want a few bits reserved with a constant value. That improves their reliability when distinguishing real objects from non-object memory that happens to have an isa-like field in front. ObjC currently gives them 6 bits on all architectures.</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">There are of course some costs and complications. For classes, we look up vtable entries and resilient ivar offsets through the isa pointer, and masking the isa costs an extra instruction per object, though that can at least be shared for multiple method calls on the same object since we assume objects don't change class (at least not in ways that would change Swift method implementation or ivar layout). We do already pay this cost on Apple platforms for NSObject subclasses. More interestingly, Objective-C already uses non-pointer isa on ARM64, but not on x86-64.</div></div></div></blockquote><br class=""></div><div>Objective-C now uses non-pointer isa on x86_64 (as of OS X 10.11 iirc).</div><div><br class=""></div><div><br class=""></div><div><blockquote type="cite" class="">I'm not sure how flexible the ObjC implementation is here—Could Swift use non-pointer isas on platforms where ObjC doesn't? Could it ascribe different meanings to the bits from ObjC's?</blockquote></div><br class=""><div class="">For backwards deployment, no. libobjc currently assumes that every isa field is either a raw class pointer or libobjc's packed representation. </div><div class=""><br class=""></div><div class="">With libobjc's cooperation, maybe. You would almost certainly need everybody to use the same mask value. If the other bits mean different things in Swift vs ObjC then you would also need to use a bit to distinguish the representations.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">How does the object header size interact with resilience? If we squeeze into an 8-byte header and then regret it later, will we have any recourse?</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">-- </div><div class="">Greg Parker <a href="mailto:gparker@apple.com" class="">gparker@apple.com</a> Runtime Wrangler</div><div class=""><br class=""></div><div class=""><br class=""></div></body></html>