From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id E7B02BC57 for ; Wed, 31 Mar 2010 21:18:37 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtgBAIo8s0tKfVI2kWdsb2JhbACREIodCBUBAQEBCQkMBw8ksS2BbIU9LYhMAQEDBYR7BIMe X-IronPort-AV: E=Sophos;i="4.51,342,1267398000"; d="scan'208";a="47808349" Received: from mail-ww0-f54.google.com ([74.125.82.54]) by mail3-smtp-sop.national.inria.fr with ESMTP; 31 Mar 2010 21:18:37 +0200 Received: by wwb24 with SMTP id 24so300028wwb.27 for ; Wed, 31 Mar 2010 12:18:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:received:message-id:subject:from:to:content-type :content-transfer-encoding; bh=QGQB/IalrUs5qifocDIEvK3dZqUZrmg+FkA0xSoLPnE=; b=I9iMSFTPp91O1XyA6FlGUTgpzXzxQ2AAO9W0NsIfkDW1A5d2AWKyY40REwJIl95zSb YdaR9q2pf/j1O2rzXkumtHCJ0kJaOM/uOft4n96hyznDvOH+JA1lM5ShlrwvMcoHRq1C +OQ6qhniVKZiXoirhnm1+u6Dmu1ZQohQYkaTU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=i3HOre/Q58xtUZiT2VWPuvrG8x1tmyPEPaHajQUWOU8GM5V0iU5fYcGT2y++vtqr7l TBl9XLaHSOQ+SfWIS3VFwGMtbPPuaZg/Z7or0y5CIQ6XkzrNot1gXS9AEaTBZct2v41i MOqOD8dQHRI3MqNMPZ2nkPwmebwb9QFSPatyE= MIME-Version: 1.0 Received: by 10.216.88.147 with HTTP; Wed, 31 Mar 2010 12:18:36 -0700 (PDT) In-Reply-To: <4BB39B7C.6050402@frisch.fr> References: <4A9F9015.2010408@inria.fr> <4BB39B7C.6050402@frisch.fr> Date: Wed, 31 Mar 2010 23:18:36 +0400 Received: by 10.216.86.148 with SMTP id w20mr1796298wee.112.1270063116909; Wed, 31 Mar 2010 12:18:36 -0700 (PDT) Message-ID: Subject: Re: [Caml-list] Optimizing Float Ref's From: Dmitry Bely To: Caml List Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; ref's:01 frisch:01 frisch:01 ref's:01 unboxed:01 compiler:01 unboxed:01 val:01 ocaml:01 compiler:01 mutable:01 xavier's:01 iteration:01 val:01 2009:98 On Wed, Mar 31, 2010 at 10:59 PM, Alain Frisch wrote: > On 31/03/2010 19:21, Dmitry Bely wrote: >> >> On Thu, Sep 3, 2009 at 1:44 PM, Xavier Leroy >> =A0wrote: >> (...) >>>> >>>> But, I thought that float ref's were automatically unboxed by the >>>> compiler >>>> when they didn't escape the local context. >>> >>> Yes, if all uses of the float ref are unboxed, which is the case in >>> your code. >> >> let max_val (a:float array) =3D >> =A0 let m =3D ref min_float in >> =A0 for i =3D 0 to (Array.length a) - 1 do >> =A0 =A0 if a.(i)> =A0!m >> =A0 =A0 then m :=3D a.(i) >> =A0 done; >> =A0 !m >> >> !m is not unboxed (Ocaml 3.11). Should it? > > As far as I can tell, the boxing for the reference cell is removed by the > compiler (that is, the reference is implemented as a mutable local > variable), but not the boxing for the float contained in the reference. > Since a is a float array, it is needed to box the float before putting it= in > the reference, hence the allocation. Hmm, my interpretation of Xavier's words was that the compiler would completely optimize out memory allocation and keep !m in register. I can live with the allocation but not at every iteration... How to rewrite the function to get rid of it? My best achievement so far is let max_val (a:float array) =3D let m =3D [|-.min_float|] in for i =3D 0 to (Array.length a) - 1 do if a.(i) > m.(0) then m.(0) <- a.(i) done; m.(0) but it looks ugly... - Dmitry Bely