<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></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 Oct 17, 2016, at 12:40 PM, John McCall <<a href="mailto:rjmccall@apple.com" class="">rjmccall@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">On Oct 17, 2016, at 10:19 AM, Joe Groff via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:<br class=""><blockquote type="cite" class="">On Oct 17, 2016, at 9:57 AM, Michael Gottesman <<a href="mailto:mgottesman@apple.com" class="">mgottesman@apple.com</a>> wrote:<br class=""><br class=""><blockquote type="cite" class=""><br class="">On Oct 17, 2016, at 9:42 AM, Joe Groff via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:<br class=""><br class=""><br class=""><blockquote type="cite" class="">On Oct 16, 2016, at 1:10 PM, Dave Abrahams via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:<br class=""><br class=""><br class="">on Thu Oct 13 2016, Joe Groff <<a href="http://swift-dev-AT-swift.org" class="">swift-dev-AT-swift.org</a>> wrote:<br class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class="">On Oct 13, 2016, at 1:18 PM, Greg Parker <<a href="mailto:gparker@apple.com" class="">gparker@apple.com</a>> wrote:<br class=""><br class=""><br class=""><blockquote type="cite" class="">On Oct 13, 2016, at 10:46 AM, John McCall via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:<br class=""><br class=""></blockquote></blockquote><br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">On Oct 13, 2016, at 9:04 AM, Joe Groff via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:<br class=""><br class=""><blockquote type="cite" class="">On Mar 1, 2016, at 1:33 PM, Joe Groff via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:<br class=""><br class="">In swift_retain/release, we have an early-exit check to pass<br class="">through a nil pointer. Since we're already burning branch, I'm<br class="">thinking we could pass through not only zero but negative pointer<br class="">values too on 64-bit systems, since negative pointers are never<br class="">valid userspace pointers on our 64-bit targets. This would give<br class="">us room for tagged-pointer-like optimizations, for instance to<br class="">avoid allocations for tiny closure contexts.<br class=""></blockquote><br class="">I'd like to resurrect this thread as we look to locking down the<br class="">ABI. There were portability concerns about doing this unilaterally<br class="">for all 64-bit targets, but AFAICT it should be safe for x86-64<br class="">and Apple AArch64 targets. The x86-64 ABI limits the userland<br class="">address space, per section 3.3.2:<br class=""><br class="">Although the AMD64 architecture uses 64-bit pointers,<br class="">implementations are only required to handle 48-bit<br class="">addresses. Therefore, conforming processes may only use addresses<br class="">from 0x00000000 00000000 to 0x00007fff ffffffff.<br class=""><br class="">Apple's ARM64 platforms always enable the top-byte-ignore<br class="">architectural feature, restricting the available address space to<br class="">the low 56 bits of the full 64-bit address space in<br class="">practice. Therefore, "negative" values should never be valid<br class="">user-space references to Swift-refcountable objects. Taking<br class="">advantage of this fact would enable us to optimize small closure<br class="">contexts, Error objects, and, if we move to a reference-counted<br class="">COW model for existentials, small `Any` values, which need to be<br class="">refcountable for ABI reasons but don't semantically promise a<br class="">unique identity like class instances do.<br class=""></blockquote><br class="">This makes sense to me. if (x <= 0) return; should be just as cheap as is (x == 0) return;<br class=""></blockquote><br class="">Conversely, I wanted to try to remove such nil checks. Currently<br class="">they look haphazard: some functions have them and some do not.<br class=""><br class="">Allowing ABI space for tagged pointer objects is a much bigger<br class="">problem than the check in swift_retain/release. For example, all<br class="">vtable and witness table dispatch sites to AnyObject or any other<br class="">type that might someday have a tagged pointer subclass would need to<br class="">compile in a fallback path now. You can't dereference a tagged<br class="">pointer to get its class pointer.<br class=""></blockquote><br class="">True. I don't think we'd want to use this optimization for class<br class="">types; I was specifically thinking of other things for which we use<br class="">nullable refcounted representations, particularly closure<br class="">contexts. The ABI for function types requires the context to be<br class="">refcountable by swift_retain/release, but it doesn't necessarily have<br class="">to be a valid pointer, if the closure formation site and invocation<br class="">function agree on a tagged-pointer representation.<span class="Apple-converted-space"> </span><br class=""></blockquote><br class="">Well, but we'd like to take advantage of the same kind of optimization<br class="">for the small string optimization. It doesn't seem like this should be<br class="">handled differently just because the string buffer is a class instance<br class="">and not a closure context.<br class=""></blockquote><br class="">String is a struct, and small strings don't have to be modeled as class instances. An enum { case Big(StringStorage), Small(Int63) } or similar layout should be able to take advantage of swift_retain/release ignoring negative values too.<br class=""></blockquote><br class="">I need to catch up on this thread, but there is an important thing to remember. If you use an enum like this there are a few potential issues:<br class=""><br class="">1. In the implementation, you will /not/ want to use the enum internally. This would prevent the optimizer from eliminating all of the Small Case reference counting operations. This means you would rewrap the internal value when you return one and when you enter into an internal implementation code path try to immediately switch to a specialized small case path if you can.<br class=""></blockquote><br class="">This poses an interesting question for the semantic ARC model with enums. It seems to me that, if switching or projecting the payload of an enum was a consuming operation, that we could avoid this optimization pitfall. Switching the enum { case Big(Class), Small(Trivial) } or similar case would semantically eliminate the nontrivial enum value and leave only the trivial payload behind.<br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">We can't make it *exclusively* a consuming operation; it has to be possible to switch on a borrowed value.</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></div></blockquote><div><br class=""></div><div>Yes. Perhaps the right way to think about it is in the context of considering putting conventions on SILArguments/Terminators. Then you have a natural way to express this and could optimize (potentially) in between such forms.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">John.</span></div></div></blockquote></div><br class=""></body></html>