Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Michael Ekstrand <michael@elehack.net>
To: caml-list@inria.fr
Subject: Re: [Caml-list] reference cells
Date: Mon, 31 Jan 2011 10:36:42 -0600	[thread overview]
Message-ID: <4D46E51A.8010907@elehack.net> (raw)
In-Reply-To: <476C4EB3-59D2-4756-927C-C1697E7AE4D8@math.harvard.edu>

On 01/31/2011 10:29 AM, Nicolas Ojeda Bar wrote:
> I am translating an imperative language into Ocaml. Right now I am
> translating mutable variables into ref cells. Will they be optimized
> when I compile the corresponding Ocaml program? Or will they be heap
> allocated? If this is the case, I will have to translate my language
> into some sort of SSA form before, and I would like to avoid that.

Most of them will be heap-allocated.  They may stay on the minor heap if
they're short-lived, so they may never hit the major heap (and thus
incur higher GC overhead), but they'll be on the heap.

I believe the compiler has optimizations to unbox some refs,
particularly for ints and floats, but for heap-allocated structures I
believe it will produce heap allocated references.

Further, mutating references can be a relatively expensive operation. 
In order to support the major-minor heap scheme, modifying a reference
that points to a non-integral value (anything that's heap allocated)
involves checking some tables to see where the reference and referenced
object are.  This is to keep track of all references in the major heap
which have been changed to point to objects on the minor heap.  Int refs
can avoid this problem, as integers are not stored on the heap, but for
anything more complex (including boxed floats), you incur the
caml_modify overhead.  This is true not only for references, but also
for mutable fields and array cells.

- Michael

  reply	other threads:[~2011-01-31 16:36 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-31 16:29 Nicolas Ojeda Bar
2011-01-31 16:36 ` Michael Ekstrand [this message]
2011-01-31 16:46 ` Guillaume Yziquel
2011-02-04 11:49   ` David Baelde
2011-02-04 12:30     ` Guillaume Yziquel
2011-01-31 18:12 ` Alain Frisch
2011-02-02 19:38   ` Jon Harrop

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=4D46E51A.8010907@elehack.net \
    --to=michael@elehack.net \
    --cc=caml-list@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