Version française
Home     About     Download     Resources     Contact us    
Browse thread
Bigarray access optimization?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Dmitry Bely <dbely@m...>
Subject: Bigarray access optimization?
For the following code 

open Bigarray

let smooth (ba:(float, float32_elt, c_layout) Array2.t) =
    for i = 1 to (Array2.dim1 ba) - 2 do
      for j = 1 to (Array2.dim2 ba) - 2 do
        ba.{i,j} <- (ba.{i,j-1} +. ba.{i,j} +. ba.{i,j+1})/.3.0
      done
    done

ocamlopt generates the code that independently calculates offsets of
involved bigarray elements 4 times (each time using one "imul", one "add"
and two "sar" and several move instructions). Of course it's not necessary
- we just need to calculate offset once and then add or subtract
sizeof(float) to get ba.{i,j+1} and ba.{i,j-1} offsets accordingly. Can the
compiler perform such optimization? Say, testing if the difference between two
adjacent indexing expressions is a constant and then reusing the already
calculated offest if possible?

- Dmitry Bely

P.S. BTW, relaxing bounds checking for bigarrays when "-unsafe" option is
present seems to be quite easy. Is there a reason why it is not done?