<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=""><div><blockquote type="cite" class=""><div class="">On Nov 8, 2016, at 7:44 AM, Joe Groff via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><div 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 Nov 7, 2016, at 3:55 PM, Dave Abrahams via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""><br class="">on Mon Nov 07 2016, John McCall <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class="">On Nov 6, 2016, at 1:20 PM, Dave Abrahams via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""><br class="">Given that we're headed for ABI (and thus stdlib API) stability, I've<br class="">been giving lots of thought to the bottom layer of our collection<br class=""></blockquote><br class=""><blockquote type="cite" class="">abstraction and how it may limit our potential for efficiency. In<br class="">particular, I want to keep the door open for optimizations that work on<br class="">contiguous memory regions. Every cache-friendly data structure, even if<br class="">it is not an array, contains contiguous memory regions over which<br class="">operations can often be vectorized, that should define boundaries for<br class="">parallelism, etc. Throughout Cocoa you can find patterns designed to<br class="">exploit this fact when possible (NSFastEnumeration). Posix I/O bottoms<br class="">out in readv/writev, and MPI datatypes essentially boil down to<br class="">identifying the contiguous parts of data structures. My point is that<br class="">this is an important class of optimization, with numerous real-world<br class="">examples.<br class=""><br class="">If you think about what it means to build APIs for contiguous memory<br class="">into abstractions like Sequence or Collection, at least without<br class="">penalizing the lowest-level code, it means exposing UnsafeBufferPointers<br class="">as a first-class part of the protocols, which is really<br class="">unappealing... unless you consider that *borrowed* UnsafeBufferPointers<br class="">can be made safe. <br class=""><br class="">[Well, it's slightly more complicated than that because<br class="">UnsafeBufferPointer is designed to bypass bounds checking in release<br class="">builds, and to ensure safety you'd need a BoundsCheckedBuffer—or<br class="">something—that checks bounds unconditionally... but] the point remains<br class="">that<br class=""><br class="">A thing that is unsafe when it's arbitrarily copied can become safe if<br class="">you ensure that it's only borrowed (in accordance with well-understood<br class="">lifetime rules).<br class=""></blockquote><br class="">UnsafeBufferPointer today is a copyable type. Having a borrowed value<br class="">doesn't prevent you from making your own copy, which could then escape<br class="">the scope that was guaranteeing safety.<br class=""><br class="">This is fixable, of course, but it's a more significant change to the<br class="">type and how it would be used.<br class=""></blockquote><br class="">It sounds like you're saying that, to get static safety benefits from<br class="">ownership, we'll need a whole parallel universe of safe move-only<br class="">types. Seems a cryin' shame.<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've discussed the possibility of types being able to control their "borrowed" representation. Even if this isn't something we generalize, arrays and contiguous buffers might be important enough to the language that your safe BufferPointer could be called 'borrowed ArraySlice<T>', with the owner backreference optimized out of the borrowed representation. Perhaps Array's own borrowed representation would benefit from acting like a slice rather than a whole-buffer borrow too.</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></blockquote><div><br class=""></div>The disadvantage of doing this is that it much more heavily penalizes the case where we actually do a copy from a borrowed reference — it becomes an actual array copy, not just a reference bump.</div><div><br class=""></div><div>John.</div></body></html>