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 mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 9A01D7FA15 for ; Mon, 30 Nov 2015 10:53:17 +0100 (CET) IronPort-PHdr: 9a23:+/9FGRHb4OAoPl7yXKJcgJ1GYnF86YWxBRYc798ds5kLTJ75r82wAkXT6L1XgUPTWs2DsrQf27eQ4/+rATZIyK3CmU5BWaQEbwUCh8QSkl5oK+++Imq/EsTXaTcnFt9JTl5v8iLzG0FUHMHjew+a+SXqvnYsExnyfTB4Ov7yUtaLyZ/niqbipNaLOU1hv3mUX/BbFF2OtwLft80b08NJC50a7V/3mEZOYPlc3mhyJFiezF7W78a0+4N/oWwL46pyv+YJa6jxfrw5QLpEF3xmdjltvIy4/SXEGC6G/HhUcmwHnhdOS1zA5Qv5UZf4oyf3sMJs0SmdOov9SrViChq46KI+bRbyi288Nz8/72zTjoQkiblboRS+qhh764zdZoyOKOBzc7+bdtQfEzkSFv1NXjBMV9vvJ7AECPAMaKMB99Hw Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=mshinwell@janestreet.com; spf=Pass smtp.mailfrom=mshinwell@janestreet.com; spf=None smtp.helo=postmaster@mxout1.mail.janestreet.com Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of mshinwell@janestreet.com) identity=pra; client-ip=38.105.200.112; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="mshinwell@janestreet.com"; x-sender="mshinwell@janestreet.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of mshinwell@janestreet.com designates 38.105.200.112 as permitted sender) identity=mailfrom; client-ip=38.105.200.112; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="mshinwell@janestreet.com"; x-sender="mshinwell@janestreet.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@mxout1.mail.janestreet.com) identity=helo; client-ip=38.105.200.112; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="mshinwell@janestreet.com"; x-sender="postmaster@mxout1.mail.janestreet.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0BRAADZG1xWnHDIaSZehA5vBq5HkVQZhXYCgR0HPBABAQEBAQEBARABAQEBAQYWCU+CLYIIAQEEEhEdAQE3AQ8LCw0CAiYCAiISAQUBChIGEyKHdwMSA5wCgTE+MYpXcYRjAQWLOAEBAQEGAQEBAQEBFwYKd4VThH6CU4IGgxyBRIYWDJA6hSqGHYFxgiSSZoYKEiSBFziCLyOBXXKFcQEBAQ X-IPAS-Result: A0BRAADZG1xWnHDIaSZehA5vBq5HkVQZhXYCgR0HPBABAQEBAQEBARABAQEBAQYWCU+CLYIIAQEEEhEdAQE3AQ8LCw0CAiYCAiISAQUBChIGEyKHdwMSA5wCgTE+MYpXcYRjAQWLOAEBAQEGAQEBAQEBFwYKd4VThH6CU4IGgxyBRIYWDJA6hSqGHYFxgiSSZoYKEiSBFziCLyOBXXKFcQEBAQ X-IronPort-AV: E=Sophos;i="5.20,364,1444687200"; d="scan'208";a="189668819" Received: from mxout1.mail.janestreet.com ([38.105.200.112]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Nov 2015 10:53:16 +0100 Received: from tot-qpr-mailcore2.delacy.com ([172.27.56.106] helo=tot-qpr-mailcore2) by mxout1.mail.janestreet.com with esmtps (TLSv1:DHE-RSA-AES256-SHA:256) (Exim 4.82) (envelope-from ) id 1a3L90-000676-6w for caml-list@inria.fr; Mon, 30 Nov 2015 04:53:14 -0500 X-JS-Flow: external Received: by tot-qpr-mailcore2 with JS-mailcore (0.1) (envelope-from ) id BWXByK-AAAEO6-FA; 2015-11-30 04:53:14.160900-05:00 Received: from mail-oi0-f54.google.com ([209.85.218.54]) by mxgoog1.mail.janestreet.com with esmtps (UNKNOWN:AES128-GCM-SHA256:128) (Exim 4.72) (envelope-from ) id 1a3L90-0007xI-23 for caml-list@inria.fr; Mon, 30 Nov 2015 04:53:14 -0500 Received: by oige206 with SMTP id e206so91567301oig.2 for ; Mon, 30 Nov 2015 01:53:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=janestreet.com; s=google; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=yT+uFQid6CF7ZDBnLWPlgeyitLvw0feMaQvCHLmRoN8=; b=VWiwhOGk9QSZMfvC3Ow40beLV0ElElre8eE3da2Dwphq3fe6cGg23KBvrJJFlvp7qS PJD5bgVvfkJFAPRtRJdKjUAfcsStBJ7lYUSn3SlWZgflGyp9aSroXPDU4GKOT8weXBjz 3nuk8MiW4ra0ltyTMgqFv4t5prIXyIE8d+o10= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=yT+uFQid6CF7ZDBnLWPlgeyitLvw0feMaQvCHLmRoN8=; b=PNuDKr+TgctIivW8PXr/OUFx5j0yKqL7+g9MwfIBIzmFQXfOJXMiWYjB2lYGDcJJU1 C+PpA3+XL5+7rOhhgkq+g9OM2aZnMluySdpFZ6BUgUNskTM6/MylsLTnD0SBrmiUVvn/ Yc7zpvqIRQ1Q2Y4FKfPRHXrfqmdJUbXkBE/llAy5+SNwAX3/UCv+Aq0Z18t1SEptCfYa c4Efgmi8KJblo5nRrpjaAwcPpK7qjmzD4VLXVaj1Uw/Y+EQIrRrXJewEBM614cES1T7E g3JWF8eNxz6atTxCDdnXuci5hZ4cI2hEO7ptFkJGp0p2AVZIQ4QlqjPgaGbworbAJtLH wCSA== X-Gm-Message-State: ALoCoQlTCTl+/5sz2MM/En5BfkJ7vgETHVqZyDqkqrwvmQb5bNuonrD4HtvjM0hJf/LQqmBiScWmgZaq8G/XohE41VHyzFMQI9SWumaYvN0VXEwH7yhoDbLN0/TyOPuJStzC59vt8jWi X-Received: by 10.202.198.132 with SMTP id w126mr40934356oif.10.1448877193688; Mon, 30 Nov 2015 01:53:13 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.202.198.132 with SMTP id w126mr40934351oif.10.1448877193556; Mon, 30 Nov 2015 01:53:13 -0800 (PST) Received: by 10.202.206.129 with HTTP; Mon, 30 Nov 2015 01:53:13 -0800 (PST) In-Reply-To: <887BAE10-E073-40B2-8607-BE5444CBA2FC@yahoo.com> References: <20151129110020.D92E17FD40@sympa.inria.fr> <887BAE10-E073-40B2-8607-BE5444CBA2FC@yahoo.com> Date: Mon, 30 Nov 2015 09:53:13 +0000 Message-ID: From:Mark Shinwell To:Mark Hayden Cc:"caml-list@inria.fr" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-JS-Processed-by: mailcore X-Validation-by: mshinwell@janestreet.com Subject: Re: [Caml-list] OCaml optimization The forthcoming version of OCaml (4.03) with the flambda optimization passes can compile the example "sum0" to code that does not allocate. (The particular optimization that enables that probably won't be enabled by default in 4.03, though.) Mark On 29 November 2015 at 18:16, Mark Hayden wrote: > 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 e= ach call > but I think is more natural. =E2=80=98sum1=E2=80=99 does do any allocati= on, 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 > call. > > % 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 > > 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 ; > ;; > > > > > >