Version française
Home     About     Download     Resources     Contact us    

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

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: -- (:)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] Optimizing Float Ref's
On Sunday 30 August 2009 20:43:17 Yaron Minsky wrote:
> Float refs are not unboxed automatically, because refs 
> Are polymorphic containers. If you create your own pseudo-ref, i.e., a
> record with a single mutable float field, then I believe you should
> get the behaviour you expect.

I believe you are talking at cross purposes. Will wants his accumulator in a 
register. You are referring to float references in data structures being 
boxed because records are boxed.

Look at the compiled forms of the inner loops of these dot products, for 

let dot a b =
  let x = ref 0.0 in
  for i=0 to Array.length a - 1 do
    x := !x +. a.(i) *. b.(i)

.L103:  movl    caml_young_ptr, %eax
        subl    $12, %eax
        movl    %eax, caml_young_ptr
        cmpl    caml_young_limit, %eax
        jb      .L104
        leal    4(%eax), %eax
        movl    $2301, -4(%eax)
        fldl    -4(%edi, %ecx, 4)
        fmull   -4(%ebx, %ecx, 4)
        faddl   (%esi)
        fstpl   (%eax)
        movl    %eax, %esi
        movl    %ecx, %eax
        addl    $2, %ecx
        cmpl    %edx, %eax
        jne     .L101

let dot2 a b =
  let x = ref 0.0 in
  for i=0 to Array.length a - 1 do
    x := !x +. a.(i) *. b.(i)
  1.0 *. !x

        fldl    -4(%eax, %ecx, 4)
        fmull   -4(%ebx, %ecx, 4)
        faddl   0(%esp)
        fstpl   0(%esp)
        movl    %ecx, %edx
        addl    $2, %ecx
        cmpl    %esi, %edx
        jne     .L107

In the latter case, "x" is unboxed into a register.

> Come to think of it, I wonder if it would be better to implement ref
> on top of a single-cell array, since then everyone would get the float
> unboxing whenever applicable. I imagine there is some runtime overhead
> to this, though.

All-float (including one-float) records are unboxed anyway.

Boxing was discussed in the book OCaml for Scientists and the OCaml Journal 
articles about optimization and the SciMark2 benchmark.

Dr Jon Harrop, Flying Frog Consultancy Ltd.