Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: function composition (.) vs function application ($) when it comes to performance?

In Haskell, when performance matters and using dollars or dots are both valid options, is one better than the other? Will one result in a performance gain over the other?

For example, given (foo . bar. baz) value and foo $ bar $ baz value, is one faster than the other?

like image 856
Mickael Bergeron Néron Avatar asked Mar 08 '15 13:03

Mickael Bergeron Néron


1 Answers

If you are compiling with optimizations (-O2), GHC will nearly certainly inline both . and $ and produce foo (bar (baz value)) in both cases (and then optimize it further). "Nearly" is just in case; inlining is one of most basic optimizations GHC does, and both . and $ are very simple and inlining them should always be a win, but I may not be thinking of some particular case. (One case I can think of when inlining them is harder is when they are partially applied, or passed to a higher-order function, but that's not the example given; or there could be a rewrite rule which fires before inlining and only covers one of these cases.)

However, you can always test it in your specific situation using e.g. Criterion. You can also verify that inlining happened by asking GHC to output Core files.

like image 197
Alexey Romanov Avatar answered Oct 04 '22 09:10

Alexey Romanov