Monday, April 12, 2021

High Performance Numeric Programming With Swift

Jeremy Howard (via Frank Illenberger):

I’ve managed to create a couple of libraries that can achieve the same speed as carefully optimized vectorized C code, whilst being concise and easy to use. […] I will include examples mainly from my BaseMath library, which provides generic math functions for Float and Double, and optimized versions for various collections of them.

[…]

One of the really cool things about Swift is that wrappers like the above have no run-time overhead. As you see, I’ve marked them with the inlinable attribute, which tells LLVM that it’s OK to replace calls to this function with the actual function body. This kind of zero-overhead abstraction is one of the most important features of C++; it’s really amazing to see it in such a concise and expressive language as Swift.

[…]

Normally, because Swift has to handle the complexities of COW, it can’t fully optimize a loop like this. But by using a pointer instead, we skip those checks, and Swift can run the code at full speed. Note that due to copy-on-write it’s possible for the array to move if you assign to it, and it can also move if you do things such as resize it; therefore, you should only grab the pointer at the time you need it.

[…]

I think this is quite remarkable; we’ve been able to create a simple API which is just as fast as the pointer code, but to the class user that complexity is entirely hidden away.

[…]

I also find Swift’s performance is harder to reason about and optimize than C.

Previously:

Update (2021-04-15): Tanner Bennett:

TIL @inlineable is no longer a private attribute

2 Comments RSS · Twitter


Ah, those were the days. Remember how optimistic we were back before S4TF got "archived"?


Old Unix Geek

It's not archived... it's pining for the fjords. ( https://www.youtube.com/watch?v=vnciwwsvNcc )

Leave a Comment