From: Guillaume Yziquel <guillaume.yziquel@citycable.ch>
To: david.baelde@ens-lyon.org
Cc: Nicolas Ojeda Bar <nojb@math.harvard.edu>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] reference cells
Date: Fri, 4 Feb 2011 13:30:45 +0100 [thread overview]
Message-ID: <20110204123043.GM21487@localhost> (raw)
In-Reply-To: <AANLkTinRM_4vpCVVWGtJ7G5Se5CjF5S8uiSibcE8uYMx@mail.gmail.com>
Le Friday 04 Feb 2011 à 12:49:28 (+0100), David Baelde a écrit :
> 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.
Yes. I understood that looking at the -dlambda compiler option. I didn't
quite grasp the scope of the original question at the time I answered.
> 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
So it's the 'makemutable' that makes all the difference.
By the way, is it possible to compile 'dlambda' code to native code directly?
It would be a nice compilation target.
--
Guillaume Yziquel
next prev parent reply other threads:[~2011-02-04 12:34 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
2011-02-04 12:30 ` Guillaume Yziquel [this message]
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=20110204123043.GM21487@localhost \
--to=guillaume.yziquel@citycable.ch \
--cc=caml-list@yquem.inria.fr \
--cc=david.baelde@ens-lyon.org \
--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