caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: Chris Hecker <checker@d6.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] two unrelated questions
Date: Thu, 26 Apr 2001 15:47:27 +0200	[thread overview]
Message-ID: <20010426154727.E29782@pauillac.inria.fr> (raw)
In-Reply-To: <200104252108.OAA02055@smtp2-cm.mail.eni.net>; from checker@d6.com on Wed, Apr 25, 2001 at 02:08:33PM -0700

> 1.  What is the right "functional pattern" for early-outing on success
>     while using an iter/map/fold type function?
>     Is throwing an exception the best bet?

I believe so.

> 2.  I'm confused as to when the compiler decides to inline and when it
>     doesn't.  None of the calls in the following program get inlined,
>     regardless of the value of -inline in ocamlopt.

It's an embarrassing example of two distinct optimizations that
conflict, resulting in bad code :-)  

The first optimization is that constant data structures, such as the
tuple (1,2) in your example, are pre-built at compile-time rather than
dynamically built at run-time.  So, by the time we get to the inliner,
the "a" value is no longer syntactically a pair, but some sort of
special constant.

The second optimization is the tupled function optimization,
whereas functions taking a N-tuple of arguments are replaced by a
function taking N arguments.  Such functions are inlined only when
applied to an argument that is syntactically a N-tuple.  Otherwise,
the "slow path" (that takes a real tuple as argument, fetches
its N components, and jumps to the fast entry point) is followed, and
this turns inlining off.

You would get inlining in your example after rewriting it like this,
for instance:

> let myfst (a,b) = a
> let mysnd (a,b) = b
> 
> let myfsti ((a,b):int*int) = a
> let mysndi ((a,b):int*int) = b
>     
> let f x y =
>   myfst (x,y) + 1;
>   1 + mysnd (x,y);
>   myfsti (x,y);
>   mysndi (x,y)
>
> let _ = f 1 2

I agree this behavior is surprising and should be fixed at some point.

> (and after running dumpobj on the bytecode and
> looking at the asm from ocamlopt, we went and saw with dismay how
> Pervasives.fst and .snd are builtins and not inlined functions)

The bytecode compiler performs no inlining at all.  This is the main
reason why fst and snd are builtins (so that they do not generate
function calls in bytecoded programs).

- Xavier Leroy

-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


  parent reply	other threads:[~2001-04-26 13:47 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-04-25 21:08 Chris Hecker
2001-04-26  0:38 ` Patrick M Doane
2001-04-26  6:04   ` Judicaël Courant
2001-04-26 12:06     ` John Max Skaller
2001-04-27  9:12       ` Anton Moscal
2001-04-29 22:24         ` Brian Rogoff
2001-04-30 18:57           ` Fergus Henderson
2001-05-01  1:31           ` Jacques Garrigue
2001-05-01 12:45             ` [Caml-list] " Ken Friis Larsen
2001-04-27 15:09       ` [Caml-list] " Brian Rogoff
2001-04-27 17:49         ` John Max Skaller
2001-04-26  8:22   ` Andreas Rossberg
2001-04-26  1:13 ` Jacques Garrigue
2001-04-26 13:47 ` Xavier Leroy [this message]
2001-04-26 22:34   ` Chris Hecker
2001-04-26 16:57 ` Mark Seaborn
2001-04-26 22:20   ` John Max Skaller
2001-05-01 21:08     ` Brian Rogoff
2001-05-01 23:30       ` John Max Skaller
2001-05-02  0:03       ` John Max Skaller
2001-05-01 17:25 Dave Berry

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=20010426154727.E29782@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=checker@d6.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).