<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Dec 7, 2015 at 12:36 PM, 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><span class=""><blockquote type="cite"><div>On Dec 7, 2015, at 10:27 AM, Karl Pickett &lt;<a href="mailto:karl.pickett@gmail.com" target="_blank">karl.pickett@gmail.com</a>&gt; wrote:</div><br><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">I created <a href="https://bugs.swift.org/browse/SR-110" target="_blank">https://bugs.swift.org/browse/SR-110</a><span> </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></div></span><div>Thanks!</div><span class=""><br><blockquote type="cite"><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><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></blockquote><div><br></div></span><div>If weren&#39;t already been chased away by &#39;vars are always allocated on the heap&#39;. 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></div></blockquote><div><br></div><div>I&#39;m find with the docs being broad and semantic but a &#39;NOTE: Copying might be elided; see &quot;Swift Optimizations&quot; for details&#39; would be helpful.  Clearly the semantics are being carefully designed to enable certain optimizations, but if we&#39;re only seeing the semantics, we&#39;re not seeing the whole picture.</div><div><br></div><div>I did a search for &quot;heap&quot; and found nothing in the 2.2 ebook.  I recall seeing heap-&gt;stack optimizations for closures in some video and didn&#39;t go digging further because it seemed like the language designer(s) had a solution.  I think that&#39;s a separate issue but yes having details would be great.</div><div><br></div><div>Consider a user who wants to write some C-style swift (using stack and heap) and wants to know &quot;what am I being forced to pay for in Swift, that C doesn&#39;t have&quot;.  The docs often contrast with C and I think that&#39;s great.</div><div><br></div><div>So the &quot;Swift Optimizations&quot; would cover heap/stack, struct copying for func calls (pure structs, and cases where they have embedded classes), and inouts to function or global vars.  And returning a struct (does caller or callee allocate, and/or copy).   And ARC.  Even if an optimization isn&#39;t currently implemented, it could say &quot;the language was designed for X optimization to be possible so don&#39;t assume Y&quot;.</div><div><br></div><div>I&#39;m also unsure how inlining works across modules/libraries.  whole-module is great but I assume we can&#39;t rely on that in the general case, unless we have c++ like &quot;header only&quot; libraries with full source.</div><div><br></div><div>Swift seems really great (the Linux build worked with no problems for me); there&#39;s just a lack of &quot;deep dive&quot; material at the moment, so I&#39;m left hanging wondering what the gotchas are with using it for a systems project. At the moment it&#39;s a few clues and hints to go off of. </div><div><br></div><div>- Karl</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><span class="HOEnZb"><font color="#888888"><div><br></div><div>-Joe</div></font></span><div><div class="h5"><br><blockquote type="cite"><div><div class="gmail_extra" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div class="gmail_quote">On Mon, Dec 7, 2015 at 11:53 AM, Joe Groff<span> </span><span dir="ltr">&lt;<a href="mailto:jgroff@apple.com" target="_blank">jgroff@apple.com</a>&gt;</span><span> </span>wrote:<br><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"><br><div><blockquote type="cite"><span><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><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">   <span> </span>precondition(sizeof(Foo) == 128)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">   <span> </span>let s = Foo()</font></div><div><font face="monospace, monospace">   <span> </span>for _ in 0..&lt;100_000_000 {</font></div><div><font face="monospace, monospace">       <span> </span>doSomething(s)</font></div><div><font face="monospace, monospace">   <span> </span>}</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><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> <span> </span>2.71 │50:┌─→xorps  %xmm0,%xmm0                                                                                          ▒</div><div> <span> </span>8.06 │   │  movaps %xmm0,-0x20(%rbp)                                                                                    ▒</div><div> <span> </span>2.71 │   │  movaps %xmm0,-0x30(%rbp)                                                                                    ▒</div><div> <span> </span>7.41 │   │  movaps %xmm0,-0x40(%rbp)                                                                                    ▒</div><div> 10.59 │   │  movaps %xmm0,-0x50(%rbp)                                                                                    ▒</div><div> 10.00 │   │  movaps %xmm0,-0x60(%rbp)                                                                                    ▒</div><div> <span> </span>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> <span> </span>3.41 │   │→ callq  _TF4main11doSomethingFVS_3FooT_                                                                      ▒</div><div> <span> </span>2.82 │   │  dec    %rbx                                                                                                 ▒</div><div> <span> </span>8.82 │   └──jne    50             </div><div><font face="monospace, monospace"><br></font></div>main.swift:<br><br><div>struct Vec4 {</div><div>   <span> </span>var a: Int64 = 0</div><div>   <span> </span>var b: Int64 = 0</div><div>   <span> </span>var c: Int64 = 0</div><div>   <span> </span>var d: Int64 = 0</div><div>}</div><div><br></div><div>struct Foo {</div><div>   <span> </span>var x: Vec4 = Vec4()</div><div>   <span> </span>var y: Vec4 = Vec4()</div><div>   <span> </span>var z: Vec4 = Vec4()</div><div>   <span> </span>var u: Vec4 = Vec4()</div><div>}</div><div><br></div><div>func test() {</div><div>   <span> </span>precondition(sizeof(Foo) == 128)</div><div><br></div><div>   <span> </span>let s = Foo()</div><div>   <span> </span>for _ in 0..&lt;100_000_000 {</div><div>       <span> </span>doSomething(s)</div><div>   <span> </span>}</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">   <span> </span>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:0px!important;margin:0px!important;padding:0px!important"><span> </span>_______________________________________________<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></div></blockquote></div></div></blockquote></div></div></div></blockquote></div></div></div><br></div></blockquote></div><br></div></div>