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 200EC7FD3A for ; Mon, 30 Nov 2015 00:28:10 +0100 (CET) IronPort-PHdr: 9a23:WXDo4hYOcDkjXqorGFmG2s7/LSx+4OfEezUN459isYplN5qZpM26bnLW6fgltlLVR4KTs6sC0LqL9fC+EjVesN6oizMrTt9lb1c9k8IYnggtUoauKHbQC7rUVRE8B9lIT1R//nu2YgB/Ecf6YEDO8DXptWZBUiv2OQc9HOnpAIma153xjLDvvc2NKFwW1XKUWvBbElaflU3prM4YgI9veO4a6yDihT92QdlQ3n5iPlmJnhzxtY+a9Z9n9DlM6bp6r5YTGY2zRakzTKRZATI6KCh1oZSz7ViQezCS/WMRWXk6lR9BAg6NrE2rH8S5jiyvk+Nn22G6NNz6QLZ8DTGr9aRkSBb5hCoBHy8w8GbQzMd3ifQIjgimoklZwoXTZIiOfN18c6/UNfkTXyIVWMdaUChIGcW7bYYDBqwLNPxwq4TtplUP6xC5AF//V6vU1jZUiyqujuUB2OM7HFSDgVUt Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=nicolas.ojeda.bar@lexifi.com; spf=None smtp.mailfrom=nicolas.ojeda.bar@lexifi.com; spf=None smtp.helo=postmaster@mx20.yaziba.net Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of nicolas.ojeda.bar@lexifi.com) identity=pra; client-ip=85.233.204.164; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="nicolas.ojeda.bar@lexifi.com"; x-sender="nicolas.ojeda.bar@lexifi.com"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of nicolas.ojeda.bar@lexifi.com) identity=mailfrom; client-ip=85.233.204.164; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="nicolas.ojeda.bar@lexifi.com"; x-sender="nicolas.ojeda.bar@lexifi.com"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mx20.yaziba.net) identity=helo; client-ip=85.233.204.164; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="nicolas.ojeda.bar@lexifi.com"; x-sender="postmaster@mx20.yaziba.net"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0BCAADuiFtWnKTM6VVehA5vrkmRVCGFbgKBGDwQAQEBAQEBAQEQAQEBAQEGDQkJIS6CLYIHAQEBBCNLCwwEAgEIEQQBAQMCCQQZAgJFCggCBBMSiAcDFgmoU4sVA4RJAQEBAQEFAQEBAQEegQGFVIR9glOCBoMcgUQFhhEMkDqFKoYdgXGCJJJmh1UCOIJSgV49NIVxAQEB X-IPAS-Result: A0BCAADuiFtWnKTM6VVehA5vrkmRVCGFbgKBGDwQAQEBAQEBAQEQAQEBAQEGDQkJIS6CLYIHAQEBBCNLCwwEAgEIEQQBAQMCCQQZAgJFCggCBBMSiAcDFgmoU4sVA4RJAQEBAQEFAQEBAQEegQGFVIR9glOCBoMcgUQFhhEMkDqFKoYdgXGCJJJmh1UCOIJSgV49NIVxAQEB X-IronPort-AV: E=Sophos;i="5.20,361,1444687200"; d="scan'208";a="155528209" Received: from mx20.yaziba.net ([85.233.204.164]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/ADH-AES256-SHA; 30 Nov 2015 00:28:09 +0100 Received: from mta30.int.yaziba.net (mta30.int.yaziba.net [10.4.20.32]) by mx20.yaziba.net (mx10.yaziba.net) with ESMTP id A78591A73B7; Mon, 30 Nov 2015 00:28:08 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by mta30.int.yaziba.net (Postfix) with ESMTP id 9987CCC7D5; Mon, 30 Nov 2015 00:28:08 +0100 (CET) X-Virus-Scanned: amavisd-new at mta30.int.yaziba.net Received: from mta30.int.yaziba.net ([127.0.0.1]) by localhost (mta30.int.yaziba.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id g8RrLYxUPQHb; Mon, 30 Nov 2015 00:28:08 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by mta30.int.yaziba.net (Postfix) with ESMTP id 72F95CC817; Mon, 30 Nov 2015 00:28:08 +0100 (CET) X-Virus-Scanned: amavisd-new at mta30.int.yaziba.net Received: from mta30.int.yaziba.net ([127.0.0.1]) by localhost (mta30.int.yaziba.net [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id Fd-l8iB7f_4e; Mon, 30 Nov 2015 00:28:08 +0100 (CET) Received: from mail13.int.yaziba.net (mail13.int.yaziba.net [10.4.20.66]) by mta30.int.yaziba.net (Postfix) with ESMTP id 54063CC7D5; Mon, 30 Nov 2015 00:28:08 +0100 (CET) Date: Mon, 30 Nov 2015 00:28:08 +0100 (CET) From: Nicolas Ojeda Bar To: Mark Hayden Cc: caml-list@inria.fr Message-ID: <2032001085.281825.1448839688192.JavaMail.zimbra@lexifi.com> In-Reply-To: <887BAE10-E073-40B2-8607-BE5444CBA2FC@yahoo.com> References: <20151129110020.D92E17FD40@sympa.inria.fr> <887BAE10-E073-40B2-8607-BE5444CBA2FC@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Mailer: Zimbra 8.0.6_GA_5922 (ZimbraWebClient - SAF9 (Mac)/8.0.6_GA_5922) Thread-Topic: OCaml optimization Thread-Index: XgW4HrG0MDgelYQbZpecYavzHOOG8w== X-DRWEB-SCAN: ok X-VRSPAM-SCORE: -51 X-VRSPAM-STATE: legit X-VRSPAM-CAUSE: gggruggvucftvghtrhhoucdtuddrfeekhedrkeeigddujeegucetufdoteggodetrfcurfhrohhfihhlvgemucggtfgfnhhsuhgsshgtrhhisggvpdgjtegkkfeuteenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnegoufhushhpvggtthffohhmrghinhculdegledmnecujfgurhepfffhvffkjghfufggtgfgofhtsehtqhgttdertdejnecuhfhrohhmpefpihgtohhlrghsucfqjhgvuggruceurghruceonhhitgholhgrshdrohhjvggurgdrsggrrheslhgvgihifhhirdgtohhmqeenucffohhmrghinhepihhnrhhirgdrfhhrpdihrghhohhordgtohhmnecurfgrrhgrmhepmhhouggvpehsmhhtphhouhht X-VRSPAM-EXTCAUSE: mhhouggvpehsmhhtphhouhht Subject: Re: [Caml-list] OCaml optimization Dear Mark, I would like to suggest a third way to write array summation which 1) avoids the closure allocations, 2) is very natural, and 3) does not requ= ire much cleverness from the compiler. Additionally, this form plays well with the float unboxing heuristics in th= e compiler (in the case when you are adding floats): let sum2 a =3D let s =3D ref 0 in for i =3D 0 to Array.length a - 1 do s :=3D !s + a.(i) done; !s Best wishes, Nicolas ----- Original Message ----- From: "Mark Hayden" To: caml-list@inria.fr Sent: Sunday, 29 November, 2015 7:16:57 PM Subject: [Caml-list] OCaml optimization Below are two ways to write array summation in OCaml, sum0 and sum1. Both = of them use recursive functions. =E2=80=99sum0' allocates a closure on eac= h call but I think is more natural. =E2=80=98sum1=E2=80=99 does do any all= ocation, but is awkward to write. Is there a compiler flag or other way to= get the OCaml native compiler to avoid closure allocation in functions in = the form of =E2=80=98sum0=E2=80=99? best, Mark Running each function 1000 times show sum0 is allocating 6 words on each ca= ll. % ocamlopt -o loop0 -S -inline 10 -unsafe loop0.ml ./loop0 sum0:6029 sum1:23 open Printf ;; (* Allocates *) let sum0 a =3D let len =3D Array.length a in let rec loop ofs sum =3D if ofs < len then ( loop (succ ofs) (sum + a.(ofs)) ) else ( sum ) in loop 0 0 ;; let rec sum1_help a len ofs sum =3D if ofs < len then ( sum1_help a len (succ ofs) (sum + a.(ofs)) ) else ( sum ) ;; (* Does not allocate *) let sum1 a =3D let len =3D Array.length a in sum1_help a len 0 0 ;; let test which sum =3D let a =3D Array.make 123 123 in let minor0 =3D (Gc.stat ()).Gc.minor_words in =20=20 for ofs =3D 0 to 1000 do ignore (sum a) done ; let minor1 =3D (Gc.stat ()).Gc.minor_words in printf "%s:%.0f\n" which (minor1 -. minor0) ; ;; let _ =3D test "sum0" sum0 ; test "sum1" sum1 ; ;; --=20 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