caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* record field access
@ 2006-10-21  3:53 j.romildo
  2006-10-21  7:23 ` [Caml-list] " Jon Harrop
  0 siblings, 1 reply; 3+ messages in thread
From: j.romildo @ 2006-10-21  3:53 UTC (permalink / raw)
  To: caml-list

Hello.

Regarding performance, is there any difference in accessing a record
field using pattern matching and using the dot notation?

For instance, given the declarations,

   type point = { x: float; y: float }

   let sqr x = x *. x

   let dist1 p q =
      sqrt (sqr (q.x -. p.x) +. sqr (q.y -. p.y))

   let dist2 {x=x1; y = y1} {x=x2; y=y2} =
      sqrt (sqr (x2 -. x1) +. sqr (y2 -. y1))

what should be preferable: dist1 or dist2?

And to compare records with tuples, given also

   let dist3 (x1,y1) (x2,y2) =
      sqrt (sqr (x2 -. x1) +. sqr (y2 -. y1))

what should be preferable: dist2 or dist3, regarding performance?

Romildo


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

* Re: [Caml-list] record field access
  2006-10-21  3:53 record field access j.romildo
@ 2006-10-21  7:23 ` Jon Harrop
  2006-10-21 14:07   ` Christophe Raffalli
  0 siblings, 1 reply; 3+ messages in thread
From: Jon Harrop @ 2006-10-21  7:23 UTC (permalink / raw)
  To: caml-list

On Saturday 21 October 2006 04:53, j.romildo@gmail.com wrote:
> And to compare records with tuples, given also
>
>    let dist3 (x1,y1) (x2,y2) =
>       sqrt (sqr (x2 -. x1) +. sqr (y2 -. y1))
>
> what should be preferable: dist2 or dist3, regarding performance?

Specifically for all-float tuples and records, the internal representations 
are different, with all-float records being unboxed. The performance 
characteristics will be correspondingly different.

Otherwise, I don't know. I tried a lot of different combinations to try to 
optimise my ray_sphere routine in the ray tracer. I got the impression that 
it can affect register allocation, e.g. no CSE => pulling out {a.x=ax} in a 
pattern requires an extra register but avoids repeated indirection (ax vs 
a.x).

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists


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

* Re: [Caml-list] record field access
  2006-10-21  7:23 ` [Caml-list] " Jon Harrop
@ 2006-10-21 14:07   ` Christophe Raffalli
  0 siblings, 0 replies; 3+ messages in thread
From: Christophe Raffalli @ 2006-10-21 14:07 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list


>
> Otherwise, I don't know. I tried a lot of different combinations to try to 
> optimise my ray_sphere routine in the ray tracer. I got the impression that 
> it can affect register allocation, e.g. no CSE => pulling out {a.x=ax} in a 
> pattern requires an extra register but avoids repeated indirection (ax vs 
> a.x).
>
>   
I think register allocation shoud make the pattern preferable, because 
if there are to much registers, one indirection is needed anyway, and 
for a record field
there is no need to store the value in the stack, because we know it is 
inside the record ?

What I mean is that in

let { l = x } = r in (* l being immutable or not muted in the ...*)
  ...
  x
  ...
  x
  ...
  x

some x may be in register, other may be implemented as r.x, but if  
there is not enough registers, the compiler should know
that it does not have to reserve stack space to store x.

Christophe Raffalli


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

end of thread, other threads:[~2006-10-21 15:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-10-21  3:53 record field access j.romildo
2006-10-21  7:23 ` [Caml-list] " Jon Harrop
2006-10-21 14:07   ` Christophe Raffalli

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).