Version française
Home     About     Download     Resources     Contact us    

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

Browse thread
Efficency of varient types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-11-28 (10:26)
From: maranget@y...
Subject: Re: [Caml-list] Efficency of varient types
> On Monday 28 November 2005 07:49, Jacques Garrigue wrote:
> > More importantly, polymorphic variants are internally encoded as
> > integers, so there is no representation cost for constant tags.
> Is pattern matching over constant ordinary variants not more efficient than 
> pattern matching over constant polymorphic variants when compiled to native 
> code?
> -- 

As far as I understandteh question, constant constructors both in
ordinary variants and polymorphic variants are encoded as integers.

Hence matching over those are basically the same. However there is
one (small) difference.

Ordinary variants are translated into consecutive integers, starting
from zero, while polymorphic variant are more arbitrary integers.
As a result the final code uses some tricks more often in the first

While obvious in particular cases, the resulting efficiency gain is,
well, difficult to assess in general.

Consider for instance :

let rec fib x = match x with
| 0|1 -> 1
| n -> fib (n-1) + fib (n-2)
  Compiled code for matching will consist in one (unsigned) test.


let rec fob x = match x with
| `ZeroIMean|`OneISay -> 1
| n -> ...
  Compiled code for matching will consist in two (equality) tests.

Your mileage may vary, of course.

-- Luc