From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p14CYTcu023774 for ; Fri, 4 Feb 2011 13:34:30 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AhwSAH+BS01V2gB4ZGdsb2JhbACXDo4nGgsKBhIkuWsNhU0Ejy4 X-IronPort-AV: E=Sophos;i="4.60,426,1291590000"; d="scan'208";a="99243737" Received: from emailfrontal1.citycable.ch ([85.218.0.120]) by mail1-smtp-roc.national.inria.fr with SMTP; 04 Feb 2011 13:34:28 +0100 X-Alinto-smtpauth-localdomain: Yes Received: from seldon (unknown [85.218.92.99]) (Authenticated sender: guillaume.yziquel@citycable.ch) by emailfrontal1.citycable.ch (Postfix) with ESMTPA id 1F8B812C945; Fri, 4 Feb 2011 13:34:27 +0100 (CET) Received: from yziquel by seldon with local (Exim 4.72) (envelope-from ) id 1PlKoJ-0001P2-1X; Fri, 04 Feb 2011 13:30:57 +0100 Date: Fri, 4 Feb 2011 13:30:45 +0100 From: Guillaume Yziquel To: david.baelde@ens-lyon.org Cc: Nicolas Ojeda Bar , caml-list@yquem.inria.fr Message-ID: <20110204123043.GM21487@localhost> References: <476C4EB3-59D2-4756-927C-C1697E7AE4D8@math.harvard.edu> <20110131164630.GN18069@localhost> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id p14CYTcu023774 Subject: Re: [Caml-list] reference cells Le Friday 04 Feb 2011 à 12:49:28 (+0100), David Baelde a écrit : > On Mon, Jan 31, 2011 at 5:46 PM, Guillaume Yziquel > 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