As the author of SE-0012, +1 :-)<br><div class="gmail_quote"><div dir="ltr">On Sun, Feb 7, 2016 at 7:50 AM Dave Abrahams via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
on Sat Feb 06 2016, Károly Lőrentey &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br>
<br>
&gt; I’d like to request feedback on the draft proposal below, about a (hopefully) trivial change to the standard library.<br>
&gt;<br>
&gt; I considered adding it to SE-0012<br>
&gt; &lt;<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0012-add-noescape-to-public-library-api.md" rel="noreferrer" target="_blank">https://github.com/apple/swift-evolution/blob/master/proposals/0012-add-noescape-to-public-library-api.md</a>&gt;,<br>
&gt; which is also about @noescape, but that proposal is about modifying<br>
&gt; C/Obj-C API, so it’s not a good fit<br>
&gt; &lt;<a href="https://github.com/apple/swift-evolution/pull/122" rel="noreferrer" target="_blank">https://github.com/apple/swift-evolution/pull/122</a>&gt;.<br>
&gt;<br>
&gt; Introduction<br>
&gt;<br>
&gt; stdlib’s ManagedBuffer family of APIs has some public initializers and<br>
&gt; methods taking closures that are missing the @noescape attribute. The<br>
&gt; same family has a set of withUnsafeMutablePointer* functions that do<br>
&gt; not have a rethrows declaration, while all similar methods elsewhere<br>
&gt; in the standard library allow closures to throw.<br>
&gt;<br>
&gt; I propose to add the missing @noescape attributes and rethrows.<br>
<br>
On first look this seems to be a great idea.  Have you checked for<br>
performance impact?<br>
<br>
&gt; Motivation<br>
&gt;<br>
&gt; - ManagedBuffer seems designed for raw performance. Not having<br>
&gt; @noescape on the only API that allows access to the buffer’s contents<br>
&gt; defeats some compiler optimizations, such as omitting unneccessary<br>
&gt; retain/release calls. This can negate the performance advantage of<br>
&gt; ManagedBuffer over simpler solutions, like using an Array.<br>
&gt; - Accepting throwing closures makes these APIs more versatile, and<br>
&gt; also improves their consistency with other parts of stdlib.<br>
&gt;<br>
&gt; Detailed Design<br>
&gt;<br>
&gt; The following set of APIs would be affected by this proposal:<br>
&gt;<br>
&gt; public class ManagedProtoBuffer&lt;Value, Element&gt; : NonObjectiveCBase {<br>
&gt;   public final func withUnsafeMutablePointerToValue&lt;R&gt;(body: (UnsafeMutablePointer&lt;Value&gt;) -&gt; R) -&gt; R<br>
&gt;   public final func withUnsafeMutablePointerToElements&lt;R&gt;(body: (UnsafeMutablePointer&lt;Element&gt;) -&gt; R) -&gt; R<br>
&gt;   public final func withUnsafeMutablePointers&lt;R&gt;(body: (_:<br>
&gt; UnsafeMutablePointer&lt;Value&gt;, _: UnsafeMutablePointer&lt;Element&gt;) -&gt; R)<br>
&gt; -&gt; R<br>
&gt; }<br>
&gt;<br>
&gt; public class ManagedBuffer&lt;Value, Element&gt; : ManagedProtoBuffer&lt;Value, Element&gt; {<br>
&gt;   public final class func create(minimumCapacity: Int, initialValue:<br>
&gt; (ManagedProtoBuffer&lt;Value,Element&gt;) -&gt; Value) -&gt;<br>
&gt; ManagedBuffer&lt;Value,Element&gt;<br>
&gt; }<br>
&gt;<br>
&gt; public struct ManagedBufferPointer&lt;Value, Element&gt; : Equatable {<br>
&gt;   public init(bufferClass: AnyClass, minimumCapacity: Int,<br>
&gt; initialValue: (buffer: AnyObject, allocatedCount: (AnyObject) -&gt; Int)<br>
&gt; -&gt; Value)<br>
&gt;   public func withUnsafeMutablePointerToValue&lt;R&gt;(body: (UnsafeMutablePointer&lt;Value&gt;) -&gt; R) -&gt; R<br>
&gt;   public func withUnsafeMutablePointerToElements&lt;R&gt;(body: (UnsafeMutablePointer&lt;Element&gt;) -&gt; R) -&gt; R<br>
&gt;   public func withUnsafeMutablePointers&lt;R&gt;(body: (_:<br>
&gt; UnsafeMutablePointer&lt;Value&gt;, _: UnsafeMutablePointer&lt;Element&gt;) -&gt; R)<br>
&gt; -&gt; R<br>
&gt; }<br>
&gt;<br>
&gt; Here is how they would look after the proposed changes:<br>
&gt;<br>
&gt; public class ManagedProtoBuffer&lt;Value, Element&gt; : NonObjectiveCBase {<br>
&gt;   public final func withUnsafeMutablePointerToValue&lt;R&gt;(@noescape body:<br>
&gt; (UnsafeMutablePointer&lt;Value&gt;) throws -&gt; R) rethrows -&gt; R<br>
&gt;   public final func withUnsafeMutablePointerToElements&lt;R&gt;(@noescape<br>
&gt; body: (UnsafeMutablePointer&lt;Element&gt;) throws -&gt; R) rethrows -&gt; R<br>
&gt;   public final func withUnsafeMutablePointers&lt;R&gt;(@noescape body: (_:<br>
&gt; UnsafeMutablePointer&lt;Value&gt;, _: UnsafeMutablePointer&lt;Element&gt;) throws<br>
&gt; -&gt; R) rethrows -&gt; R<br>
&gt;<br>
&gt; public class ManagedBuffer&lt;Value, Element&gt; : ManagedProtoBuffer&lt;Value, Element&gt; {<br>
&gt;   public final class func create(minimumCapacity: Int, @noescape<br>
&gt; initialValue: (ManagedProtoBuffer&lt;Value,Element&gt;) -&gt; Value) -&gt;<br>
&gt; ManagedBuffer&lt;Value,Element&gt;<br>
&gt; }<br>
&gt;<br>
&gt; public struct ManagedBufferPointer&lt;Value, Element&gt; : Equatable {<br>
&gt;   public init(bufferClass: AnyClass, minimumCapacity: Int, @noescape<br>
&gt; initialValue: (buffer: AnyObject, allocatedCount: (AnyObject) -&gt; Int)<br>
&gt; -&gt; Value)<br>
&gt;   public func withUnsafeMutablePointerToValue&lt;R&gt;(@noescape body:<br>
&gt; (UnsafeMutablePointer&lt;Value&gt;) throws -&gt; R) rethrows -&gt; R<br>
&gt;   public func withUnsafeMutablePointerToElements&lt;R&gt;(@noescape body:<br>
&gt; (UnsafeMutablePointer&lt;Element&gt;) throws -&gt; R) rethrows -&gt; R<br>
&gt;   public func withUnsafeMutablePointers&lt;R&gt;(@noescape body: (_:<br>
&gt; UnsafeMutablePointer&lt;Value&gt;,_: UnsafeMutablePointer&lt;Element&gt;) throws<br>
&gt; -&gt; R) rethrows -&gt; R<br>
&gt; }<br>
&gt;<br>
&gt; A draft implementation is available at<br>
&gt; <a href="https://github.com/apple/swift/compare/master...lorentey:noescape" rel="noreferrer" target="_blank">https://github.com/apple/swift/compare/master...lorentey:noescape</a><br>
&gt; &lt;<a href="https://github.com/apple/swift/compare/master...lorentey:noescape" rel="noreferrer" target="_blank">https://github.com/apple/swift/compare/master...lorentey:noescape</a>&gt;<br>
&gt;<br>
&gt; Impact on Existing Code<br>
&gt;<br>
&gt; Luckily, all modified API is either marked final, or defined in a<br>
&gt; struct, so I expect no existing code is going to break due to these<br>
&gt; changes.<br>
<br>
--<br>
-Dave<br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>