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 <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> 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 <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br>
<br>
> I’d like to request feedback on the draft proposal below, about a (hopefully) trivial change to the standard library.<br>
><br>
> I considered adding it to SE-0012<br>
> <<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>>,<br>
> which is also about @noescape, but that proposal is about modifying<br>
> C/Obj-C API, so it’s not a good fit<br>
> <<a href="https://github.com/apple/swift-evolution/pull/122" rel="noreferrer" target="_blank">https://github.com/apple/swift-evolution/pull/122</a>>.<br>
><br>
> Introduction<br>
><br>
> stdlib’s ManagedBuffer family of APIs has some public initializers and<br>
> methods taking closures that are missing the @noescape attribute. The<br>
> same family has a set of withUnsafeMutablePointer* functions that do<br>
> not have a rethrows declaration, while all similar methods elsewhere<br>
> in the standard library allow closures to throw.<br>
><br>
> 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>
> Motivation<br>
><br>
> - ManagedBuffer seems designed for raw performance. Not having<br>
> @noescape on the only API that allows access to the buffer’s contents<br>
> defeats some compiler optimizations, such as omitting unneccessary<br>
> retain/release calls. This can negate the performance advantage of<br>
> ManagedBuffer over simpler solutions, like using an Array.<br>
> - Accepting throwing closures makes these APIs more versatile, and<br>
> also improves their consistency with other parts of stdlib.<br>
><br>
> Detailed Design<br>
><br>
> The following set of APIs would be affected by this proposal:<br>
><br>
> public class ManagedProtoBuffer<Value, Element> : NonObjectiveCBase {<br>
> public final func withUnsafeMutablePointerToValue<R>(body: (UnsafeMutablePointer<Value>) -> R) -> R<br>
> public final func withUnsafeMutablePointerToElements<R>(body: (UnsafeMutablePointer<Element>) -> R) -> R<br>
> public final func withUnsafeMutablePointers<R>(body: (_:<br>
> UnsafeMutablePointer<Value>, _: UnsafeMutablePointer<Element>) -> R)<br>
> -> R<br>
> }<br>
><br>
> public class ManagedBuffer<Value, Element> : ManagedProtoBuffer<Value, Element> {<br>
> public final class func create(minimumCapacity: Int, initialValue:<br>
> (ManagedProtoBuffer<Value,Element>) -> Value) -><br>
> ManagedBuffer<Value,Element><br>
> }<br>
><br>
> public struct ManagedBufferPointer<Value, Element> : Equatable {<br>
> public init(bufferClass: AnyClass, minimumCapacity: Int,<br>
> initialValue: (buffer: AnyObject, allocatedCount: (AnyObject) -> Int)<br>
> -> Value)<br>
> public func withUnsafeMutablePointerToValue<R>(body: (UnsafeMutablePointer<Value>) -> R) -> R<br>
> public func withUnsafeMutablePointerToElements<R>(body: (UnsafeMutablePointer<Element>) -> R) -> R<br>
> public func withUnsafeMutablePointers<R>(body: (_:<br>
> UnsafeMutablePointer<Value>, _: UnsafeMutablePointer<Element>) -> R)<br>
> -> R<br>
> }<br>
><br>
> Here is how they would look after the proposed changes:<br>
><br>
> public class ManagedProtoBuffer<Value, Element> : NonObjectiveCBase {<br>
> public final func withUnsafeMutablePointerToValue<R>(@noescape body:<br>
> (UnsafeMutablePointer<Value>) throws -> R) rethrows -> R<br>
> public final func withUnsafeMutablePointerToElements<R>(@noescape<br>
> body: (UnsafeMutablePointer<Element>) throws -> R) rethrows -> R<br>
> public final func withUnsafeMutablePointers<R>(@noescape body: (_:<br>
> UnsafeMutablePointer<Value>, _: UnsafeMutablePointer<Element>) throws<br>
> -> R) rethrows -> R<br>
><br>
> public class ManagedBuffer<Value, Element> : ManagedProtoBuffer<Value, Element> {<br>
> public final class func create(minimumCapacity: Int, @noescape<br>
> initialValue: (ManagedProtoBuffer<Value,Element>) -> Value) -><br>
> ManagedBuffer<Value,Element><br>
> }<br>
><br>
> public struct ManagedBufferPointer<Value, Element> : Equatable {<br>
> public init(bufferClass: AnyClass, minimumCapacity: Int, @noescape<br>
> initialValue: (buffer: AnyObject, allocatedCount: (AnyObject) -> Int)<br>
> -> Value)<br>
> public func withUnsafeMutablePointerToValue<R>(@noescape body:<br>
> (UnsafeMutablePointer<Value>) throws -> R) rethrows -> R<br>
> public func withUnsafeMutablePointerToElements<R>(@noescape body:<br>
> (UnsafeMutablePointer<Element>) throws -> R) rethrows -> R<br>
> public func withUnsafeMutablePointers<R>(@noescape body: (_:<br>
> UnsafeMutablePointer<Value>,_: UnsafeMutablePointer<Element>) throws<br>
> -> R) rethrows -> R<br>
> }<br>
><br>
> A draft implementation is available at<br>
> <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>
> <<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>
><br>
> Impact on Existing Code<br>
><br>
> Luckily, all modified API is either marked final, or defined in a<br>
> struct, so I expect no existing code is going to break due to these<br>
> 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>