<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Aug 9, 2017 at 2:34 AM, Andrew Trick <span dir="ltr">&lt;<a href="mailto:atrick@apple.com" target="_blank">atrick@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 Aug 8, 2017, at 11:10 PM, Taylor Swift &lt;<a href="mailto:kelvin13ma@gmail.com" target="_blank">kelvin13ma@gmail.com</a>&gt; wrote:</div><br class="m_-4859785049460883042Apple-interchange-newline"><div><div dir="ltr"><br><div class="gmail_extra">On Wed, Aug 9, 2017 at 1:51 AM, Andrew Trick <span dir="ltr">&lt;<a href="mailto:atrick@apple.com" target="_blank">atrick@apple.com</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><br><div><span class="m_-4859785049460883042gmail-"><blockquote type="cite"><div>On Aug 8, 2017, at 8:44 PM, Taylor Swift &lt;<a href="mailto:kelvin13ma@gmail.com" target="_blank">kelvin13ma@gmail.com</a>&gt; wrote:</div><br class="m_-4859785049460883042gmail-m_2532167022362721963Apple-interchange-newline"><div><div dir="ltr">cool,, as for <span style="font-family:monospace,monospace">UnsafeMutableRawBufferPointer.<wbr>copy(from:bytes:)</span>, I cannot find such a function anywhere in the API. There is <a href="https://developer.apple.com/documentation/swift/unsafemutablerawbufferpointer/2635415-copybytes" target="_blank"><span style="font-family:monospace,monospace">copyBytes(from:)</span></a>, but the documentation is messed up and mentions a nonexistent <span style="font-family:monospace,monospace">count:</span> argument over and over again. The documentation also doesn’t mention what happens if there is a length mismatch, so users are effectively relying on an implementation detail. I don’t know how to best resolve this.<br></div></div></blockquote><div><br></div></span><div>We currently have `UnsafeMutableRawBufferPointer<wbr>.copyBytes(from:)`. I don’t think your proposal changes that. The current docs refer to the `source` parameter, which is correct. Docs refer to the parameter name, not the label name. So `source.count` is the size of the input. I was pointing out that it has the semantics: `debugAssert(source.count &lt;= self.count)`.</div><div><br></div><div>Your proposal changes `UnsafeRawPointer.copyBytes(fr<wbr>om:count:)` to `UnsafeRawPointer.copy(from:by<wbr>tes:)`. Originally we wanted to those API names to match, but I’m fine with your change. What is more important is that the semantics are the same as `copyBytes(from:)`. Furthermore, any new methods that you add that copy into a raw buffer (e.g. initializeMemory(as:from:count<wbr>:)) should have similar behavior.</div><div><br></div></div></div></blockquote><div> </div><div>I’m fine with switching to taking the <span style="font-family:monospace,monospace">count</span> from the source, though I think taking the <span style="font-family:monospace,monospace">count</span>
 from the destination is slightly better because 1) the use cases I 
mentioned in the other email, and 2) all the other memorystate functions
 use <span style="font-family:monospace,monospace">self.count</span> instead of <span style="font-family:monospace,monospace">source.count</span>, if they take a <span style="font-family:monospace,monospace">source</span> argument. But being consistent with the raw pointer version is more important.</div></div></div></div></div></blockquote><div><br></div></span>If it’s copying from a buffer it should not take a count, if it’s copying from a pointer it obviously needs to take a count. What I mean by the two versions being named consistently is simply that they’re both named `copyBytes`. That really isn’t important though. The overflow/underflow semantics being consistent are important.</div><div><br></div><div>(Incidentally, the reason “bytes” needs to be in the somewhere name is because this method isn’t capable of copying nontrivial values)</div><div><span class=""><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Should the methods that don’t deal with raw buffers also be modified to use the source argument (i.e. UnsafeMutableBufferPointer.<wbr>initialize(from:))?<br></div></div></div></div></div></blockquote><div><br></div></span><div>I’m not sure what you mean by this. It also allows the destination to be larger than the source. Initializing from a sequence does not trap on overflow because we can’t guarantee the size of the sequence ahead of time. When I talk about consistent overflow/underflow semantics, I’m only talking about initializing one unsafe buffer/pointer from another unsafe buffer/pointer.</div><span class=""><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Also, was there a reason why <span style="font-family:monospace,monospace">UnsafeMutableRawBufferPointer.<wbr>copyBytes(from:)</span> uses the source’s count instead of its own? Right now this behavior is “technically” undocumented behavior (as the public docs haven’t been updated) so if there was ever a time to change it, now would be it.<br></div></div></div></div></div></blockquote><div><br></div></span><div>Mainly because partial initialization is more expected than dropping data on the floor. Ultimately, this should be whatever typical developers would expect the behavior to be. I would be very hesitant to change the behavior now though.</div><div><br></div><div>-Andy</div></div></div></blockquote><div><br></div><div>The problem is I would expect to be able to safely call deinitialize() and friends after calling initialize(from:). If Element is a class type and initialize doesn’t fill the entire buffer range, calling deinitialize() will crash. That being said, since copy(from:bytes:) and copyBytes(from:) don’t do any initialization and have no direct counterparts in UnsafeMutableBufferPointer, it’s okay if they have different behavior than the other methods.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><div class="h5"><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div><div></div><div>—</div><div><br></div><div>Another thing. The initialization methods that you’re adding to `UnsafeRawPointer` and `UnsafeRawBufferPointer` should return typed `UnsafePointer&lt;Element&gt;` and `UnsafeBufferPointer&lt;Element&gt;` respectively.</div></div></div></blockquote><div><br></div><div>I’ll fix that once the current <a href="https://github.com/apple/swift-evolution/pull/741" target="_blank">pending edit</a> gets merged.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div><div><br></div><div>Thanks,</div><div><br></div><div>-Andy</div><span class="m_-4859785049460883042gmail-"><br><blockquote type="cite"><div><div class="gmail_extra"><div class="gmail_quote">On Tue, Aug 8, 2017 at 11:33 PM, Andrew Trick <span dir="ltr">&lt;<a href="mailto:atrick@apple.com" target="_blank">atrick@apple.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div><div class="m_-4859785049460883042gmail-m_2532167022362721963h5"><br><div><blockquote type="cite"><div>On Aug 8, 2017, at 8:29 PM, Taylor Swift &lt;<a href="mailto:kelvin13ma@gmail.com" target="_blank">kelvin13ma@gmail.com</a>&gt; wrote:</div><br class="m_-4859785049460883042gmail-m_2532167022362721963m_-6986739811646066256Apple-interchange-newline"><div><div class="gmail_extra" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br class="m_-4859785049460883042gmail-m_2532167022362721963m_-6986739811646066256Apple-interchange-newline"><br><div class="gmail_quote">On Tue, Aug 8, 2017 at 11:24 PM, Andrew Trick<span class="m_-4859785049460883042gmail-m_2532167022362721963m_-6986739811646066256Apple-converted-space"> </span><span dir="ltr">&lt;<a href="mailto:atrick@apple.com" target="_blank">atrick@apple.com</a>&gt;</span><span class="m_-4859785049460883042gmail-m_2532167022362721963m_-6986739811646066256Apple-converted-space"> </span>wrote<wbr>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div><div class="m_-4859785049460883042gmail-m_2532167022362721963m_-6986739811646066256h5"><br><div><blockquote type="cite"><div>On Aug 8, 2017, at 6:51 PM, Taylor Swift &lt;<a href="mailto:kelvin13ma@gmail.com" target="_blank">kelvin13ma@gmail.com</a>&gt; wrote:</div><br class="m_-4859785049460883042gmail-m_2532167022362721963m_-6986739811646066256m_-67376284151341790Apple-interchange-newline"><div><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Aug 8, 2017 at 9:38 PM, Andrew Trick<span class="m_-4859785049460883042gmail-m_2532167022362721963m_-6986739811646066256Apple-converted-space"> </span><span dir="ltr">&lt;<a href="mailto:atrick@apple.com" target="_blank">atrick@apple.com</a>&gt;</span><span class="m_-4859785049460883042gmail-m_2532167022362721963m_-6986739811646066256Apple-converted-space"> </span>wrote<wbr>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div><span><div><br></div><blockquote type="cite"><div class="gmail_quote" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div><div>&gt; UnsafeMutableRawBufferPointer.<wbr>allocate(bytes:alignedTo:)</div><div><br></div><div>Well, I think it&#39;s somewhat ridiculous for users to write this every time they allocate a buffer:</div><div><br></div><div>`UnsafeMutableRawBufferPointer<wbr>.allocate(bytes: size, alignedTo: MemoryLayout&lt;UInt&gt;.alignment)`</div><div><br></div><div>If anyone reading the code is unsure about the Swift API&#39;s alignment</div><div>guarantee, it&#39;s trivial to check the API docs.</div><div><br></div><div>You could introduce a clearly documented default `alignedTo`</div><div>argument. The reason I didn&#39;t do that is that the runtime won&#39;t</div><div>respect it anyway. But I think it would be fair to go ahead with the</div><div>API and file a bug against the runtime.</div></div></div></blockquote><div><br></div><div>Default argument of<span class="m_-4859785049460883042gmail-m_2532167022362721963m_-6986739811646066256m_-67376284151341790m_7821494737473580892Apple-converted-space"> </span><span style="font-family:monospace,monospace">MemoryLayout&lt;Int&gt;.alignment</span><span class="m_-4859785049460883042gmail-m_2532167022362721963m_-6986739811646066256m_-67376284151341790m_7821494737473580892Apple-converted-space"><wbr> </span>is the way to go but as you said i don’t know if that is actually allowed/works. An alternative is to have two allocate methods each, one that takes an alignment argument and one that doesn’t (and aligns to pointer alignment) but that feels inelegant. Default arguments would be better.<br></div></div></blockquote><div><br></div></span><div>Default argument makes sense to me too. Then the raw buffer pointer and regular raw pointer APIs can be consistent with each other.</div><div><br></div><div>Runtime bug:<span class="m_-4859785049460883042gmail-m_2532167022362721963m_-6986739811646066256Apple-converted-space"> </span><a href="https://bugs.swift.org/browse/SR-5664" target="_blank">https://bugs.swift.org/br<wbr>owse/SR-5664</a></div><span></span><br></div></div></blockquote><div><br></div><div>yikes i was not aware of this. I don’t think it’s bad enough to warrant dropping the argument like with<span class="m_-4859785049460883042gmail-m_2532167022362721963m_-6986739811646066256Apple-converted-space"> </span><span style="font-family:monospace,monospace">deallocate(capacity:)</span><span class="m_-4859785049460883042gmail-m_2532167022362721963m_-6986739811646066256Apple-converted-space"> </span>but I can imagine bad things happening to code that crams extra inhabitants into pointers.<br></div></div></div></div></div></blockquote></div><br></div></div><div>If we ever need to do pointer adjustment during deallocation to accommodate alignment, then I think the Swift runtime can track that. I see no reason to muddy the UnsafeRawPointer API with it. So, I agree with your proposed change to drop `alignedTo` there.</div><div><br></div><div>-Andy</div></div></blockquote></div><br></div><div class="gmail_extra" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">oh lol I was talking about assuming the pointer returned by<span class="m_-4859785049460883042gmail-m_2532167022362721963m_-6986739811646066256Apple-converted-space"> </span><span style="font-family:monospace,monospace">allocate(bytes:alignedTo:)</span><span class="m_-4859785049460883042gmail-m_2532167022362721963m_-6986739811646066256Apple-converted-space"> </span><wbr>is a multiple of<span class="m_-4859785049460883042gmail-m_2532167022362721963m_-6986739811646066256Apple-converted-space"> </span><span style="font-family:monospace,monospace">alignedTo</span>. Some code might be relying on the last few bits of the pointer being zero; i.e. sticking bit flags there like how some implementations store the red/black color information in a red-black tree node.</div></div></blockquote><br></div></div></div><div>Oh, sure. But I think it will be easy to fix the runtime. We could probably do it before the proposal is accepted if necessary.</div><div>-Andy</div><br></div></blockquote></div><br></div>
</div></blockquote></span></div><br></div></blockquote></div><br></div></div>
</div></blockquote></div></div></div><br></div></blockquote></div><br></div></div>