<div dir="ltr">I created <a href="https://bugs.swift.org/browse/SR-110">https://bugs.swift.org/browse/SR-110</a> for the compiler.  However, I also think that the documentation needs an issue filed.  (Where to do that at?) <div><br></div><div>The current docs say structs are always copied (the only exception being inout to memory variable optimization).  That would make programmers worried about speed and stack usage run away screaming, and not give swift a try.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Dec 7, 2015 at 11:53 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><blockquote type="cite"><span class=""><div>On Dec 6, 2015, at 5:16 PM, Karl Pickett via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br></span><span class=""><div><div dir="ltr"><font face="monospace, monospace">I have a struct and this code:</font><div><font face="monospace, monospace"><br></font></div><div><div><font face="monospace, monospace">func test() {</font></div><div><font face="monospace, monospace">    precondition(sizeof(Foo) == 128)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    let s = Foo()</font></div><div><font face="monospace, monospace">    for _ in 0..&lt;100_000_000 {</font></div><div><font face="monospace, monospace">        doSomething(s)</font></div><div><font face="monospace, monospace">    }</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">The asm (on LInux, with -O) is showing me that s is being re-initialized on every iteration of the loop.  I was hoping that thanks to swift&#39;s strict constness rules on structs, it wouldn&#39;t have to do this - and just pass the same pointer to doSomething() each time.</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">When I use an inout param, that is 2x as fast and doesn&#39;t re-initialize each time.  However I don&#39;t see why passing something immutably wouldn&#39;t be as fast.</font></div></div></div></div></span></blockquote><div><br></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><br></div><div>-Joe</div><div><br><blockquote type="cite"><div><div><div class="h5"><div dir="ltr"><div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">- Karl</font></div><font face="monospace, monospace"><br>asm from perf:<br><br><div>  2.71 │50:┌─→xorps  %xmm0,%xmm0                                                                                          ▒</div><div>  8.06 │   │  movaps %xmm0,-0x20(%rbp)                                                                                    ▒</div><div>  2.71 │   │  movaps %xmm0,-0x30(%rbp)                                                                                    ▒</div><div>  7.41 │   │  movaps %xmm0,-0x40(%rbp)                                                                                    ▒</div><div> 10.59 │   │  movaps %xmm0,-0x50(%rbp)                                                                                    ▒</div><div> 10.00 │   │  movaps %xmm0,-0x60(%rbp)                                                                                    ▒</div><div>  9.53 │   │  movaps %xmm0,-0x70(%rbp)                                                                                    ▒</div><div> 10.65 │   │  movaps %xmm0,-0x80(%rbp)                                                                                    ▒</div><div> 11.24 │   │  movaps %xmm0,-0x90(%rbp)                                                                                    ▒</div><div> 12.06 │   │  mov    %r14,%rdi                                                                                            ▒</div><div>  3.41 │   │→ callq  _TF4main11doSomethingFVS_3FooT_                                                                      ▒</div><div>  2.82 │   │  dec    %rbx                                                                                                 ▒</div><div>  8.82 │   └──jne    50             </div><div><font face="monospace, monospace"><br></font></div>main.swift:<br><br><div>struct Vec4 {</div><div>    var a: Int64 = 0</div><div>    var b: Int64 = 0</div><div>    var c: Int64 = 0</div><div>    var d: Int64 = 0</div><div>}</div><div><br></div><div>struct Foo {</div><div>    var x: Vec4 = Vec4()</div><div>    var y: Vec4 = Vec4()</div><div>    var z: Vec4 = Vec4()</div><div>    var u: Vec4 = Vec4()</div><div>}</div><div><br></div><div>func test() {</div><div>    precondition(sizeof(Foo) == 128)</div><div><br></div><div>    let s = Foo()</div><div>    for _ in 0..&lt;100_000_000 {</div><div>        doSomething(s)</div><div>    }</div><div>}</div><div><br></div><div>test()</div><div><br></div></font><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">lib.swift:</font></div><div><font face="monospace, monospace"><br></font></div><div><div><font face="monospace, monospace">func doSomething(s: Foo) {</font></div><div><font face="monospace, monospace">    precondition(s.x.a != 1)</font></div><div><font face="monospace, monospace">}</font></div></div></div></div>
</div></div><img alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
_______________________________________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></blockquote></div><br></div>