[Haskell-cafe] Efficiency of passing function arguments
KS
kramosik at gmail.com
Sat Apr 4 17:32:11 UTC 2020
Thanks for everyone's answers.
The concept of "boxed" and "unboxed" types seems to be in the correct
direction. I found some relevant sections in GHC's user guide:
https://downloads.haskell.org/~ghc/8.8.3/docs/users_guide.pdf#page=269 . It
might be an interesting read.
Best,
Kram
On Sat, Apr 4, 2020 at 6:37 AM Will Yager <will.yager at gmail.com> wrote:
> 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?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20200404/e9f28717/attachment.html>
More information about the Haskell-Cafe
mailing list