Version française
Home     About     Download     Resources     Contact us    
Browse thread
Execution time of class versus record
[ 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] Execution time of class versus record
On Tuesday 26 June 2007 07:53:01 Loup Vaillant wrote:
> Err, I don't get it : I see exactly the same thing (written twice) here.
>
> Are you telling that :
> type t = A of int * int   <==>   type t = (A of int) * int

Nope:

# type t1 = A1 of int * int;;
type t1 = A1 of int * int
# type t2 = A2 of (int * int);;
type t2 = A2 of (int * int)

The former type has a contructor with two arguments. The latter type has a 
contructor with one argument that is a pair.

Only in the latter case can you contruct directly from a pair:

# let a = 1, 2;;
val a : int * int = (1, 2)
# A1 a;;
The constructor A1 expects 2 argument(s),
but is here applied to 1 argument(s)
# A2 a;;
- : t2 = A2 (1, 2)

This distinction does not appear with polymorphic variants because they always 
adopt the latter representation.

Despite the additional boxing, the performance trade-offs are non-trivial. For 
example, a pair can be extracted directly from the latter representation with 
no allocation required:

# function A2 x -> x;;
- : t2 -> int * int = <fun>

whereas the former requires the construction of a pair:

# function A1 x -> x;;
The constructor A1 expects 2 argument(s),
but is here applied to 1 argument(s)
# function A1(x, y) -> x, y;;
- : t1 -> int * int = <fun>

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The OCaml Journal
http://www.ffconsultancy.com/products/ocaml_journal/?e