From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 8612F7F706 for ; Fri, 17 Jan 2014 16:50:14 +0100 (CET) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of yotambarnoy@gmail.com) identity=pra; client-ip=209.85.128.45; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="yotambarnoy@gmail.com"; x-sender="yotambarnoy@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of yotambarnoy@gmail.com designates 209.85.128.45 as permitted sender) identity=mailfrom; client-ip=209.85.128.45; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="yotambarnoy@gmail.com"; x-sender="yotambarnoy@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-qe0-f45.google.com) identity=helo; client-ip=209.85.128.45; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="yotambarnoy@gmail.com"; x-sender="postmaster@mail-qe0-f45.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AssDAJFf2FLRVYAtlWdsb2JhbABZg0NWqCiKFIhVgQcIFg4BAQEBBw0JCRIqgiUBAQEDAUABFAcSCwEDAQsGBQsNDSEhAQERAQUBChIGExKHXQEDCQgNnQ6MXIMJkhQKGScDCmSEchEBBQyMX4IQBAeEOASJR4xugWyBMYsrg04YKYR3Hg X-IPAS-Result: AssDAJFf2FLRVYAtlWdsb2JhbABZg0NWqCiKFIhVgQcIFg4BAQEBBw0JCRIqgiUBAQEDAUABFAcSCwEDAQsGBQsNDSEhAQERAQUBChIGExKHXQEDCQgNnQ6MXIMJkhQKGScDCmSEchEBBQyMX4IQBAeEOASJR4xugWyBMYsrg04YKYR3Hg X-IronPort-AV: E=Sophos;i="4.95,674,1384297200"; d="scan'208";a="45199208" Received: from mail-qe0-f45.google.com ([209.85.128.45]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 17 Jan 2014 16:50:13 +0100 Received: by mail-qe0-f45.google.com with SMTP id x7so1509374qeu.4 for ; Fri, 17 Jan 2014 07:50:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=IJFdnyWNY8oVldf6VfFmPwzx8eD5FW/Ap04WbB6d8P8=; b=qZqJ5HNr8lyEQYbpDOWUR1wwI2G1R+0YryvlQXqQK6eCvzqieTWdFzkDIfD9j8LxHx 1VXc0CLq1lA09w+5Ye4QQoSQHxWINrKVRpncKUNYJSyJknp4CyKeu+USyazd5O+l7/ph 7ZCf4aqyuyz395zBlMoJQpXXVnxYCiJkKEcCPf0UNUW/x8eHjaAwFdI08dIuXr1jX37Q OCFbRmjR+PHYHGWLbAXCyGw42B+kU4tv8i9jvmQqy4mz9fms2IrHApOxPHC6U7EA9Mf0 9CVNCjxlBciw/seE2H+6NXEHymO9dKtSoyXUsF6aOrZSupOe564h3U38EotjLz7swM81 fYig== X-Received: by 10.229.102.4 with SMTP id e4mr4450318qco.2.1389973812028; Fri, 17 Jan 2014 07:50:12 -0800 (PST) MIME-Version: 1.0 Received: by 10.224.95.8 with HTTP; Fri, 17 Jan 2014 07:49:50 -0800 (PST) In-Reply-To: References: <523666417617602473@orange.fr> From: Yotam Barnoy Date: Fri, 17 Jan 2014 10:49:50 -0500 Message-ID: To: Markus Mottl Cc: Damien Guichard , Caml Mailing List Content-Type: multipart/alternative; boundary=001a1133ba2c502c7104f02c7fdc Subject: Re: [Caml-list] How much optimized is the 'a option type ? --001a1133ba2c502c7104f02c7fdc Content-Type: text/plain; charset=ISO-8859-1 That reminds me -- what is the situation regarding inlining? Is there work being done on inlining from other modules? Is heavy inlining being done within a module? Would it be possible to annotate with [@@inline] to give hints to the compiler at some point? -Yotam On Fri, Jan 17, 2014 at 9:36 AM, Markus Mottl wrote: > 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 > 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 > --001a1133ba2c502c7104f02c7fdc Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
That reminds me -- what is the situation regarding in= lining? Is there work being done on inlining from other modules? Is heavy i= nlining being done within a module? Would it be possible to annotate with [= @@inline] to give hints to the compiler at some point?

-Yotam


On Fri, Jan 17, 2014 at 9:36 AM, Markus Mottl <markus.mo= ttl@gmail.com> wrote:
Others have already answered about memory re= presentation, 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. =A0E.g. consider this
function (references are records, btw.):

=A0 let f n =3D
=A0 =A0 let x =3D ref (ref (Some (Some n))) in
=A0 =A0 [(x, n); (x, n)]

One might naively assume that this would lead to seven or so
allocations. =A0But 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. =A0I'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. =A0There 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 =3D 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. =A0Subscription 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 =A0 =A0 =A0 =A0http://www.ocaml.info =A0 =A0 =A0 =A0markus.mottl@gmail.com

--
Caml-list mailing list. =A0Subscription management and archives:
ht= tps://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

--001a1133ba2c502c7104f02c7fdc--