From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id PAA31412; Thu, 26 Apr 2001 15:47:31 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id PAA31476 for ; Thu, 26 Apr 2001 15:47:30 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f3QDlR529370; Thu, 26 Apr 2001 15:47:27 +0200 (MET DST) Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id PAA30782; Thu, 26 Apr 2001 15:47:27 +0200 (MET DST) Date: Thu, 26 Apr 2001 15:47:27 +0200 From: Xavier Leroy To: Chris Hecker Cc: caml-list@inria.fr Subject: Re: [Caml-list] two unrelated questions Message-ID: <20010426154727.E29782@pauillac.inria.fr> References: <200104252108.OAA02055@smtp2-cm.mail.eni.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: <200104252108.OAA02055@smtp2-cm.mail.eni.net>; from checker@d6.com on Wed, Apr 25, 2001 at 02:08:33PM -0700 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > 1. What is the right "functional pattern" for early-outing on success > while using an iter/map/fold type function? > Is throwing an exception the best bet? I believe so. > 2. I'm confused as to when the compiler decides to inline and when it > doesn't. None of the calls in the following program get inlined, > regardless of the value of -inline in ocamlopt. It's an embarrassing example of two distinct optimizations that conflict, resulting in bad code :-) The first optimization is that constant data structures, such as the tuple (1,2) in your example, are pre-built at compile-time rather than dynamically built at run-time. So, by the time we get to the inliner, the "a" value is no longer syntactically a pair, but some sort of special constant. The second optimization is the tupled function optimization, whereas functions taking a N-tuple of arguments are replaced by a function taking N arguments. Such functions are inlined only when applied to an argument that is syntactically a N-tuple. Otherwise, the "slow path" (that takes a real tuple as argument, fetches its N components, and jumps to the fast entry point) is followed, and this turns inlining off. You would get inlining in your example after rewriting it like this, for instance: > let myfst (a,b) = a > let mysnd (a,b) = b > > let myfsti ((a,b):int*int) = a > let mysndi ((a,b):int*int) = b > > let f x y = > myfst (x,y) + 1; > 1 + mysnd (x,y); > myfsti (x,y); > mysndi (x,y) > > let _ = f 1 2 I agree this behavior is surprising and should be fixed at some point. > (and after running dumpobj on the bytecode and > looking at the asm from ocamlopt, we went and saw with dismay how > Pervasives.fst and .snd are builtins and not inlined functions) The bytecode compiler performs no inlining at all. This is the main reason why fst and snd are builtins (so that they do not generate function calls in bytecoded programs). - Xavier Leroy ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr