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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With