From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 2A11EBC57 for ; Wed, 1 Sep 2010 10:32:18 +0200 (CEST) X-IronPort-AV: E=Sophos;i="4.56,303,1280700000"; d="scan'208";a="58162242" Received: from peermanence.saclay.inria.fr (HELO [195.83.212.205]) ([195.83.212.205]) by mail2-relais-roc.national.inria.fr with ESMTP; 01 Sep 2010 10:32:17 +0200 Message-ID: <4C7E0F60.6030406@inria.fr> Date: Wed, 01 Sep 2010 10:31:28 +0200 From: Fabrice Le Fessant User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.11) Gecko/20100713 Thunderbird/3.0.6 MIME-Version: 1.0 To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Re: Question about float refs. References: <20100823.140626.634675953541672216.Christophe.Troestler+ocaml@umons.ac.be> <031b01cb4944$77110900$65331b00$@com> In-Reply-To: X-Enigmail-Version: 1.0.1 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Spam: no; 0.00; unboxed:01 ocamlopt:01 compiler:01 compiler:01 unboxed:01 iteration:01 cheers:01 printf:01 printf:01 beginner's:01 ocaml:01 bug:01 scalable:01 orsay:01 orsay:01 If you replace (!r +. 0.) by (!r), you are passing the content of the reference as an argument to the function. Function arguments are not unboxed in ocamlopt, so the compiler sees a boxed occurrence of the float, and so must allocate it. The compiler could decide to have the float both as boxed and unboxed values, but since the reference is modified in the loop, there would be a risk of inconsistency, and so the compiler decides to have only a boxed value, and so the loop is much slower since it has to allocate the float at each iteration (the reference itself is probably not allocated). With (!r +. 0.), the float is not used as argument of a function, but in a floating-point computation, where unboxed values are allowed. As a consequence, the compiler discovers that all occurrences of the float can be unboxed, and so decides to unbox the value completely. --Fabrice Dmitry Bely wrote, On 09/01/2010 09:18 AM: > On Tue, Aug 31, 2010 at 11:41 PM, Jon Harrop > wrote: > >> You might prefer to multiply by 1.0 because adding 0.0 changes -0.0. >> >> Cheers, >> Jon. >> >>> Some black magic is needed: >>> >>> let r = ref 0.0 in >>> for i = 0 to 1000_000_000 do r := float i done; >>> Printf.printf "%f\n" (!r +. 0.); >>> Printf.printf "words: %f\n" (Gc.stat ()).Gc.minor_words >>> >>> and the reference will be placed in a register (Caml heap will not be >>> used inside the loop) > > Do you have any idea why the optimizer needs that? > > - Dmitry Bely > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > -- Fabrice LE FESSANT Chercheur, Equipe ASAP (As Scalable As Possible) http://www.lefessant.net/ INRIA-Futurs, Bat P - 112 Parc Orsay Université 2-4, rue Jacques Monod F-91893 Orsay Cedex, FRANCE