<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 6, 2015, at 5:16 PM, Karl Pickett 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 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; 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; let s = Foo()</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; for _ in 0..&lt;100_000_000 {</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp; doSomething(s)</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; }</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></blockquote><div><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><br class=""></div><div>-Joe</div><div><br class=""><blockquote type="cite" class=""><div class=""><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; 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; 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; 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; 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; 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; 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; 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; 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; var a: Int64 = 0</div><div class="">&nbsp; &nbsp; var b: Int64 = 0</div><div class="">&nbsp; &nbsp; var c: Int64 = 0</div><div class="">&nbsp; &nbsp; var d: Int64 = 0</div><div class="">}</div><div class=""><br class=""></div><div class="">struct Foo {</div><div class="">&nbsp; &nbsp; var x: Vec4 = Vec4()</div><div class="">&nbsp; &nbsp; var y: Vec4 = Vec4()</div><div class="">&nbsp; &nbsp; var z: Vec4 = Vec4()</div><div class="">&nbsp; &nbsp; var u: Vec4 = Vec4()</div><div class="">}</div><div class=""><br class=""></div><div class="">func test() {</div><div class="">&nbsp; &nbsp; precondition(sizeof(Foo) == 128)</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; let s = Foo()</div><div class="">&nbsp; &nbsp; for _ in 0..&lt;100_000_000 {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; doSomething(s)</div><div class="">&nbsp; &nbsp; }</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; precondition(s.x.a != 1)</font></div><div class=""><font face="monospace, monospace" class="">}</font></div></div></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=RoDF4MveSEMYBIqIJA6ub1g8cOZ-2BVYvqV-2FqygPhjPn-2B203gvsbXwt1Pz4VPve8NdfpW9WourZS1ZHrR-2BROoH3TpUb1uIwsv7VU0KVuC8lpjRAqYmJKo-2BVWFQ-2Fq9Ztr9mqHh3VZJAu4We6DbFE-2Br-2Fnx4h24gI9r3Nj8kl0oecofyNg8s-2FXp29QpQgjsMo-2B2UMsZjFnbhADVRhQJtsqS0aiw-3D-3D" alt="" width="1" height="1" border="0" style="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;" class="">
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>