<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 <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> 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=""> precondition(sizeof(Foo) == 128)</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class=""> let s = Foo()</font></div><div class=""><font face="monospace, monospace" class=""> for _ in 0..<100_000_000 {</font></div><div class=""><font face="monospace, monospace" class=""> doSomething(s)</font></div><div class=""><font face="monospace, monospace" class=""> }</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. 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. 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=""> 2.71 │50:┌─→xorps %xmm0,%xmm0 ▒</div><div class=""> 8.06 │ │ movaps %xmm0,-0x20(%rbp) ▒</div><div class=""> 2.71 │ │ movaps %xmm0,-0x30(%rbp) ▒</div><div class=""> 7.41 │ │ movaps %xmm0,-0x40(%rbp) ▒</div><div class=""> 10.59 │ │ movaps %xmm0,-0x50(%rbp) ▒</div><div class=""> 10.00 │ │ movaps %xmm0,-0x60(%rbp) ▒</div><div class=""> 9.53 │ │ movaps %xmm0,-0x70(%rbp) ▒</div><div class=""> 10.65 │ │ movaps %xmm0,-0x80(%rbp) ▒</div><div class=""> 11.24 │ │ movaps %xmm0,-0x90(%rbp) ▒</div><div class=""> 12.06 │ │ mov %r14,%rdi ▒</div><div class=""> 3.41 │ │→ callq _TF4main11doSomethingFVS_3FooT_ ▒</div><div class=""> 2.82 │ │ dec %rbx ▒</div><div class=""> 8.82 │ └──jne 50 </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=""> var a: Int64 = 0</div><div class=""> var b: Int64 = 0</div><div class=""> var c: Int64 = 0</div><div class=""> var d: Int64 = 0</div><div class="">}</div><div class=""><br class=""></div><div class="">struct Foo {</div><div class=""> var x: Vec4 = Vec4()</div><div class=""> var y: Vec4 = Vec4()</div><div class=""> var z: Vec4 = Vec4()</div><div class=""> var u: Vec4 = Vec4()</div><div class="">}</div><div class=""><br class=""></div><div class="">func test() {</div><div class=""> precondition(sizeof(Foo) == 128)</div><div class=""><br class=""></div><div class=""> let s = Foo()</div><div class=""> for _ in 0..<100_000_000 {</div><div class=""> doSomething(s)</div><div class=""> }</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=""> 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>