<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="">Hi everyone!<div class=""><br class=""></div><div class="">Here is the proposal to allow access to the underlying collections of slices. Existing API of slice types is very minimal and as such prevents possible optimizations. Exposing the base collection via a public readonly property will make such optimizations possible.</div><div class=""><br class=""></div><div class="">Max</div><div class=""><br class=""></div><div class=""><h1 id="adding-a-public-base-property-to-slices" style="line-height: 1.1; color: rgb(51, 51, 51); font-family: Arial, Helvetica, sans-serif;" class="">Adding a public <code style="font-family: Menlo, Consolas, 'Ubuntu Mono', Monaco, source-code-pro, monospace; line-height: 1.4; margin: 0px; padding: 0.2em 0px; font-size: 24px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">base</code> property to slices</h1><ul style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, sans-serif;" class=""><li class="">Proposal: SE-NNNN</li><li class="">Author(s): <a href="https://github.com/moiseev" class="">Max Moiseev</a></li><li class="">Status: <strong class="">Awaiting review</strong></li><li class="">Review manager: TBD</li></ul><h2 id="introduction" style="line-height: 1.1; color: rgb(51, 51, 51); font-family: Arial, Helvetica, sans-serif;" class="">Introduction</h2><p style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, sans-serif;" class="">Slice types <a href="https://github.com/apple/swift/blob/master/stdlib/public/core/Slice.swift.gyb" class="">provided by the standard library</a> should allow public readonly access to their base collections to make efficient implementations of protocol requirements possible in conforming types.</p><h2 id="motivation" style="line-height: 1.1; color: rgb(51, 51, 51); font-family: Arial, Helvetica, sans-serif;" class="">Motivation</h2><p style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, sans-serif;" class="">The <code style="font-family: Menlo, Consolas, 'Ubuntu Mono', Monaco, source-code-pro, monospace; line-height: 1.4; margin: 0px; padding: 0.2em 0px; font-size: 12px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">MutableCollection</code> protocol conformance requires providing an implementation of the following subscript:</p><pre style="color: rgb(51, 51, 51);" class=""><div class="ace-chrome" style="background-color: rgb(255, 255, 255); color: black;"><div class="ace_static_highlight ace_show_gutter" style="white-space: pre-wrap; counter-reset: ace_line 0; font-family: Menlo, Consolas, 'Ubuntu Mono', Monaco, source-code-pro, monospace; font-size: 12px;"><div class="ace_line" style="position: relative; padding-left: 2.6em;"><span class="ace_gutter ace_gutter-cell" unselectable="on" style="position: absolute; overflow: hidden; width: 2em; top: 0px; bottom: 0px; left: 0px; cursor: default; z-index: 4; -webkit-user-select: none; padding: 0px 3px 0px 0px; text-align: right; margin-right: 3px; background-color: rgb(235, 235, 235); color: rgb(51, 51, 51); background-position: initial initial; background-repeat: initial initial;"></span><span class="ace_support ace_function" style="color: rgb(60, 76, 114);">subscript</span><span class="ace_lparen ace_paren">(</span><span class="ace_tag ace_meta" style="color: rgb(147, 15, 128);">bounds</span><span class="ace_operator ace_punctuation">:</span> <span class="ace_identifier">Range</span><span class="ace_operator ace_keyword" style="color: rgb(104, 118, 135);"><</span><span class="ace_identifier">Index</span><span class="ace_operator ace_keyword" style="color: rgb(104, 118, 135);">></span><span class="ace_rparen ace_paren">)</span> <span class="ace_operator ace_keyword" style="color: rgb(104, 118, 135);">-></span> <span class="ace_identifier">SubSequence</span> <span class="ace_lparen ace_paren">{</span> <span class="ace_keyword" style="color: rgb(147, 15, 128);">get</span> <span class="ace_keyword" style="color: rgb(147, 15, 128);">set</span> <span class="ace_rparen ace_paren">}</span>
</div></div></div></pre><p style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, sans-serif;" class="">If the collection chooses to use one of a variety of slice types from the standard library as its <code style="font-family: Menlo, Consolas, 'Ubuntu Mono', Monaco, source-code-pro, monospace; line-height: 1.4; margin: 0px; padding: 0.2em 0px; font-size: 12px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">SubSequence</code>, the default implementation of a setter for this subscript will use the algorithm provided by the <a href="https://github.com/apple/swift/blob/master/stdlib/public/core/WriteBackMutableSlice.swift" class=""><code style="font-family: Menlo, Consolas, 'Ubuntu Mono', Monaco, source-code-pro, monospace; line-height: 1.4; margin: 0px; padding: 0.2em 0px; font-size: 12px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">_writeBackMutableSlice</code></a> function. This approach is fine for forward collections. It is quite possible, however, that the most efficient implementation of this setter would be to simply call the <code style="font-family: Menlo, Consolas, 'Ubuntu Mono', Monaco, source-code-pro, monospace; line-height: 1.4; margin: 0px; padding: 0.2em 0px; font-size: 12px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">memcpy</code> function. Unfortunately, slice API does not provide any way to reach to the underlying base collection, even though reference to it is stored in an internal property.</p><h2 id="proposed-solution" style="line-height: 1.1; color: rgb(51, 51, 51); font-family: Arial, Helvetica, sans-serif;" class="">Proposed solution</h2><p style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, sans-serif;" class="">We propose to export a public readonly property <code style="font-family: Menlo, Consolas, 'Ubuntu Mono', Monaco, source-code-pro, monospace; line-height: 1.4; margin: 0px; padding: 0.2em 0px; font-size: 12px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">base</code>, that will enable optimizations mentioned above. Here is how <code style="font-family: Menlo, Consolas, 'Ubuntu Mono', Monaco, source-code-pro, monospace; line-height: 1.4; margin: 0px; padding: 0.2em 0px; font-size: 12px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">MutableRandomAccessSlice</code> definition would look like:</p><pre style="color: rgb(51, 51, 51);" class=""><div class="ace-chrome" style="background-color: rgb(255, 255, 255); color: black;"><div class="ace_static_highlight ace_show_gutter" style="white-space: pre-wrap; counter-reset: ace_line 0; font-family: Menlo, Consolas, 'Ubuntu Mono', Monaco, source-code-pro, monospace; font-size: 12px;"><div class="ace_line" style="position: relative; padding-left: 2.6em;"><span class="ace_gutter ace_gutter-cell" unselectable="on" style="position: absolute; overflow: hidden; width: 2em; top: 0px; bottom: 0px; left: 0px; cursor: default; z-index: 4; -webkit-user-select: none; padding: 0px 3px 0px 0px; text-align: right; margin-right: 3px; background-color: rgb(235, 235, 235); color: rgb(51, 51, 51); background-position: initial initial; background-repeat: initial initial;"></span><span class="ace_storage ace_type" style="color: rgb(147, 15, 128);">public</span> <span class="ace_keyword" style="color: rgb(147, 15, 128);">struct</span> <span class="ace_identifier">MutableRandomAccessSlice</span><span class="ace_operator ace_keyword" style="color: rgb(104, 118, 135);"><</span>
</div><div class="ace_line" style="position: relative; padding-left: 2.6em;"><span class="ace_gutter ace_gutter-cell" unselectable="on" style="position: absolute; overflow: hidden; width: 2em; top: 0px; bottom: 0px; left: 0px; cursor: default; z-index: 4; -webkit-user-select: none; padding: 0px 3px 0px 0px; text-align: right; margin-right: 3px; background-color: rgb(235, 235, 235); color: rgb(51, 51, 51); background-position: initial initial; background-repeat: initial initial;"></span> <span class="ace_tag ace_meta" style="color: rgb(147, 15, 128);">Base</span> <span class="ace_operator ace_punctuation">:</span> <span class="ace_keyword" style="color: rgb(147, 15, 128);">protocol</span><span class="ace_operator ace_keyword" style="color: rgb(104, 118, 135);"><</span><span class="ace_identifier">RandomAccessIndexable</span><span class="ace_operator ace_punctuation">,</span> <span class="ace_identifier">MutableIndexable</span><span class="ace_operator ace_keyword" style="color: rgb(104, 118, 135);">></span>
</div><div class="ace_line" style="position: relative; padding-left: 2.6em;"><span class="ace_gutter ace_gutter-cell" unselectable="on" style="position: absolute; overflow: hidden; width: 2em; top: 0px; bottom: 0px; left: 0px; cursor: default; z-index: 4; -webkit-user-select: none; padding: 0px 3px 0px 0px; text-align: right; margin-right: 3px; background-color: rgb(235, 235, 235); color: rgb(51, 51, 51); background-position: initial initial; background-repeat: initial initial;"></span><span class="ace_operator ace_keyword" style="color: rgb(104, 118, 135);">></span> <span class="ace_operator ace_punctuation">:</span> <span class="ace_identifier">RandomAccessCollection</span><span class="ace_operator ace_punctuation">,</span> <span class="ace_identifier">MutableCollection</span> <span class="ace_lparen ace_paren">{</span>
</div><div class="ace_line" style="position: relative; padding-left: 2.6em;"><span class="ace_gutter ace_gutter-cell" unselectable="on" style="position: absolute; overflow: hidden; width: 2em; top: 0px; bottom: 0px; left: 0px; cursor: default; z-index: 4; -webkit-user-select: none; padding: 0px 3px 0px 0px; text-align: right; margin-right: 3px; background-color: rgb(235, 235, 235); color: rgb(51, 51, 51); background-position: initial initial; background-repeat: initial initial;"></span>
</div><div class="ace_line" style="position: relative; padding-left: 2.6em;"><span class="ace_gutter ace_gutter-cell" unselectable="on" style="position: absolute; overflow: hidden; width: 2em; top: 0px; bottom: 0px; left: 0px; cursor: default; z-index: 4; -webkit-user-select: none; padding: 0px 3px 0px 0px; text-align: right; margin-right: 3px; background-color: rgb(235, 235, 235); color: rgb(51, 51, 51); background-position: initial initial; background-repeat: initial initial;"></span> <span class="ace_comment" style="color: rgb(35, 110, 36);">/// The underlying collection of the slice</span>
</div><div class="ace_line" style="position: relative; padding-left: 2.6em;"><span class="ace_gutter ace_gutter-cell" unselectable="on" style="position: absolute; overflow: hidden; width: 2em; top: 0px; bottom: 0px; left: 0px; cursor: default; z-index: 4; -webkit-user-select: none; padding: 0px 3px 0px 0px; text-align: right; margin-right: 3px; background-color: rgb(235, 235, 235); color: rgb(51, 51, 51); background-position: initial initial; background-repeat: initial initial;"></span> <span class="ace_storage ace_type" style="color: rgb(147, 15, 128);">public</span> <span class="ace_keyword" style="color: rgb(147, 15, 128);">var</span> <span class="ace_tag ace_meta" style="color: rgb(147, 15, 128);">base</span><span class="ace_operator ace_punctuation">:</span> <span class="ace_identifier">Base</span> <span class="ace_lparen ace_paren">{</span> <span class="ace_keyword" style="color: rgb(147, 15, 128);">get</span> <span class="ace_rparen ace_paren">}</span>
</div><div class="ace_line" style="position: relative; padding-left: 2.6em;"><span class="ace_gutter ace_gutter-cell" unselectable="on" style="position: absolute; overflow: hidden; width: 2em; top: 0px; bottom: 0px; left: 0px; cursor: default; z-index: 4; -webkit-user-select: none; padding: 0px 3px 0px 0px; text-align: right; margin-right: 3px; background-color: rgb(235, 235, 235); color: rgb(51, 51, 51); background-position: initial initial; background-repeat: initial initial;"></span><span class="ace_rparen ace_paren">}</span>
</div></div></div></pre><p style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, sans-serif;" class="">The same change is applicable to both mutable and immutable slice types.</p><h2 id="impact-on-existing-code" style="line-height: 1.1; color: rgb(51, 51, 51); font-family: Arial, Helvetica, sans-serif;" class="">Impact on existing code</h2><p style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, sans-serif;" class="">The proposed change is purely additive and does not affect existing code.</p><h2 id="alternatives-considered" style="line-height: 1.1; color: rgb(51, 51, 51); font-family: Arial, Helvetica, sans-serif;" class="">Alternatives considered</h2><p style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, sans-serif;" class="">Alternative for immutable slices would be to simply rename the already read-only <code style="font-family: Menlo, Consolas, 'Ubuntu Mono', Monaco, source-code-pro, monospace; line-height: 1.4; margin: 0px; padding: 0.2em 0px; font-size: 12px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">_base</code> property to <code style="font-family: Menlo, Consolas, 'Ubuntu Mono', Monaco, source-code-pro, monospace; line-height: 1.4; margin: 0px; padding: 0.2em 0px; font-size: 12px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">base</code> and make it public, but this way the change is not purely additive and might cause some damage inside the standard library code.</p></div><div class=""><br class=""></div></body></html>