From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 2284D7EC6E for ; Fri, 17 Jan 2014 15:36:23 +0100 (CET) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of markus.mottl@gmail.com) identity=pra; client-ip=74.125.82.51; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="markus.mottl@gmail.com"; x-sender="markus.mottl@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of markus.mottl@gmail.com designates 74.125.82.51 as permitted sender) identity=mailfrom; client-ip=74.125.82.51; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="markus.mottl@gmail.com"; x-sender="markus.mottl@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-wg0-f51.google.com) identity=helo; client-ip=74.125.82.51; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="markus.mottl@gmail.com"; x-sender="postmaster@mail-wg0-f51.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AsECAJFf2FJKfVIzlGdsb2JhbABZg0NWqCiSaYEHCBYOAQEBAQcLCwkSKoIlAQEBBEABFAcSCwEDDAYFCwMKDSEhAQERAQUBChIGEwgKh10BAxENnQ6MXIMJkhQKGScDCmSEchEBBQyMX4IUB4Q4BIlHjG6BbIExiyuDThgphHce X-IPAS-Result: AsECAJFf2FJKfVIzlGdsb2JhbABZg0NWqCiSaYEHCBYOAQEBAQcLCwkSKoIlAQEBBEABFAcSCwEDDAYFCwMKDSEhAQERAQUBChIGEwgKh10BAxENnQ6MXIMJkhQKGScDCmSEchEBBQyMX4IUB4Q4BIlHjG6BbIExiyuDThgphHce X-IronPort-AV: E=Sophos;i="4.95,670,1384297200"; d="scan'208";a="53725702" Received: from mail-wg0-f51.google.com ([74.125.82.51]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 17 Jan 2014 15:36:22 +0100 Received: by mail-wg0-f51.google.com with SMTP id z12so4485406wgg.18 for ; Fri, 17 Jan 2014 06:36:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=AOy4bFQS/Fyn868CkM+BD98wrK2SZyjz6miJ6XZo7pQ=; b=sQYB72n36sZdAMfVjU1OVVwM78kCXposCQ9VAWSNJSYrvUoWfhjirzFwgI1TaIdWly 0YboYWHmBnnu7jO4C309/NQveL2iJ47Wr/XRSrw/3ykWIfeCxpudExL52RQO8LuHWD6T /S5OHGFtW+zxD2nT6oDFTdikkmimN0HhSln0/a/vsaK5h+u1N95mRcbIuL1VfR8hfroL b9JWfEuHMkQjWkHWT7cKpucx2VvXuz/cXre44nNdWufNM4tJfI2KhUyjBBzPEp96RzzE ZFcFS1+KNs2HylPOQfF2LFM0LB9+L4FK25kOSFdier0dzKdnyJEq7m57M0UeB59BRi8k rGCw== MIME-Version: 1.0 X-Received: by 10.180.12.146 with SMTP id y18mr2743583wib.37.1389969382349; Fri, 17 Jan 2014 06:36:22 -0800 (PST) Received: by 10.195.13.41 with HTTP; Fri, 17 Jan 2014 06:36:22 -0800 (PST) In-Reply-To: <523666417617602473@orange.fr> References: <523666417617602473@orange.fr> Date: Fri, 17 Jan 2014 09:36:22 -0500 Message-ID: From: Markus Mottl To: Damien Guichard Cc: Caml Mailing List Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [Caml-list] How much optimized is the 'a option type ? 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