caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: maranget@yquem.inria.fr (Luc Maranget)
To: Jon Harrop <jon@ffconsultancy.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Efficency of varient types
Date: Mon, 28 Nov 2005 11:26:32 +0100	[thread overview]
Message-ID: <20051128102632.GA28969@yquem.inria.fr> (raw)
In-Reply-To: <200511281001.32158.jon@ffconsultancy.com>

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

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.

and

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


  reply	other threads:[~2005-11-28 10:26 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-25 23:53 Michael D. Adams
2005-11-26  0:31 ` [Caml-list] " Nicolas Cannasse
2005-11-26  1:22   ` Brian Hurt
2005-11-26  9:39     ` Nicolas Cannasse
2005-11-28  0:17     ` Obj or not Obj Christophe Raffalli
2005-11-28  8:41       ` [Caml-list] " Nicolas Cannasse
2005-11-28  9:27         ` Christophe Raffalli
2005-11-28  9:33         ` skaller
2005-11-28  8:43       ` Alain Frisch
2005-11-26  2:54   ` [Caml-list] Efficency of varient types skaller
2005-11-27  5:06   ` Michael D. Adams
2005-11-27  5:45     ` Brian Hurt
2005-11-27 10:02       ` Nicolas Cannasse
2005-11-27 15:35       ` Michael D. Adams
2005-11-27 18:08         ` Brian Hurt
2005-12-02 15:07           ` Michael D. Adams
2005-11-26  1:18 ` Jon Harrop
2005-11-27 14:57 ` Lukasz Stafiniak
2005-11-27 15:47   ` Lukasz Stafiniak
2005-11-28  8:14   ` Christophe Raffalli
2005-11-28  7:24 ` David Baelde
2005-11-28  7:49   ` Jacques Garrigue
2005-11-28 10:01     ` Jon Harrop
2005-11-28 10:26       ` Luc Maranget [this message]
2005-11-28  7:53   ` Ville-Pertti Keinonen
2005-12-01 17:05 ` Stefan Monnier
2005-12-02 15:07   ` [Caml-list] " Michael D. Adams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20051128102632.GA28969@yquem.inria.fr \
    --to=maranget@yquem.inria.fr \
    --cc=caml-list@yquem.inria.fr \
    --cc=jon@ffconsultancy.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).