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

Numeric programming efficiency question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 1999-03-24 (14:17) From: Xavier Leroy Subject: Re: Numeric programming efficiency question
```> type vector = {x: float; y: float; z: float};;
> let vadd a b = {x = a.x +. b.x; y = a.y +. b.y; z = a.z +. b.z};;
> let vec (a,b,c) = {x=a; y=b; z=c};;
>
> I'm curious if the "shape changing" vec routine is optimized away in such
> an expression.  I would expect it to be, but that's just the wishful
> programmer in me.

The "vec" function is actually small enough to fall under the default
inlining threshold, and so it is inlined at the points of call.

actually), the inlining doesn't work because it conflicts with
an earlier optimization on constant data structures (this will have to
be fixed some day).  But in more complex examples such as

let f x x' = vadd (vec(x +. x', 0.0, 0.0)) (vec (x -. x', 0.0, 0.0))

the calls to "vec" are really inlined away, and the intermediate results
x +. x' and x -. x' are not heap-allocated separately.

So, it's not too bad, although it might not generate optimal code all
the time due to the rather simple-minded inlining and unboxing
algorithms used in ocamlopt.

- Xavier Leroy

```