English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Optimizing Float Ref's
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-03-31 (19:18)
From: Dmitry Bely <dmitry.bely@g...>
Subject: Re: [Caml-list] Optimizing Float Ref's
On Wed, Mar 31, 2010 at 10:59 PM, Alain Frisch <alain@frisch.fr> wrote:
> On 31/03/2010 19:21, Dmitry Bely wrote:
>> On Thu, Sep 3, 2009 at 1:44 PM, Xavier Leroy<Xavier.Leroy@inria.fr>
>>  wrote:
>> (...)
>>>> But, I thought that float ref's were automatically unboxed by the
>>>> compiler
>>>> when they didn't escape the local context.
>>> Yes, if all uses of the float ref are unboxed, which is the case in
>>> your code.
>> let max_val (a:float array) =
>>   let m = ref min_float in
>>   for i = 0 to (Array.length a) - 1 do
>>     if a.(i)>  !m
>>     then m := a.(i)
>>   done;
>>   !m
>> !m is not unboxed (Ocaml 3.11). Should it?
> As far as I can tell, the boxing for the reference cell is removed by the
> compiler (that is, the reference is implemented as a mutable local
> variable), but not the boxing for the float contained in the reference.
> Since a is a float array, it is needed to box the float before putting it in
> the reference, hence the allocation.

Hmm, my interpretation of Xavier's words was that the compiler would
completely optimize out memory allocation and keep !m in register. I
can live with the allocation but not at every iteration... How to
rewrite the function to get rid of it? My best achievement so far is

let max_val (a:float array) =
  let m = [|-.min_float|] in
  for i = 0 to (Array.length a) - 1 do
    if a.(i) > m.(0)
    then m.(0) <- a.(i)

but it looks ugly...

- Dmitry Bely