Others have already answered about memory representation, but there is
one thing about allocations that many OCaml programmers may not yet be
aware of and that can make a noticeable performance difference when
used wisely: the OCaml compiler will inspect your code and, under the
right conditions, batch together allocations. E.g. consider this
function (references are records, btw.):
let f n =
let x = ref (ref (Some (Some n))) in
[(x, n); (x, n)]
One might naively assume that this would lead to seven or so
allocations. But if you inspect the assembly, you'll see only one
allocation followed by mere initialization assignments.
Such allocation batches can break if, for example, the code calls
non-inlined (e.g. recursive or external) functions. I'm not quite
sure about all the rules there, but it's generally trivial to just
look at the assembler output to find out what the compiler is doing.
In some cases merely calling all functions before any allocations take
place can speed up your code. There are even cases where it may be
more efficient to allocate several values in one chunk though only
some may be needed depending on branches taken later.
Regards,
Markus
--
On Fri, Jan 17, 2014 at 2:35 AM, 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
Markus Mottl http://www.ocaml.info markus.mottl@gmail.com
--
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