<div dir="ltr">not a fan of this. A lot of data structures including buffer pointers already store their capacity in a property, storing an allocation token would be redundant and waste space. It also forces <span style="font-family:monospace,monospace">allocate(capacity:)</span> to return a tuple which is a big turn off for me.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Sep 7, 2017 at 10:56 AM, Joe Groff <span dir="ltr">&lt;<a href="mailto:jgroff@apple.com" target="_blank">jgroff@apple.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><span class=""><blockquote type="cite"><div>On Sep 7, 2017, at 8:06 AM, Taylor Swift &lt;<a href="mailto:kelvin13ma@gmail.com" target="_blank">kelvin13ma@gmail.com</a>&gt; wrote:</div><br class="m_-7393461898039856691Apple-interchange-newline"><div><div dir="ltr">I don’t see any source for this claim in the <a href="https://developer.apple.com/documentation/swift/unsafemutablepointer/2295090-deallocate" target="_blank">documentation</a>, or the <a href="https://github.com/apple/swift/blob/master/stdlib/public/core/UnsafePointer.swift.gyb#L432" target="_blank">source code</a>. As far as I can tell the expected behavior is that partial deallocation “should” work.</div></div></blockquote><div><br></div></span><div>We should fix the documentation bug then.</div><div><br></div><div>Another way we could make these APIs easier to use correctly is to leverage the type system. allocate could return the allocation size wrapped up in an opaque AllocationToken type, and deallocate could take an AllocationToken:</div><div><br></div><div>static func allocate(capacity: Int) -&gt; (UnsafeMutableBufferPointer&lt;T&gt;<wbr>, AllocationToken)</div><div><br></div><div>func deallocate(token: AllocationToken)</div><div><br></div><div>That would make it harder for user code to pass an arbitrary size in, and make the relationship between the allocate and deallocate calls more explicit in their signatures. If we get an ABI break and a bold new idea for an allocator design with different context needs in the future, it&#39;d also insulate source code from the exact contents of the information that needs to be carried from allocation to deallocation.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>-Joe</div></font></span><div><div class="h5"><br><blockquote type="cite"><div><div class="gmail_extra"><div class="gmail_quote">On Thu, Sep 7, 2017 at 8:59 AM, Joe Groff <span dir="ltr">&lt;<a href="mailto:jgroff@apple.com" target="_blank">jgroff@apple.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="m_-7393461898039856691HOEnZb"><div class="m_-7393461898039856691h5"><br>
&gt; On Sep 6, 2017, at 4:46 PM, Taylor Swift &lt;<a href="mailto:kelvin13ma@gmail.com" target="_blank">kelvin13ma@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;&gt; On Sep 6, 2017, at 5:12 PM, Joe Groff &lt;<a href="mailto:jgroff@apple.com" target="_blank">jgroff@apple.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;&gt; On Sep 6, 2017, at 3:07 PM, Taylor Swift &lt;<a href="mailto:kelvin13ma@gmail.com" target="_blank">kelvin13ma@gmail.com</a>&gt; wrote:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; On Sep 6, 2017, at 4:31 PM, Joe Groff &lt;<a href="mailto:jgroff@apple.com" target="_blank">jgroff@apple.com</a>&gt; wrote:<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; On Sep 6, 2017, at 2:28 PM, Andrew Trick &lt;<a href="mailto:atrick@apple.com" target="_blank">atrick@apple.com</a>&gt; wrote:<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; On Sep 6, 2017, at 1:12 PM, Joe Groff via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; On Sep 6, 2017, at 1:06 PM, Taylor Swift &lt;<a href="mailto:kelvin13ma@gmail.com" target="_blank">kelvin13ma@gmail.com</a>&gt; wrote:<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; On Wed, Sep 6, 2017 at 12:41 PM, Joe Groff via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Currently, memory is deallocated by an instance method on UnsafeMutablePointer, deallocate(count:). Like much of the Swift pointer API, performing this operation on a buffer pointer requires extracting baseAddress! and count. It is very common for the allocation code above to be immediately followed by:<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; defer<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; {<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; buffer.<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; baseAddress?.deallocate(capaci<wbr>ty: buffer.count<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; )<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; }<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; This method is extremely problematic because nearly all users, on first seeing the signature of deallocate(capacity:), will naturally conclude from the capacity label that deallocate(capacity:) is equivalent to some kind of realloc()that can only shrink the buffer. However this is not the actual behavior — deallocate(capacity:) actually ignores the capacity argument and just calls free() on self. The current API is not only awkward and suboptimal, it is misleading. You can write perfectly legal Swift code that shouldn’t segfault, but still can, for example<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; var ptr = UnsafeMutablePointer&lt;UInt8&gt;.al<wbr>locate(capacity: 1000000<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; )<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; ptr.<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; initialize(to: 13, count: 1000000<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; )<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; ptr.<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; deallocate(capacity: 500000) // deallocate the second half of the memory block<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; ptr[0] // segmentation fault<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; where the first 500000 addresses should still be valid if the documentation is to be read literally.<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; The fact that the Swift runtime currently uses malloc/free is an implementation detail. Tracking deallocation size is a very useful optimization for better allocator backends, and C++ underwent an ABI break to make it so that sized delete can be supported. Maybe we can change the name to `deallocate(allocatedCapacity:<wbr>)` to make it clear that it isn&#39;t resizing the memory, and/or make the capacity argument optional so that you can pay for the overhead of the allocator deriving the size if it&#39;s inconvenient for the calling code to carry the size around, but we shouldn&#39;t remove the functionality altogether.<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; -Joe<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; ______________________________<wbr>_________________<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; swift-evolution mailing list<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; <a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-evolution</a><br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; The idea is to get the house in order by removing all parameters from deallocate(), since that’s what it really does right now. Then, in the future, if Swift gets a more sophisticated allocator backend, a new method like deallocate(capacity:) or reallocate(toCapacity:) could be added without conflicting with the currently named deallocate(capacity:). However, using the function signature to pretend that it does something it can’t actually do right now is extremely dangerous.<br>
&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt; I don&#39;t think that&#39;s a good idea in this case, because it&#39;s not unlikely we would explore an optimized allocator soon after ABI stability, and retrofitting these interfaces in a future version of Swift would put a deployment target limit on when they can be used, and mean that a lot of user code would need to be retrofitted to carry allocated capacities where needed to see any benefit.<br>
&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt; -Joe<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; The fact that we’re using malloc and free is already part of the ABI because old libraries need to be able to deallocate memory allocated by newer libraries.<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; The compiler doesn&#39;t ever generate calls directly to malloc and free, and the runtime entry points we do use already take size and alignment on both allocation and deallocation.<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; Within the standard library we could make use of some new deallocation fast path in the future without worrying about backward deployment.<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; Outside of the standard library, clients will get the benefits of whatever allocator is available on their deployed platform because we now encourage them to use UnsafeBufferPointer.deallocate<wbr>(). We can change the implementation inside UnsafeBufferPointer all we want, as long as it’s still malloc-compatible.<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; I’m sure we’ll want to provide a better allocation/deallocation API in the future for systems programmers based on move-only types. That will already be deployment-limited.<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; Absolute worst case, we introduce a sized UnsafePointer.deallocate in the future. Any new code outside the stdlib that wants the performance advantage would either need to<br>
&gt;&gt;&gt;&gt;&gt; - trivially wrap deallocation using UnsafeBufferPointer<br>
&gt;&gt;&gt;&gt;&gt; - create a trivial UnsafePointer.deallocate thunk under an availability flag<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; Since we already have sized deallocate, why would we take it away? If the name is misleading, we can change the name.<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; -Joe<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Exactly, we are changing the name to `deallocate()`. As for the old `deallocate(capacity:)` method that *needs* to be removed because it is actively harmful. As I’ve explained it’s not enough to just drop in a sized backend later as an “implementation detail” because it’s not an implementation detail, you’re changing the fundamental behavior of that method.<br>
&gt;&gt;<br>
&gt;&gt; It would remove a mode of &quot;holding it wrong&quot;, but the specified behavior will not change; it has and will always fully deallocate the object being referenced in the cases where the call has defined behavior.<br>
&gt;<br>
&gt; idk what you mean by it holding it wrong, but  while the specified behavior won’t change the actual behavior *will* change, because right now the two don’t agree. We can’t just treat it as a bug to fix because it’s been around since the beginning of Swift and some people treat it as part of expected behavior, using the function like `free()`. This isn’t using it incorrectly, in fact in terms of behavior, passing a meaningful capacity argument to `deallocate(capacity:)` is *incorrect usage*, as demonstrated in the segfaulting example in the proposal.<br>
<br>
</div></div>The segfaulting example is an incorrect usage. The only valid parameters to deallocate(capacity:) are the base address of an allocation, and the original capacity passed into allocate(); it has never been intended to support partial deallocation of allocated blocks. It seems to me like this proposal is based on a misunderstanding of how the API works. The documentation and/or name should be clarified.<br>
<span class="m_-7393461898039856691HOEnZb"><font color="#888888"><br>
-Joe<br>
</font></span><div class="m_-7393461898039856691HOEnZb"><div class="m_-7393461898039856691h5"><br>
&gt; “fixing” this bug will cause programs that once operated on previously valid assumptions of “free()” semantics to behave differently, without any warnings ever being generated. Conversely incorrect code will suddenly become “correct” though this is less of a problem.<br>
&gt;<br>
&gt;&gt; A sized implementation may fail more obviously when you violate the contract in the future. Not having sized deallocation is a known deficiency of the C model we&#39;ve been fairly diligent about avoiding in Swift&#39;s allocation interfaces, and it would be extremely unfortunate for us to backpedal from it.<br>
&gt;&gt;<br>
&gt;&gt; -Joe<br>
&gt;<br>
&gt; Which is worse, an active gaping hole in Swift’s memory system, or potentially discouraging users from using a hypothetical future allocation API?<br>
&gt;<br>
&gt; Making sure the existing allocation API is working properly is a prerequisite to introducing a future more advanced allocation API.<br>
<br>
</div></div></blockquote></div><br></div>
</div></blockquote></div></div></div><br></div></blockquote></div><br></div>