[Haskell-cafe] Efficiency of passing function arguments

Will Yager will.yager at gmail.com
Sat Apr 4 10:37:07 UTC 2020


As a slight refinement on the previous responses,

* you do not need to worry about this at all, at least not in the direction you’re thinking. 
* you don’t need to use IORef or anything for efficiency - almost everything is passed by reference already (one benefit of immutable data)
* GHC can automatically switch to pass-by-value when appropriate 

In fact, the tricky part is actually getting your program to *stop* passing by reference when there’s a performance gain associated with pass-by-value. 

My mental model of GHC’s rule for when to use pass-by-value is approximately 

* it won’t change the semantics of the problem
* the value is small

E.g. if we’re writing a very fast small loop that operates over Int64s, there’s no point passing by reference when a value is the same size as a pointer anyway, and we can avoid extraneous dereferences and heap allocations. 

The interesting part is “won’t change the semantics of the program”. That basically boils down to the fact that pass-by-value is strict, so often times all you need to do is tell GHC that a function is strict on some of its arguments and GHC can choose to make those arguments PBV. See BangPatterns.  

Haskellers don’t normally use the terminology PBV and PBR, however, but rather “unboxed” and “boxed” (referring to the values themselves). 

Will


> On Apr 4, 2020, at 12:55 AM, KS <kramosik at gmail.com> wrote:
> 
> Should I worry about this at all?


More information about the Haskell-Cafe mailing list