<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:27 AM, Karl Pickett <<a href="mailto:karl.pickett@gmail.com" class="">karl.pickett@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" 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="">I created <a href="https://bugs.swift.org/browse/SR-110" class="">https://bugs.swift.org/browse/SR-110</a><span class="Apple-converted-space"> </span>for the compiler. However, I also think that the documentation needs an issue filed. (Where to do that at?) </div></div></blockquote><div><br class=""></div><div>Thanks!</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" 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=""><div class="">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></blockquote><div><br class=""></div><div>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><br class=""></div><div>-Joe</div><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"> </span><span dir="ltr" class=""><<a href="mailto:jgroff@apple.com" target="_blank" class="">jgroff@apple.com</a>></span><span class="Apple-converted-space"> </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 style="word-wrap: break-word;" class=""><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 <<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>> 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=""> <span class="Apple-converted-space"> </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=""> <span class="Apple-converted-space"> </span>let s = Foo()</font></div><div class=""><font face="monospace, monospace" class=""> <span class="Apple-converted-space"> </span>for _ in 0..<100_000_000 {</font></div><div class=""><font face="monospace, monospace" class=""> <span class="Apple-converted-space"> </span>doSomething(s)</font></div><div class=""><font face="monospace, monospace" class=""> <span class="Apple-converted-space"> </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. 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></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=""> <span class="Apple-converted-space"> </span>2.71 │50:┌─→xorps %xmm0,%xmm0 ▒</div><div class=""> <span class="Apple-converted-space"> </span>8.06 │ │ movaps %xmm0,-0x20(%rbp) ▒</div><div class=""> <span class="Apple-converted-space"> </span>2.71 │ │ movaps %xmm0,-0x30(%rbp) ▒</div><div class=""> <span class="Apple-converted-space"> </span>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=""> <span class="Apple-converted-space"> </span>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=""> <span class="Apple-converted-space"> </span>3.41 │ │→ callq _TF4main11doSomethingFVS_3FooT_ ▒</div><div class=""> <span class="Apple-converted-space"> </span>2.82 │ │ dec %rbx ▒</div><div class=""> <span class="Apple-converted-space"> </span>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=""> <span class="Apple-converted-space"> </span>var a: Int64 = 0</div><div class=""> <span class="Apple-converted-space"> </span>var b: Int64 = 0</div><div class=""> <span class="Apple-converted-space"> </span>var c: Int64 = 0</div><div class=""> <span class="Apple-converted-space"> </span>var d: Int64 = 0</div><div class="">}</div><div class=""><br class=""></div><div class="">struct Foo {</div><div class=""> <span class="Apple-converted-space"> </span>var x: Vec4 = Vec4()</div><div class=""> <span class="Apple-converted-space"> </span>var y: Vec4 = Vec4()</div><div class=""> <span class="Apple-converted-space"> </span>var z: Vec4 = Vec4()</div><div class=""> <span class="Apple-converted-space"> </span>var u: Vec4 = Vec4()</div><div class="">}</div><div class=""><br class=""></div><div class="">func test() {</div><div class=""> <span class="Apple-converted-space"> </span>precondition(sizeof(Foo) == 128)</div><div class=""><br class=""></div><div class=""> <span class="Apple-converted-space"> </span>let s = Foo()</div><div class=""> <span class="Apple-converted-space"> </span>for _ in 0..<100_000_000 {</div><div class=""> <span class="Apple-converted-space"> </span>doSomething(s)</div><div class=""> <span class="Apple-converted-space"> </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=""> <span class="Apple-converted-space"> </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" style="min-height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""><span class="Apple-converted-space"> </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=""></body></html>