caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Numeric programming efficiency question
@ 1999-03-22 18:33 James Hague
  1999-03-23 17:12 ` Xavier Leroy
  0 siblings, 1 reply; 3+ messages in thread
From: James Hague @ 1999-03-22 18:33 UTC (permalink / raw)
  To: caml-list

First of all, let me say that I've been having a great time learning
Objective CAML!

I implemented some simple functions that operate on three dimensional
vectors.  After reading the "Numeric Programming in CAML" document, it
seems that, unfortunately, the code resulting from using a more classic
syntax is less efficient than using structures.  That is, this:

let vadd (x0,y0,z0) (x1,y1,z1) = (x0 +. x1, y0 +. y1, z0 +. z1);;

generates poorer code than:

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};;

When using this function, one implementation has a more concise calling
syntax:

vadd (1.0,2.0,3.0) (10.0,20.0,30.0);;
vadd {x=1.0;y=2.0;z=3.0} {x=10.0;y.0;z=30.0};;

A utility routine makes the second option a little nicer:

let vec (a,b,c) = {x=a; y=b; z=c};;

This lets one write:

vadd vec(1.0,2.0,3.0) vec(10.0,20.0,30.0);;

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.

James Hague




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Numeric programming efficiency question
  1999-03-22 18:33 Numeric programming efficiency question James Hague
@ 1999-03-23 17:12 ` Xavier Leroy
  1999-03-23 19:32   ` James Hague
  0 siblings, 1 reply; 3+ messages in thread
From: Xavier Leroy @ 1999-03-23 17:12 UTC (permalink / raw)
  To: James Hague, caml-list

> 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};;
> vadd vec(1.0,2.0,3.0) vec(10.0,20.0,30.0);;
> 
> 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.

In your example above (which should read
        vadd (vec(1.0,2.0,3.0)) vec((10.0,20.0,30.0));;
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




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Numeric programming efficiency question
  1999-03-23 17:12 ` Xavier Leroy
@ 1999-03-23 19:32   ` James Hague
  0 siblings, 0 replies; 3+ messages in thread
From: James Hague @ 1999-03-23 19:32 UTC (permalink / raw)
  To: caml-list

Xavier Leroy wrote:
 
> 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.

I found the term "simple-minded" to be highly amusing, considering that
OCaml is generally considered to generate the best code of all free
implementations of functional languages using a pattern matching syntax! 

James Hague




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1999-03-24 14:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-03-22 18:33 Numeric programming efficiency question James Hague
1999-03-23 17:12 ` Xavier Leroy
1999-03-23 19:32   ` James Hague

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).