Add @inlinable attribute to improve performance #163
Replies: 2 comments
-
Found out about swift build -c release -Xswiftc -cross-module-optimization provides the same performance as when using The flag seems to be available only from Swift 5.2 and looks like it's still a somewhat obscure bit of knowledge because I couldn't find much info about it. It's also not enabled by default, so it might still make sense to consider adding the |
Beta Was this translation helpful? Give feedback.
-
@mortigenus Thanks for the discussion (and apologies for the radio silence, we don't actively maintain this repo right now). I think you bring up good points, and if we were actively maintaining Prelude it's something we'd certainly look into, as we've done similar inlining in other projects (like swift-parsing, [swift-identified-collections]https://github.com/pointfreeco/swift-identified-collections), and swift-gen). I'm going to convert this to a discussion for now, as it may be useful information for those maintaining their own forks of Prelude. |
Beta Was this translation helpful? Give feedback.
-
I've been using swift-prelude when solving some coding challenges and noticed that when I start using operators in tight loops performance noticeably drops. Using Instruments I found out that a lot of time is spent in places like
closure #1 in >>> infix<A, B, C>(_:_:)
Simple example. This code takes around a second on my machine:
(To get running time I used SwiftPM to create my package with this code and included swift-prelude with SwiftPM as well. I then built my package in Release configuration and ran it with
time
on the command line.)Let's rewrite it with function composition:
Now it takes a bit more than 2 seconds on my machine.
What if we use global map function?
It now takes 25 seconds.
If I declare these two operators and global
map
function in my code the same way they are declared in the package instead of importing Prelude, running time will drop to around 0.75s.Since Swift 4.2 there's an
@inlinable
attribute (proposal):Considering fundamental nature of the functions in this package I think marking them with
@inlinable
should be pretty safe, but I'm not very far in your video series and not yet familiar with most of the things in this repo, so I'm not making a PR with the proposed change, sorry about that.Beta Was this translation helpful? Give feedback.
All reactions