English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    
Browse thread
Question about float refs.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Fabrice Le Fessant <fabrice.le_fessant@i...>
Subject: Re: [Caml-list] Re: Question about float refs.
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
> <jonathandeanharrop@googlemail.com> 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