<div dir="ltr">I&#39;m working on a math library for OpenGL. At the core of this are, of course, scalar types. The scalars are grouped into vectors of length 2, 3, and 4. The vectors are used to build matrices of all variations from 2x2 to 4x4.<div><br></div><div>In order to be performant, scalars, vectors, and matrices must all be values types aka structs. This way, for example, an Array&lt;Vector3&lt;Float&gt;&gt; can be passed directly to OpenGL without any copying. In my testing so far, Swift does this quite well.</div><div><br></div><div>Ideally, I&#39;d do something like this:</div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">public struct</span> Vector2&lt;T:ScalarType&gt; : Array&lt;T, <span style="color:rgb(39,42,216)">2</span>&gt; {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">var</span> x:<span style="color:rgb(112,61,170)">T</span> { <span style="color:rgb(187,44,162)">get</span> {<span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(187,44,162)">self</span>[<span style="color:rgb(39,42,216)">0</span>]} <span style="color:rgb(187,44,162)">set</span> {<span style="color:rgb(187,44,162)">self</span>[<span style="color:rgb(39,42,216)">0</span>] = newValue} }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">var</span> y:<span style="color:rgb(112,61,170)">T</span> { <span style="color:rgb(187,44,162)">get</span> {<span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(187,44,162)">self</span>[<span style="color:rgb(39,42,216)">1</span>]} <span style="color:rgb(187,44,162)">set</span> {<span style="color:rgb(187,44,162)">self</span>[<span style="color:rgb(39,42,216)">1</span>] = newValue} }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p></div><div><br></div><div>But there&#39;s so much wrong with that. You can&#39;t use inheritance with structs. Array isn&#39;t really a struct; the docs say it is but really it&#39;s a reference to a special copy-on-write value type. Array can&#39;t be a fixed size. You can&#39;t use literals with generic placeholders. Ok, fine, I accept this isn&#39;t C++, let&#39;s move on to something Swifty.</div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">struct</span> Vector2&lt;T:ScalarType&gt; {<br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">var</span> x:<span style="color:rgb(112,61,170)">T</span>, y:<span style="color:rgb(112,61,170)">T</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">var</span> r:<span style="color:rgb(112,61,170)">T</span> { <span style="color:rgb(187,44,162)">get</span> {<span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(79,129,135)">x</span>} <span style="color:rgb(187,44,162)">set</span> {<span style="color:rgb(79,129,135)">x</span> = newValue} }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">var</span> g:<span style="color:rgb(112,61,170)">T</span> { <span style="color:rgb(187,44,162)">get</span> {<span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(79,129,135)">y</span>} <span style="color:rgb(187,44,162)">set</span> {<span style="color:rgb(79,129,135)">y</span> = newValue} }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">var</span> s:<span style="color:rgb(112,61,170)">T</span> { <span style="color:rgb(187,44,162)">get</span> {<span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(79,129,135)">x</span>} <span style="color:rgb(187,44,162)">set</span> {<span style="color:rgb(79,129,135)">x</span> = newValue} }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">var</span> t:<span style="color:rgb(112,61,170)">T</span> { <span style="color:rgb(187,44,162)">get</span> {<span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(79,129,135)">y</span>} <span style="color:rgb(187,44,162)">set</span> {<span style="color:rgb(79,129,135)">y</span> = newValue} }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">subscript</span>(i: <span style="color:rgb(112,61,170)">Int</span>) -&gt; <span style="color:rgb(112,61,170)">T</span> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">get</span> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            <span style="color:rgb(187,44,162)">switch</span>(i) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            <span style="color:rgb(187,44,162)">case</span> <span style="color:rgb(39,42,216)">0</span>: <span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(79,129,135)">x</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            <span style="color:rgb(187,44,162)">case</span> <span style="color:rgb(39,42,216)">1</span>: <span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(79,129,135)">y</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            <span style="color:rgb(187,44,162)">default</span>: <span style="color:rgb(61,29,129)">fatalError</span>()</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">set</span> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            <span style="color:rgb(187,44,162)">switch</span>(i) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            <span style="color:rgb(187,44,162)">case</span> <span style="color:rgb(39,42,216)">0</span>: <span style="color:rgb(79,129,135)">x</span> = newValue</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            <span style="color:rgb(187,44,162)">case</span> <span style="color:rgb(39,42,216)">1</span>: <span style="color:rgb(79,129,135)">y</span> = newValue</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            <span style="color:rgb(187,44,162)">default</span>: <span style="color:rgb(61,29,129)">fatalError</span>()</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p></div><div><br></div><div>Functionally, this works fine. The x and y properties are the struct data. I can access vectors with subscripts, both coordinate properties, and color properties  It&#39;s exactly what someone using an OpenGL vector type would expect. You can make these into arrays, use them in other structs which are made into arrays, and pass them to OpenGL just fine.</div><div><br></div><div>But I hit some performance issues. Let&#39;s use myvec.x as a baseline. This is always inlined and as fast as C.</div><div><br></div><div>You might expect myvec.r to have the same performance. It does in other languages but not Swift. The performance penalty is 20X. Yes, twenty times slower than myvec.x. The performance hit comes entirely from dynamic dispatch. 10X because it&#39;s not inlined, and another 10X because Vector2 is a template.</div><div><br></div><div>I can get rid of 10X of that by writing my own preprocessor for the template. There&#39;s only four scalar types that are valid for OpenGL so this really isn&#39;t that hard. But it&#39;s not a complete solution and preprocessing core languages features only to gain performance is an indication the compiler isn&#39;t doing optimization as well as it could.</div><div><br></div><div>Subscript access is the same 20X slower for the same reasons. The switch disappears into the noise. But I&#39;m still tempted to use a precondition and cast to an UnsafePointer.</div><div><br></div><div>I&#39;m aware you can mark a class final and a method private to enable inlining. Except this isn&#39;t a class and making the API private, well, it&#39;s not an API then. Forcing @inline(__always) doesn&#39;t seem to do anything.</div><div><br></div><div>Perhaps I could just not make this a module and leave everything internal. Supposedly it&#39;d be inlined when whole module optimization is enabled. Except that doesn&#39;t happen.</div><div><br></div><div>How can I get these property aliases to be inlined? Is it possible today? Will it be possible in the future? Is there a different pattern for vectors that&#39;s better suited to the task?</div><div><br></div><div>-david (<a href="https://github.com/AE9RB/SwiftGL">https://github.com/AE9RB/SwiftGL</a>)</div><div><br></div><div><br></div></div>