caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David Baelde <david.baelde@gmail.com>
To: Guillaume Yziquel <guillaume.yziquel@citycable.ch>
Cc: Nicolas Ojeda Bar <nojb@math.harvard.edu>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] reference cells
Date: Fri, 4 Feb 2011 12:49:28 +0100	[thread overview]
Message-ID: <AANLkTinRM_4vpCVVWGtJ7G5Se5CjF5S8uiSibcE8uYMx@mail.gmail.com> (raw)
In-Reply-To: <20110131164630.GN18069@localhost>

On Mon, Jan 31, 2011 at 5:46 PM, Guillaume Yziquel
<guillaume.yziquel@citycable.ch> wrote:
>> # let x = ref 10;;
>> val x : int ref = {contents = 10}
>> # Obj.tag (Obj.repr x);;
>> - : int = 0

A funny side note about that test: the fact that you pass your
reference to a function (Obj.repr) prevents the compiler from
optimizing it. So even if you try (let x = ref 10 in Obj.tag (Obj.repr
x)) you'll see the same tag, while (as Alain said) Ocaml can actually
avoid heap allocation in similar expressions where your reference is
clearly local.

The lambda code remains the best option:

$ cat test.ml
let () = let x = ref 10 in Printf.printf "%d\n" (Obj.tag (Obj.repr !x))
let () = let x = ref 10 in Printf.printf "%d\n" (Obj.tag (Obj.repr x))

$ ocamlc -dlambda test.ml
(setglobal Test!
  (let
    (match/61
       (let (x/58 10)
         (apply (field 1 (global Printf!)) "%d\n" (caml_obj_tag (id x/58))))
     match/60
       (let (x/59 (makemutable 0 10))
         (apply (field 1 (global Printf!)) "%d\n" (caml_obj_tag (id x/59)))))
    (makeblock 0)))

Cheers,
-- 
David

  reply	other threads:[~2011-02-04 11:49 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
2011-01-31 16:46 ` Guillaume Yziquel
2011-02-04 11:49   ` David Baelde [this message]
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=AANLkTinRM_4vpCVVWGtJ7G5Se5CjF5S8uiSibcE8uYMx@mail.gmail.com \
    --to=david.baelde@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=david.baelde@ens-lyon.org \
    --cc=guillaume.yziquel@citycable.ch \
    --cc=nojb@math.harvard.edu \
    /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).