<div dir="ltr">I'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<Vector3<Float>> 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'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<T:ScalarType> : Array<T, <span style="color:rgb(39,42,216)">2</span>> {</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's so much wrong with that. You can't use inheritance with structs. Array isn't really a struct; the docs say it is but really it's a reference to a special copy-on-write value type. Array can't be a fixed size. You can't use literals with generic placeholders. Ok, fine, I accept this isn't C++, let'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<T:ScalarType> {<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>) -> <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'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'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'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's only four scalar types that are valid for OpenGL so this really isn't that hard. But it's not a complete solution and preprocessing core languages features only to gain performance is an indication the compiler isn'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'm still tempted to use a precondition and cast to an UnsafePointer.</div><div><br></div><div>I'm aware you can mark a class final and a method private to enable inlining. Except this isn't a class and making the API private, well, it's not an API then. Forcing @inline(__always) doesn'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'd be inlined when whole module optimization is enabled. Except that doesn'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'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>