<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 18 Apr 2017, at 00:29, Karl Wagner &lt;<a href="mailto:karl.swift@springsup.com" class="">karl.swift@springsup.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 17 Apr 2017, at 23:18, Michael Ilseman via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">This is the best approach that I’m aware of. It does bake in an ABI assumption that the tuple layout will be contiguous and strided/addressable. Monitor <a href="https://bugs.swift.org/browse/SR-3726" class="">https://bugs.swift.org/browse/SR-3726</a> for changes here. Note that you can also a little more “pure” in a sense if you construct an UnsafeBufferPointer from your UnsafeRawPointer and operate on that. Currently, the compiler does not always elide the copy in the getter, see&nbsp;<a href="https://bugs.swift.org/browse/SR-4581" class="">https://bugs.swift.org/browse/SR-4581</a></div><div class=""><br class=""></div></div></div></blockquote></div><br class=""><div class="">Will the compiler ever optimise an UBP allocation on to the stack? For example, I’ve written plenty of code such as:</div><div class=""><br class=""></div><div class="">let ptr = UnsafeRawBufferPointer.allocate(count: 128)</div><div class="">defer { ptr.deallocate(count: 128) }</div><div class=""><br class=""></div><div class="">...when interfacing with C APIs. Which is basically the definition of a stack allocation. That would perhaps be the “swiftier” way to do it; to let the compiler determine how to allocate the memory based on what it knows about its expected lifetime.</div></div></div></blockquote></div><div class=""><br class=""></div><div class="">I’ll answer that: no, it won’t.</div><br class=""><div class="">echo&nbsp;"let ptr = UnsafeMutableRawPointer.allocate(bytes: 128, alignedTo: 1);&nbsp;defer { ptr.deallocate(bytes: 128, alignedTo: 1) }"&nbsp;|&nbsp;swiftc -O -emit-assembly -</div><div class=""><br class=""><font face="Courier" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>.section<span class="Apple-tab-span" style="white-space:pre">        </span>__TEXT,__text,regular,pure_instructions<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>.macosx_version_min 10, 9<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>.globl<span class="Apple-tab-span" style="white-space:pre">        </span>_main<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>.p2align<span class="Apple-tab-span" style="white-space:pre">        </span>4, 0x90<br class="">_main:<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>pushq<span class="Apple-tab-span" style="white-space:pre">        </span>%rbp<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>movq<span class="Apple-tab-span" style="white-space:pre">        </span>%rsp, %rbp<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>movl<span class="Apple-tab-span" style="white-space:pre">        </span>$128, %edi<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>xorl<span class="Apple-tab-span" style="white-space:pre">        </span>%esi, %esi<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>callq<span class="Apple-tab-span" style="white-space:pre">        </span>_swift_rt_swift_slowAlloc &nbsp; ; a.k.a.&nbsp;‘malloc'<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>movq<span class="Apple-tab-span" style="white-space:pre">        </span>%rax, __Tv4main3ptrSv(%rip)<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>movl<span class="Apple-tab-span" style="white-space:pre">        </span>$128, %esi<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>xorl<span class="Apple-tab-span" style="white-space:pre">        </span>%edx, %edx<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>movq<span class="Apple-tab-span" style="white-space:pre">        </span>%rax, %rdi<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>callq<span class="Apple-tab-span" style="white-space:pre">        </span>_swift_rt_swift_slowDealloc ; a.k.a&nbsp;‘free'<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>xorl<span class="Apple-tab-span" style="white-space:pre">        </span>%eax, %eax<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>popq<span class="Apple-tab-span" style="white-space:pre">        </span>%rbp<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>retq</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class="">Would be cool to do one day, though.</div><div class=""><br class=""></div><div class="">- Karl</div></body></html>