caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Warren Harris <warrensomebody@gmail.com>
To: Brian Hurt <bhurt@spnz.org>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] cost of monads
Date: Sun, 22 Jun 2008 12:02:10 -0700	[thread overview]
Message-ID: <B563F7A3-7142-4831-A439-60DEE99521C4@gmail.com> (raw)
In-Reply-To: <Pine.LNX.4.64.0806212219190.3616@localhost>

[-- Attachment #1: Type: text/plain, Size: 2866 bytes --]

Brian,

Thanks for your response. I realize that the cost will be very  
application-dependent, which is why I'm seeking other's practical  
experience programming with these techniques, particularly for stacked  
monad transformers involving simple monads (e.g. for interpreted  
languages).

I can relay a little of my own practical experience in writing a  
monadic parser for a character-oriented grammar -- it is not  
practical. The performance was at least an order-of-magnitude worse  
than the yacc-based parser I later wrote. (Although the idea I was  
just pointed at of using metaocaml for this would seem to offer the  
best of both worlds: http://www.cas.mcmaster.ca/~carette/publications/scp_metamonads.pdf)

Warren


On Jun 21, 2008, at 7:32 PM, Brian Hurt wrote:

>
>
> On Sat, 21 Jun 2008, Warren Harris wrote:
>
>> I'm considering writing a moderate sized program with high  
>> performance needs in a monad / monad transformer style in ocaml.  
>> Although I believe that this abstraction will offer me benefits in  
>> hiding some complexity, some of the monad transformers I would like  
>> to "stack" are quite simple (e.g. a state-transition monad), and  
>> I'm concerned that my program will be paying a high performance  
>> cost due to high function call overhead -- ones which cannot be  
>> optimized away due to module boundaries.
>
> The performance hit of monads are two-fold: 1) generally, bind  
> requires an allocation, and 2) functorization and partial  
> application defeat inlining, and require more expensive call  
> semantics (basically, you end up having to call caml_applyn where  
> normally you'd just directly call, or even jump to, the function in  
> question).
>
> How much of a penalty this is depends upon how often the monad layer  
> is invoked, or how much work is performed per bind.  If the cost of  
> a bind is, say, 10 clocks, and on average you're doing a bind every  
> 20 clocks, that's a huge hit- perfomance just dropped by a factor of  
> 50%.  But if you only bind every 200 clocks, then it's only a 5%  
> hit, and it is much less a big deal.  I pull these numbers out of me  
> rear end, but they're probably vaguely close to correct.
>
> The point is that it's impossible to generally state what the  
> performance hit of monads are, because that's dependent upon how  
> they're used.
>
> For performance-sensitive code, I'd probably stay away from higher  
> level abstractions.  On the other hand, I'd also consider how  
> performance sensitive the code really is- we programmers have a bad  
> habit of wanting to assume that all code needs to be tuned to within  
> an inch of it's life- but the reality is hardly any code needs to be  
> tuned at all (witness the popularity of languages like Ruby, Python,  
> and PHP- all of which make Java look like greased lightning).
>
> Brian
>


[-- Attachment #2: Type: text/html, Size: 3242 bytes --]

      reply	other threads:[~2008-06-22 19:02 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-21 18:23 Warren Harris
2008-06-21 23:41 ` [Caml-list] " David Teller
2008-06-22  2:32 ` Brian Hurt
2008-06-22 19:02   ` Warren Harris [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=B563F7A3-7142-4831-A439-60DEE99521C4@gmail.com \
    --to=warrensomebody@gmail.com \
    --cc=bhurt@spnz.org \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).