<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=""><div class=""><blockquote type="cite" class=""><div dir="ltr" class=""><span class="" style="font-family: monospace, monospace;">The intermediates are also things you’re going to want to store if anything for code clarity since the array literal starts looking messy when you shove long expressions into it.</span></div></blockquote><div class=""><br class=""></div><div class="">Yes, I agree. I guess having everything in the matrix looks kind of nice with simple expressions, but starts to become untenable once you need to do more than a few computations per element. Here I was more interested in learning something about the performance characteristics of the Swift type checker than writing good code. After all, this is a project that I started specifically to experiment.</div><br class=""><blockquote type="cite" class=""><div dir="ltr" class=""><span class="" style="font-family: monospace, monospace;">It also cuts down a little on the number of redundant operations you have to do (from 21 in your example to 16), especially divisions which you have six of in the original.</span></div></blockquote></div><div class=""><br class=""></div><div class="">I’m not so sure about this. I haven’t tried optimising this function for performance, it is called only once in my application for now, but I would expect Common Subexpression Elimination in the Swift compiler / LLVM to be able to automatically identify those redundancies.</div><div class=""><br class=""></div><div class="">Back to the point of compilation times, is there some resource that explains the performance of the type checker? I’ve been reading&nbsp;<a href="https://github.com/apple/swift/blob/master/docs/TypeChecker.rst#performance" class="">https://github.com/apple/swift/blob/master/docs/TypeChecker.rst#performance</a>, but that only illustrates possible optimisations to improve the performance from the side of the compiler. I’d also be curious to look at the constraints generated by the type checker, is there any flag that dumps them?</div><a href="https://github.com/apple/swift/blob/master/docs/TypeChecker.rst#performance" class=""></a><div class=""><br class=""></div><div class="">Regards,</div><div class="">
<div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: 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-size-adjust: auto; -webkit-text-stroke-width: 0px;">Elia Cereda</div>

</div>
<br class=""><div><blockquote type="cite" class=""><div class="">Il giorno 28 giu 2017, alle ore 22:21, Taylor Swift &lt;<a href="mailto:kelvin13ma@gmail.com" class="">kelvin13ma@gmail.com</a>&gt; ha scritto:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">i have a similar function in my code which uses four intermediates but compiles in reasonable time <br class=""><br class=""><span style="font-family:monospace,monospace" class="">&nbsp;&nbsp;&nbsp; {<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // frustum<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; let f_width:Float&nbsp; =&nbsp; self.half_h&nbsp; * self.twice_size,<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f_height:Float =&nbsp; self.half_k&nbsp; * self.twice_size,<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dx:Float&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = -self.shift_x / self.half_h,<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dy:Float&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = -self.shift_y / self.half_k<br class=""><br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; let clip_ratio:Float = 1000<br class=""><br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.projection_matrix =<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [self.z/f_width , 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; , 0&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; , 0,<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; , self.z/f_height, 0&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; , 0,<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dx&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; , dy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,&nbsp;&nbsp;&nbsp; (1 + clip_ratio) / (1 - clip_ratio),-1,<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; , 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; , self.z*2*clip_ratio / (1 - clip_ratio), 0]<br class="">&nbsp;&nbsp;&nbsp; }<br class=""><br class=""></span></div><span style="font-family:monospace,monospace" class="">The intermediates are also things you’re going to want to store if anything for code clarity since the array literal starts looking messy when you shove long expressions into it. It also cuts down a little on the number of redundant operations you have to do (from 21 in your example to 16), especially divisions which you have six of in the original.<br class=""></span></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Wed, Jun 28, 2017 at 1:51 PM, Elia Cereda via swift-users <span dir="ltr" class="">&lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class="">Hi,</div><div class=""><br class=""></div><div class="">I currently writing a demo app to teach myself the fundamentals of Metal and a big part of that is obviously working with matrices. What I’m seeing is that the code build them has some serious compilation time problems.</div><div class=""><br class=""></div><div class="">The worst case is this function, which according to&nbsp;-debug-time-function-bodies takes over 9500ms of time to compile.</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">static func frustum(l: Float, r: Float, t: Float, b: Float, n: Float, f: Float) -&gt; float4x4 {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; return float4x4(rows: [</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; &nbsp; &nbsp; [ 2 * n / (r - l), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,&nbsp; (r + l) / (r - l),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 ],</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; &nbsp; &nbsp; [ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0, &nbsp; &nbsp; 2 * n / (t - b),&nbsp; (t + b) / (t - b),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 ],</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; &nbsp; &nbsp; [ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0, -(f + n) / (f - n), -2 * f * n / (f - n) ],</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; &nbsp; &nbsp; [ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 ],</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; ])</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">}</div></div><div class=""><br class=""></div><div class="">I’ve tried making some naive changes to the code and gotten it down to a much more reasonable 4ms, but the result is not something I would write if given a choice.</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">static func frustum(l: Float, r: Float, t: Float, b: Float, n: Float, f: Float) -&gt; float4x4 {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; let twoN = 2 * n</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(102, 102, 102); min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="m_8653826296516026669webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; let rPlusL = r + l</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; let rMinusL = r - l</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(102, 102, 102); min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="m_8653826296516026669webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; let tPlusB = t + b</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; let tMinusB = t - b</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(102, 102, 102); min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="m_8653826296516026669webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; let fPlusN = f + n</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; let fMinusN = f - n</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(102, 102, 102); min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="m_8653826296516026669webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; return float4x4(rows: [</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; &nbsp; &nbsp; [ twoN / rMinusL,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,&nbsp; rPlusL / rMinusL, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 ],</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0, twoN / tMinusB,&nbsp; tPlusB / tMinusB, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 ],</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0, -fPlusN / fMinusN, -twoN * f / fMinusN ],</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -1, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 ],</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">&nbsp; &nbsp; ])</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class="">}</div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(102,102,102)" class=""><br class=""></div><div style="margin:0px;line-height:normal" class="">I’m taking this to swift-users since I’m aware this is a known pain point with the compiler. Is this specific instance something that would be worth filing a bug for?</div><div style="margin:0px;line-height:normal" class=""><br class=""></div><div style="margin:0px;line-height:normal" class="">Since I do not understand enough of the compiler to understand specifically what is causing problems with the first piece of code, I would also be extremely grateful if something from the core team (or anyone for that matter) could share some wisdom regarding what to do here.</div><div style="margin:0px;line-height:normal" class=""><br class=""></div><div style="margin:0px;line-height:normal" class="">Specifically, is there something that could be done to the first code to reduce the amount of overloads that the compiler needs to consider? In my naive view of the world, a sum or a multiplication between two Floats can only ever produce another Float, is there some way to pass this knowledge to the compiler?</div><div class=""><br class=""></div>Regards,<br class=""><div class="">
<div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;" class="">Elia Cereda</div>

</div>

<br class=""></div><br class="">______________________________<wbr class="">_________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-users</a><br class="">
<br class=""></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></body></html>