<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:01 PM, Michael Gottesman <<a href="mailto:mgottesman@apple.com" class="">mgottesman@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Oct 17, 2016, at 11:53 AM, Michael Gottesman via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Oct 17, 2016, at 10:00 AM, Joe Groff <<a href="mailto:jgroff@apple.com" class="">jgroff@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><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=""><br class=""><blockquote type="cite" 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. <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="">2. {Retain,Release}Values will be created outside. We are talking about some ways of fixing this from a code-size perspective by using a value witness, but in the present this may cause additional code-size increase.<br class=""></blockquote><br class="">This is exactly the case that would be improved, since retain/release_value on such an enum would boil down to a single swift_retain/release call if the runtime functions ignored the tagged small case values.<br class=""></div></div></blockquote><div class=""><br class=""></div><div class="">I am saying something stronger. What I am saying is that, you could have <b class="">0</b> <b class="">retain/release</b> operations on the SmallString path.</div></div></div></div></blockquote><div class=""><br class=""></div><div class="">Let me elaborate a little bit, and then I am going to drop my point here since as Joe pointed out to me offlist, this is orthogonal to the ABI discussion.</div><div class=""><br class=""></div><div class="">What I am trying to say is that the optimizer will eliminate all retain, release operations on trivial values. Any code path which uses the top level enum can not take advantage of this property since the top level enum /could/ have the BigString contained in it. So what you want to do to get rid of the most retain/release operations is to move the enum switch to the entrances of the API so that one has the largest region of code where the optimizer can clearly see that it has a small string.</div><div class=""><br class=""></div><div class="">Now we /could/ specialize on enum cases. I will file a radar for this.</div></div></div></div></blockquote><div><br class=""></div><div><a href="rdar://28805035" class="">rdar://28805035</a></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=""><div class=""><br class=""></div><div class="">Michael</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=""><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class="">-Joe<br class=""><br class=""><blockquote type="cite" class=""><br class=""><br class=""><blockquote type="cite" class=""><br class="">-Joe<br class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class="">We could also do interesting things with enums; if one payload type is<br class="">a class reference and the rest are trivial, we could lay the enum out<br class="">in such a way that we can use swift_retain/release on it by setting<br class="">the high bit when tagging the trivial representations, saving us the<br class="">need to emit a switch. We wouldn't actually dereference the pointer<br class="">representation without checking it first.<br class=""><br class="">I know we've discussed taking the nil check out of<br class="">swift_retain/release, and possibly having separate variants that do<br class="">include the null check for when we know we're working with<br class="">Optionals. How much of difference would that really make, though? I'd<br class="">expect it to be a fairly easily predictable branch, since most objects<br class="">are likely to be nonnull in practice.<br class=""><br class="">-Joe<br class="">_______________________________________________<br class="">swift-dev mailing list<br class=""><a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-dev" class="">https://lists.swift.org/mailman/listinfo/swift-dev</a><br class=""></blockquote><br class="">-- <br class="">-Dave<br class=""><br class="">_______________________________________________<br class="">swift-dev mailing list<br class=""><a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-dev" class="">https://lists.swift.org/mailman/listinfo/swift-dev</a><br class=""></blockquote><br class="">_______________________________________________<br class="">swift-dev mailing list<br class=""><a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-dev" class="">https://lists.swift.org/mailman/listinfo/swift-dev</a><br class=""></blockquote><br class=""></blockquote><br class=""></div></div></blockquote></div><br class=""></div>_______________________________________________<br class="">swift-dev mailing list<br class=""><a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-dev" class="">https://lists.swift.org/mailman/listinfo/swift-dev</a><br class=""></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></body></html>