caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Hurt <bhurt@spnz.org>
To: Warren Harris <warrensomebody@gmail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] cost of monads
Date: Sat, 21 Jun 2008 22:32:43 -0400 (EDT)	[thread overview]
Message-ID: <Pine.LNX.4.64.0806212219190.3616@localhost> (raw)
In-Reply-To: <3822B729-FD99-429E-8150-CEAA57E4D84F@gmail.com>



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


  parent reply	other threads:[~2008-06-22  1:56 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 [this message]
2008-06-22 19:02   ` Warren Harris

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=Pine.LNX.4.64.0806212219190.3616@localhost \
    --to=bhurt@spnz.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=warrensomebody@gmail.com \
    /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).