This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

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 (18:28) From: Dmitry Bely Subject: Re: [Caml-list] Optimizing Float Ref's
```On Wed, Mar 31, 2010 at 9:55 PM, Jake Donham <jake@donham.org> wrote:
> On Wed, Mar 31, 2010 at 10:21 AM, Dmitry Bely <dmitry.bely@gmail.com> wrote:
>> 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?
>
> Why do you say that? It looks unboxed in the lambda code:
>
>  (let
>    (max_val/58
>       (function a/59
>         (let (m/60 (field 13 (global Pervasives!)))
>           (seq
>             (for i/61 0 to (- (array.length a/59) 1)
>               (if (>. (array.get a/59 i/61) m/60)
>                 (assign m/60 (array.get a/59 i/61)) 0a))
>             m/60))))

No. "assign" is translated into memory allocation inside the loop.

> My x86 assembler is not very good but it looks to me like m is kept in %esi.

Ok, probably returning float makes allocation place not obvious. Try
this variant:

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;
truncate !m

It's translated into the following C-- code (-unsafe flag):

(let m/60 (load (+a "camlPervasives" 52))
(let (i/61 1 bound/65 (+ (or (>>u (load (+a a/59 -4)) 10) 1) -2))
(catch
(if (> i/61 bound/65) (exit 3)
(loop
(if
(>f (load float64u (+a (+a a/59 (<< i/61 2)) -4))
(assign m/60
(alloc 2301
(load float64u (+a (+a a/59 (<< i/61 2)) -4))))
[])
(let i/64 i/61 (assign i/61 (+ i/61 2))
(if (== i/64 bound/65) (exit 3) []))))
with(3) []))
(+ (<< (intoffloat (load float64u m/60)) 1) 1)))

See "alloc 2301" inside the loop? That's boxed float.

- Dmitry Bely

```