<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 Dec 7, 2015, at 10:36 AM, Joe Groff via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class=""><br class="Apple-interchange-newline">On Dec 7, 2015, at 10:27 AM, Karl Pickett &lt;<a href="mailto:karl.pickett@gmail.com" class="">karl.pickett@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">I created&nbsp;<a href="https://bugs.swift.org/browse/SR-110" class="">https://bugs.swift.org/browse/SR-110</a><span class="Apple-converted-space">&nbsp;</span>for the compiler.&nbsp; However, I also think that the documentation needs an issue filed. &nbsp;(Where to do that at?)&nbsp;</div></div></blockquote><div class=""><br class=""></div><div class="">Thanks!</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class="">The current docs say structs are always copied (the only exception being inout to memory variable optimization).&nbsp; That would make programmers worried about speed and stack usage run away screaming, and not give swift a try.</div></div></div></blockquote><div class=""><br class=""></div><div class="">If weren't already been chased away by 'vars are always allocated on the heap'. The docs generally discuss high-level semantic behavior rather than the real code emitted; in general, users can count on structs being copied whenever necessary to preserve value semantics between different names. How would you suggest rewording the documentation?</div><div class=""><br class=""></div><div class="">-Joe</div></div></div></blockquote><div><br class=""></div>Perhaps the docs should instead talk about how the lifetime of the var's value might extend beyond the return from its scope, without explicitly saying anything about stack or heap allocation, which as you note is entirely implementation detail.</div><div><br class=""><blockquote type="cite" class=""><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""><blockquote type="cite" class=""><div class=""><div class="gmail_extra" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class="gmail_quote">On Mon, Dec 7, 2015 at 11:53 AM, Joe Groff<span class="Apple-converted-space">&nbsp;</span><span dir="ltr" class="">&lt;<a href="mailto:jgroff@apple.com" target="_blank" class="">jgroff@apple.com</a>&gt;</span><span class="Apple-converted-space">&nbsp;</span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div class="" style="word-wrap: break-word;"><br class=""><div class=""><blockquote type="cite" class=""><span class=""><div class="">On Dec 6, 2015, at 5:16 PM, Karl Pickett via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt; wrote:</div><br class=""></span><span class=""><div class=""><div dir="ltr" class=""><font face="monospace, monospace" class="">I have a struct and this code:</font><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><div class=""><font face="monospace, monospace" class="">func test() {</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>precondition(sizeof(Foo) == 128)</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>let s = Foo()</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>for _ in 0..&lt;100_000_000 {</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>doSomething(s)</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>}</font></div><div class=""><font face="monospace, monospace" class="">}</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">The asm (on LInux, with -O) is showing me that s is being re-initialized on every iteration of the loop.&nbsp; I was hoping that thanks to swift's strict constness rules on structs, it wouldn't have to do this - and just pass the same pointer to doSomething() each time.</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">When I use an inout param, that is 2x as fast and doesn't re-initialize each time.&nbsp; However I don't see why passing something immutably wouldn't be as fast.</font></div></div></div></div></span></blockquote><div class=""><br class=""></div>This definitely seems like a place where we ought to be able to peephole the extra copies away. Mind filing a bug?</div><div class=""><br class=""></div><div class="">-Joe</div><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div class=""><div class="h5"><div dir="ltr" class=""><div class=""><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">- Karl</font></div><font face="monospace, monospace" class=""><br class="">asm from perf:<br class=""><br class=""><div class="">&nbsp;<span class="Apple-converted-space">&nbsp;</span>2.71 │50:┌─→xorps &nbsp;%xmm0,%xmm0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;▒</div><div class="">&nbsp;<span class="Apple-converted-space">&nbsp;</span>8.06 │ &nbsp; │ &nbsp;movaps %xmm0,-0x20(%rbp) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;▒</div><div class="">&nbsp;<span class="Apple-converted-space">&nbsp;</span>2.71 │ &nbsp; │ &nbsp;movaps %xmm0,-0x30(%rbp) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;▒</div><div class="">&nbsp;<span class="Apple-converted-space">&nbsp;</span>7.41 │ &nbsp; │ &nbsp;movaps %xmm0,-0x40(%rbp) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;▒</div><div class="">&nbsp;10.59 │ &nbsp; │ &nbsp;movaps %xmm0,-0x50(%rbp) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;▒</div><div class="">&nbsp;10.00 │ &nbsp; │ &nbsp;movaps %xmm0,-0x60(%rbp) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;▒</div><div class="">&nbsp;<span class="Apple-converted-space">&nbsp;</span>9.53 │ &nbsp; │ &nbsp;movaps %xmm0,-0x70(%rbp) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;▒</div><div class="">&nbsp;10.65 │ &nbsp; │ &nbsp;movaps %xmm0,-0x80(%rbp) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;▒</div><div class="">&nbsp;11.24 │ &nbsp; │ &nbsp;movaps %xmm0,-0x90(%rbp) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;▒</div><div class="">&nbsp;12.06 │ &nbsp; │ &nbsp;mov &nbsp; &nbsp;%r14,%rdi &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;▒</div><div class="">&nbsp;<span class="Apple-converted-space">&nbsp;</span>3.41 │ &nbsp; │→ callq &nbsp;_TF4main11doSomethingFVS_3FooT_ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;▒</div><div class="">&nbsp;<span class="Apple-converted-space">&nbsp;</span>2.82 │ &nbsp; │ &nbsp;dec &nbsp; &nbsp;%rbx &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ▒</div><div class="">&nbsp;<span class="Apple-converted-space">&nbsp;</span>8.82 │ &nbsp; └──jne &nbsp; &nbsp;50 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div>main.swift:<br class=""><br class=""><div class="">struct Vec4 {</div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>var a: Int64 = 0</div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>var b: Int64 = 0</div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>var c: Int64 = 0</div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>var d: Int64 = 0</div><div class="">}</div><div class=""><br class=""></div><div class="">struct Foo {</div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>var x: Vec4 = Vec4()</div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>var y: Vec4 = Vec4()</div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>var z: Vec4 = Vec4()</div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>var u: Vec4 = Vec4()</div><div class="">}</div><div class=""><br class=""></div><div class="">func test() {</div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>precondition(sizeof(Foo) == 128)</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>let s = Foo()</div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>for _ in 0..&lt;100_000_000 {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>doSomething(s)</div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>}</div><div class="">}</div><div class=""><br class=""></div><div class="">test()</div><div class=""><br class=""></div></font><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">lib.swift:</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><div class=""><font face="monospace, monospace" class="">func doSomething(s: Foo) {</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>precondition(s.x.a != 1)</font></div><div class=""><font face="monospace, monospace" class="">}</font></div></div></div></div></div></div><img alt="" width="1" height="1" border="0" class="" style="min-height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;"><span class="Apple-converted-space">&nbsp;</span>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-users</a></div></blockquote></div></div></blockquote></div></div></div></blockquote></div><br class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=NLTid1W7V2mxBEfr5Y3KfTntaxSmOQp5vjACZc9Eh19Ag0RdZK9mYyQ3kccdEQryYFTaef1NkTlna9A8lMHbl6L-2BIXGitOFW48HQUh6jVvgEL-2BeEMZIbocTDnODZmlY94HjRtNFG7RhTUpMBcvViTsJAHNPQ7pzvztKQROmleBq7VzbEAOEa9-2Fx4Uk9JF9g-2FeXbq-2FA2uFVmuUNkbqL5by8XZHABDbIREkH76US4Pr0A-3D" alt="" width="1" height="1" border="0" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""><span class="Apple-converted-space">&nbsp;</span></span><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">swift-users mailing list</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="mailto:swift-users@swift.org" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">swift-users@swift.org</a><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-users</a></div></blockquote></div><br class=""></body></html>