[swift-users] Design and performance of Vector2/3/4 and Matrix

Janosch Hildebrand jnosh at jnosh.com
Sat Dec 19 13:56:43 CST 2015

> On 19 Dec 2015, at 06:19, David Turnbull <dturnbull at gmail.com> wrote:
> On Fri, Dec 18, 2015 at 4:34 PM, Janosch Hildebrand <jnosh at jnosh.com <mailto:jnosh at jnosh.com>> wrote:
> If you only care about having a simple cross-platform library, doing a simple implementation by yourself is fine. Also Swift (well, LLVM) is also pretty good at auto-vectorization so you get decent results for custom vector/matrix types.
> However OpenGL-related code is usually pretty performance sensitive and the math parts doubly so, so I'd recommend wrapping some appropriate libraries instead of writing your own...
> Multiplying two mat4x4 is one of the most complex and common operations. Doing 2 million of these...
> C++: 1.3 seconds
> Swift: 1.9 seconds
> The C++ code is glm using packed SIMD operations. Swift is just plain whatever LLVM gave me with -O.
> While implementing the multiplication I darn near fell out of my chair when the compiler said, "Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions." I had to break it in half twice before the compiler stopped complaining about the math being too hard.

:-) It's not the math that's bothering the compiler but type inference which can get pretty nasty if you have many operations in a single statement.
The compiler emits that error to help you reduce compile times.

Depending on the code, specifying the resulting type can also be of help (eg var foo: T = <lots of math here>) but breaking up the code into smaller pieces will always work.

> -david (https://github.com/AE9RB/SwiftGL <https://github.com/AE9RB/SwiftGL>)

- Janosch

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20151219/588fa28c/attachment.html>

More information about the swift-users mailing list