Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: David House <dhouse@janestreet.com>
Cc: Julien Blond <julien.blond@gmail.com>,
	Damien Guichard <alphablock@orange.fr>,
	 Caml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] How much optimized is the 'a option type ?
Date: Fri, 17 Jan 2014 10:10:26 +0100	[thread overview]
Message-ID: <CAPFanBHE=0D=Sgti3=GnxgUi=mCtFYixjpz_XANQFO5F1bwKQA@mail.gmail.com> (raw)
In-Reply-To: <CAK=fH+iO-_+gciaQ5gc1HzQNGUxT6+Z6w+9SGFnq2WgeBD9MCw@mail.gmail.com>

There have been recurrent discussions of optimizing `'a option` to
avoid allocation in some cases, which is interesting when it is used
as a default value for example. (The nice recent blog post by Thomas
Leonard also seems to assume that `'a option` is somehow optimized.)

My strictly personal opinion is that I doubt this would be a good
idea, because I expect a fair share of the programming practice that
currently use ('a option) to move to something like (('a,
error-description) either) later in their lifetime, and I wouldn't
want people to avoid to do that for performance concerns.
Historically, we've rather come to see special-case representation
optimizations (eg. array of floats) as a mistake -- but on the other
hand there is not much downside to record of floats.

The OCaml GC is very good at optimizing *short-lived* allocations, so
many idiomatic uses of option are in fact already quite fast despite
the allocation. Using an `'a option array` for values that start
undefined is not one of such cases. Hopefully in fifteen years we'll
be using programming languages with well-typed strong update such as
Mezzo ( http://protz.github.io/mezzo/ ), that can solve this problem
without any kind of ad-hoc optimization.

On Fri, Jan 17, 2014 at 9:40 AM, David House <dhouse@janestreet.com> wrote:
> Err, right, sorry. If you have None in, say, a record, that's not allocated
> at all. Rather than there being a pointer in that field, there is special
> word in that field which represents None.
>
> If that field is a Some, then it's a pointer to a two word allocated block
> which in turn points at the actual thing. So compared to a C pointer, there
> an extra two words and one more indirection.
>
>
> On 17 January 2014 08:16, Julien Blond <julien.blond@gmail.com> wrote:
>>
>> > An option value always takes two words: one for the header, and then
>> > either a pointer or a word that means "None".
>>
>> No. From the reference manual § 19.3.4 :
>>
>> type 'a option = None           (* Val_int(0), i.e. just an integer value
>> = 1 word *)
>>                      | Some of 'a   (* block of size 1 = [(header = 1
>> word) + (1 field = 1 word)] = 2 words *)
>>
>>
>> 2014/1/17 David House <dhouse@janestreet.com>
>>>
>>> It behaves identically to that type.
>>>
>>> It is just like any other sum type. However, due to the way that sum
>>> types are represented in memory, it is not that inefficient. The only thing
>>> that makes it less efficient than a C pointer is the header block (necessary
>>> for the GC). An option value always takes two words: one for the header, and
>>> then either a pointer or a word that means "None".
>>>
>>>
>>> On 17 January 2014 07:35, Damien Guichard <alphablock@orange.fr> wrote:
>>>>
>>>> Hello,
>>>>
>>>> Compared to the code :
>>>>
>>>> type 'a option = None | Some of 'a
>>>>
>>>> How do an 'a option value performs ?
>>>> Any allocation saved ?
>>>> Any indirection removed ?
>>>>
>>>> Is 'a option just like any sum type ?
>>>> Or is 'a option more like an ANSI C pointer type ?
>>>>
>>>> Regards,
>>>>
>>>> Damien Guichard
>>>>
>>>>
>>>>
>>>> --
>>>> Caml-list mailing list.  Subscription management and archives:
>>>> https://sympa.inria.fr/sympa/arc/caml-list
>>>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>>>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>>>
>>>
>>
>

  reply	other threads:[~2014-01-17  9:11 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-17  7:35 Damien Guichard
2014-01-17  7:55 ` David House
2014-01-17  8:16   ` Julien Blond
2014-01-17  8:40     ` David House
2014-01-17  9:10       ` Gabriel Scherer [this message]
2014-01-17  9:22         ` Simon Cruanes
2014-01-17 17:57           ` Gerd Stolpmann
2014-01-18  1:35             ` Jon Harrop
2014-01-19  6:19               ` oleg
2014-01-21  1:51                 ` Francois Berenger
2014-01-18  1:01         ` Jon Harrop
2014-01-24 10:06         ` Alain Frisch
2014-01-24 10:16           ` Alain Frisch
2014-01-24 13:32             ` Yaron Minsky
     [not found]       ` <CAK=fH+jfi=GsMYBZzmuo=V5UAWimyxiiamY2+DkLg6F0i8XHGw@mail.gmail.com>
2014-01-17  9:11         ` David House
2014-01-17 11:23           ` Jonathan Kimmitt
2014-01-17 13:46             ` Nicolas Braud-Santoni
2014-01-17 13:56               ` Frédéric Bour
2014-01-17 14:02               ` Yaron Minsky
2014-01-17 14:09                 ` Simon Cruanes
2014-01-17 22:52                   ` Yaron Minsky
2014-01-18  1:37                   ` Jon Harrop
2014-01-17 14:24                 ` Gabriel Scherer
2014-01-17 22:29                   ` Yaron Minsky
2014-01-18  1:27                 ` Jon Harrop
2014-01-18  1:18             ` Jon Harrop
2014-01-20 10:16             ` Goswin von Brederlow
2014-01-20 11:23               ` Jonathan Kimmitt
2014-01-21  2:05                 ` Francois Berenger
2014-01-22 21:22                   ` Jon Harrop
2014-01-22 21:26               ` Jon Harrop
2014-01-23  9:29                 ` Goswin von Brederlow
2014-01-23 23:20                   ` Jon Harrop
2014-01-23 23:28                     ` Yotam Barnoy
2014-01-24  8:22                       ` Jon Harrop
2014-01-24  8:34                         ` Andreas Rossberg
2014-01-24 16:56                           ` Jon Harrop
2014-01-27 15:29                             ` Goswin von Brederlow
2014-01-27 16:18                               ` Yotam Barnoy
2014-01-29  7:56                                 ` Goswin von Brederlow
2014-01-29  8:32                                 ` Jon Harrop
2014-01-29 16:11                                   ` Yotam Barnoy
2014-01-30 18:43                                     ` Yotam Barnoy
2014-02-01 15:58                                       ` Goswin von Brederlow
2014-01-30 21:31                                     ` Jon Harrop
2014-01-30 21:43                                       ` Yotam Barnoy
2014-01-31  8:26                                         ` Jon Harrop
2014-02-01 15:40                                 ` Goswin von Brederlow
2014-01-27 10:03                         ` Goswin von Brederlow
2014-01-17 14:36 ` Markus Mottl
2014-01-17 15:49   ` Yotam Barnoy
2014-01-17 16:22     ` Markus Mottl
2014-01-20 10:09   ` Goswin von Brederlow

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='CAPFanBHE=0D=Sgti3=GnxgUi=mCtFYixjpz_XANQFO5F1bwKQA@mail.gmail.com' \
    --to=gabriel.scherer@gmail.com \
    --cc=alphablock@orange.fr \
    --cc=caml-list@inria.fr \
    --cc=dhouse@janestreet.com \
    --cc=julien.blond@gmail.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